|
使用C++实现链表的操作。欢迎大家留言提问。
- // 1.cpp : 定义控制台应用程序的入口点。
- // 16.线性表.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- //enum bool{false,true};
- template<class T>
- class LinearList
- {
- public:
- LinearList(){}
- ~LinearList(){}
- virtual int Size() const =0; //表的最大长度
- virtual int Length() const =0; //表的长度
- virtual int Search(T &x) const =0;
- virtual int Locate(int i) const =0;
- virtual bool getData(int i,T &x) const =0;
- virtual void setData(int i,T &x) =0;
- virtual bool Insert(int i,T &x) =0;
- virtual bool Remove(int i,T &x) =0;
- virtual bool IsEmpty() const =0;
- virtual bool IsFull() const =0;
- virtual void Sort() =0;
- virtual void input()=0;
- virtual void output()=0;
- virtual LinearList<T>operator=(LinearList<T>& L)=0;
- };
- template<class T>
- struct LinkNode
- {
- T data;
- LinkNode<T> * link;
- LinkNode(LinkNode<T> *ptr=NULL)
- {
- link=ptr;
- }
- LinkNode(const T&item,LinkNode<T> *ptr=NULL)
- {
- data=item;
- link=ptr;
- }
- };
- template<class T>
- class List
- //class List:public LinearList<T>
- {
- protected:
- LinkNode<T> *first;
- public:
- List()
- {
- first=new LinkNode<T>;
- }
- List(const T& x)
- {
- first=new LinkNode<T>(x);
- }
- List(List<T> &L)
- {
- T value1;
- LinkNode<T>*srcptr=L.getHead();
- LinkNode<T>*destptr=first=new LinkNode<T>;
- while(srcptr->link!=NULL)
- {
- value1=srcptr->link->data;
- destptr->link=new LinkNode<T>(value1);
- destptr=destptr->link;
- srcptr=srcptr->link;
- }
- destptr->link=NULL;
- }
- ~List(){makeEmpty();}
- void makeEmpty()
- {
- LinkNode<T>* q;
- while(first->link!=NULL)
- {
- q=first->link;
- first->link = q->link;
- delete q;
- }
- }
- int Length()const
- {
- LinkNode<T>* p;
- int count=0;
- p=first->link;
- while(p!=NULL)
- {
- p=p->link;
- count++;
- }
- return count;
- }
- LinkNode<T>* getHead()const
- {
- return first;
- }
- bool IsEmpty()const
- {
- return first->link==NULL?true:false;
- }
- bool IsFull()const
- {
- return false;
- }
- LinkNode<T> * search(T x)
- {
- LinkNode<T>* current;
- current=first->link;
- while(current!=NULL)
- {
- if(current->data==x)
- {
- break;
- }
- else
- {
- current=current->link;
- }
- }
- return current;
- }
- LinkNode<T>* Locate(int i)
- {
- LinkNode<T>* current;
- current=first;
- int k=0;
- for (;k<i;k++)
- {
- if (current==NULL)
- {
- break;
- }
- current=current->link;
- }
- return current;
- }
- bool getData(int i,T &x)const
- {
- if (i<=0)
- {
- return NULL;
- }
- LinkNode<T>*current=Locate(i);
- if (current==NULL)
- {
- return false;
- }
- else
- {
- x=current->data;
- return true;
- }
- }
- void setData(int i,T &x)
- {
- if (i<=0)
- {
- return;
- }
- LinkNode<T>*current=Locate(i);
- if (current==NULL)
- {
- return;
- }
- else
- {
- current->data=x;
- }
- }
- bool Insert(int i,T &x)
- {
- if(i<0)
- {
- return false;
- }
- LinkNode<T>*current=Locate(i);
- if (current==NULL)
- {
- return false;
- }
- LinkNode<T>*newNode=new LinkNode<T>(x);
- if (newNode==NULL)
- {
- cerr<<"存储器空间分配错误"<<endl;
- exit(1);
- }
- newNode->link=current->link;
- current->link=newNode;
- return true;
- }
- bool Remove(int i,T &x)
- {
- LinkNode<T>*current=Locate(i-1);
- if (current==NULL||current->link==NULL)
- {
- return false;
- }
- LinkNode<T>*del=current->link;
- current->link=del->link;
- x=del->data;
- delete del;
- return true;
- }
- void output()
- {
- LinkNode<T>*current=first->link;
- while (current!=NULL)
- {
- cout<<current->data<<endl;
- current=current->link;
- }
- }
- LinkNode<T>& operator=(List<T>&L)
- {
- T value1;
- LinkNode<T>*srcptr=L.getHead();
- LinkNode<T>*destptr=first=new LinkNode<T>;
- while(srcptr->link!=NULL)
- {
- value1=srcptr->link->data;
- destptr->link=new LinkNode<T>(value1);
- destptr=destptr->link;
- srcptr=srcptr->link;
- }
- destptr->link=NULL;
- return *this;
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- List<int> a(1);
- int c=10;
- int d=20;
- a.Insert(0,c);
- a.Insert(1,d);
- int b=0;
- b=a.Length();
- cout<<b<<endl;
- a.output();
- List<int> h(a);
- // h=a;
- d=1000;
- h.setData(1,d);
- h.output();
- return 0;
- }
- 输出结果如下:
复制代码
|
赞赏
-
1
查看全部赞赏
-
|