动态链表类linearList

本文介绍了如何定义一个动态链表类LinearList,该类包含插入、删除、获取元素、查找和计算长度等成员函数。具体操作如在指定位置插入元素、删除元素、查找值并返回其位置等。示例代码基于DevC++ 5.11的TDM-GCC 4.9.2 64-Bit Release环境,并提供了头文件iquery.h、成员函数实现文件iquery.cpp以及使用示例main.cpp。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对类的要求:
定义一个由int型元素所构成的动态链表类LinearList,成员函数包括:
bool insert(int x, int pos); //在位置pos之后插入一个元素x;
//pos为0时,在第一个元素之前插入。
//操作成功时返回true,否则返回false。
bool remove(int &x, int pos); //删除位置pos处的元素。
//操作成功时返回true,否则返回false。
int element(int pos) const; //返回位置pos处的元素。
int search(int x) const; //查找值为x的元素,返回元素的位置(第一个元素的位置为1)。未找到时返回0。
int length() const; //返回元素个数。
!!!编译环境 DevC++ 5.11 TDM-GCC 4.9.2 64-Bit Release
头文件 iquery.h

	#ifndef  _IQUERY_H
	#define _IQUERY_H 1
    using std::string;
	struct Node 
	{
		int element;
		Node *next;
	};
	class LinerList{
		private: 
			Node *head;
			int len ;
		public:
			LinerList(){len=0;head = NULL;}
			bool insert(int x,int pos);    //add a new element to any position 
			bool remove(int &x,int pos);   //remove an element
			int element(int pos) const;    //cout this element at positon pos
			int search(int x) const;       //find this element and return this position 
	};

#endif
//	类成员函数中const的使用
//	一般放在函数体后,形如:void   fun()   const;     
//    任何不会修改数据成员的函数都因该声明为const类型。

成员函数实现文件 iquery.cpp


#include <iostream>
#include "string"
#include "iquery.h"
#include "iomanip"
using namespace std; 
bool LinerList::insert(int x,int pos){
	if (pos<0){
		cout<<"请输入正确的pos值(需要>=0)"<<endl;
		return false;
	}else if (pos == 0){
		Node *p = new Node;
		p->element = x;
		if (head !=NULL){
			p->next  = head;
			head = p;
			p->next = NULL;
			len++;
			return true;
		}else{
		head = p;
		p->next=NULL;
		len++;
		return true;
		}
	
	}else if (pos>0&&pos<len){
		if (head !=NULL){
			Node *p   = new Node;
			p->element = x;
			Node *p1 = head,*p2=head;
			for (int i = 0;i<pos;i++){
				p2 = p1;
				p1 = p1->next;
			}
			p2->next = p;
			p->next = p1;
			len++;
			   return true;
			}else{
			Node *p   = new Node;
			p->element = x;
			head = p;
			p->next=NULL;
			len++;
			return true;
		}
		
	}else {
		Node *p = new Node;
		p->element = x;
		Node *p1 = head;
		while (p1->next!=NULL){
			p1 = p1->next;
		}
		p1->next = p;
		p->next = NULL;
		len ++;
		return true;
	}
}


	bool LinerList::remove(int &x,int pos){
		if (pos <0||pos>=len){
			cout<<"请输入正确的pos值!"<<endl;
			return false;
		}else if (pos ==0){
			Node *p = head;
			x = p->element;
			head = p->next;
			len--;
			return true;
		}else if(pos>0&&pos<len-1){
			Node *p1 = head,*p2= head;
			for (int i = 0;i<pos;i++){
				p2 = p1;
				p1 = p1->next;
			}
			p2->next = NULL;
			x = p1->element;
			p1 = p1->next;
			p2->next = p1;
			len--;
			return true;
		}else {
			Node *p1 = head,*p2 = head;
			for (int i = 0;i<pos;i++){
				p2 = p1;
				p1 = p1->next;
			}
			p2->next = NULL;
			len --;
			return true;
		}
	}
	
	
int LinerList::element(int pos) const{
	
	if (pos>=len||pos<0){
		cout<<"请输入正确的pos值"<<endl;
		return -1;
	}else{
		Node *p1 = head;
		for (int i = 0;i<pos;i++){
			p1 = p1->next;
		}
		return p1->element;
	}
}


int LinerList::search(int x)const{
	Node *p = head;
	for (int i = 0;i<len;i++){
		if (p->element ==x){
			return i+1;
		}
		p = p->next;
	}
	return 0;
}

下面是使用范例 main.cpp

#include <iostream>
#include "iquery.h"
#include "iomanip"
using namespace std;
int main(int argc, char** argv) {
	int ele=0;
	char sel;
	int pos;
		LinerList list;
	do{
		cout<<"请输入element:"<<endl;
		cin>>ele;
		if(list.insert(ele,pos)){
			pos++;
		}else {
			cout<<"element加入不成功!!!"; 
		} 
		cout<<"继续?(Y/N)"<<endl;
		cin>>sel;
	}while(sel=='Y');
	//数据打印 
	for(int idx=0;idx<pos;idx++){
		ele=list.element(idx);
		cout<<ele<<"\t";
	}
	//delete 
//	if(list.remove(&pos,0)) {
//		ele=list.element(0);
//		cout<<endl<<ele<<"\t";
//	}
//	else cout<<"delete不成功"<<endl; 
//	
	ele=list.search(8);
	if(ele!=0){
		cout<<"element 8 位置为"<<ele;
	}else cout<<"该element 8 不存在.";
}

注:笔者使用prj文件自动链接代码。
欢迎访问陈风的个人博客

线性表C++语言实现,包含《数据结构算法与应用-C++语言描述》练习题。 template<class T> class LinearList { public: LinearList(int MaxListSize = 10);//构造函数 LinearList(LinearList<T>& L);//复制构造函数 ~LinearList() { delete[] element; }//析构函数 bool IsEmpty() const { return length == 0; }//判断线性表是否为空 int Length() const { return length; }//返回数组长度 void SetLength(int SetLength) { length = SetLength; }//设置数组长度 int MaxSize_ret() const { return MaxSize; }//返回数组最大长度 int Current_ret(void) const { return current; } bool Find(int k, T& x);//返回第k个元素至x中 int Search(const T& x) const;//返回x所在位置 LinearList<T>& Delete(int k, T& x);//删除第k个元素并将它返回至x中 LinearList<T>& Insert(int k, const T& x);//在第k个元素之后插入x LinearList<T>& Insert(const T& x);//在表尾插入x void Output(ostream& out) const; void Self_adaption_LinearList_Size(void); void Reverse(void); bool Half(void); LinearList<T>& Alternate(LinearList<T>& A, LinearList<T>& B); LinearList<T>& Merge(LinearList<T>& A, LinearList<T>& B); LinearList<T>& Merge_1(LinearList<T>& A, LinearList<T>& B); void Split(LinearList<T>& A, LinearList<T>& B); LinearList<T>& Sort(void); /*关于前移和后移*/ void Reset(void) { current = 1; } //置current为0 T Current(void) { return element[current-1]; }//返回当前元素 bool End(void);//当且仅当当前元素为表的最后一个元素时,返回true bool Front(void);//当且仅当当前元素为表的第一个元素时,返回true void Next(void);//移动current至表中的下一个元素,如果操作失败则引发一个异常 void Previous(void);//移动current至表中的前一个元素,如果操作失败则引发一个异常 private: int length;//线性表长度 int MaxSize;//线性表最大可容纳的长度 int current;//记录线性表当前的位置 T* element;//一维动态数组 };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值