1、#include using namespace std; /节点类 class jiedian public: int date; jiedian *next; ; /链表类 class lianbiao jiedian *head;/存放头结点 jiedian *tail; int count;/存放节点的个数 public : lianbiao();/ /lianbiao(lianbiao&);/拷贝构造函数 lianbiao();/ void creat();/创建链表 int insert(int,int);/ 插入一个元素 int del(int);/删除元素 int length
2、(); jiedian* headf(); /int jiedianshumu();/输出 int find(int);/查找指定数据的节点的位置 int findnote(int);/查找指定节点的值 /lianbiao operator =(lianbiao&);/赋值操作符重载 /friend lianbiao lianjie(lianbiao&,lianbiao&);/连接两个链表 friend ostream& operator next=NULL; tail=head; count=0; jj:lianbiao()jiedian *kk=head;jiedian *hh;while
3、 (kk!=NULL) hh=kk-next; delete kk; kk=hh;jiedian* jj:headf()return head;/创建链表void jj:creat()int k;jiedian*h=head;coutplease input the list:k;while(k!=-1) jiedian*xin=new jiedian; count+; xin-date=k; h-next=xin; h=xin; cink;h-next=NULL;tail=h;/插入到表头、表尾、表中int jj:insert(int m,int n) if(mcount) coutwron
4、g insertdate=n; if(m=count) tail-next=xin; tail=xin; xin-next=NULL; count+; return 1; jiedian* h=head; jiedian *kk; for(int i=0;inext; kk=h-next; h-next=xin; xin-next=kk; count+;return 1;int jj:length()return count;int jj:del(int m) if(mcount) coutwrong delendl; return 0; jiedian* h=head; jiedian *k
5、k; for(int j=0;jnext; if(m=count) h-next=NULL; delete tail; tail=h; count-; return 1; kk=h-next; h-next=kk-next; delete kk; return 1; int jj:find(int mm)jiedian *kk=head;jiedian *jj;int j=1;for(j=1;jnext; if(kk-date=mm) return j;int jj:findnote(int mm) if(mmcount) coutwrong positionendl; return 0; i
6、nt j=1; for(j=1;jcount+1;j+) if(j=mm)return j; ostream& operator next; cout; for(int i=0;ij;i+) coutdate; cout; nn=nn-next; couttailendl;void listui();int main()listui();void listui()coutnnendl; cout please input the operation:nnendl; cout %endl; cout % 1.creat %endl; cout % 2.find %endl; cout % 3.d
7、el %endl; cout % 4.insert %endl; cout % 5.length %endl; cout % 6.findnote %endl; cout % 7.shuchu %endl; cout %k;lianbiao kk;doswitch(k)case 1: kk.creat();break;case 2:coutkkendl;cout输入查找位置:jjj;cout kk.find(jjj)endl; break;case 3:coutkkendl;cout输入del位置:jj3;cout kk.del(jj3)endl; break;case 4:coutkkend
8、l;cout输入位置和插入元素:jjj1m;coutkk.insert(jjj1,m)endl;break;case 5:coutkkendl;coutkk.length()endl;break;case 6:coutkkendl;cout输入需查找数据:jjj2;coutnkk.findnote(jjj2);break;case 7:cout输出链表:endl;coutkk;break;coutnnendl;coutplease input the operation:nnendl; cout %endl; cout % 1.creat %endl; cout % 2.find %endl; cout % 3.del %endl; cout % 4.insert %endl; cout % 5.length %endl; cout % 6.findnote %endl; cout % 7.shuchu %endl; cout %endl; coutnnk);