#include<iostream.h> typedef struct node { char ch; node *next; }*ptrn; class link { private: ptrn head,p2; public: void creatlink() { ptrn p1; head=0; char ch1; int n; while(1) { cout<<"1代表继续输入,-1代表终止输入,请输入"<<endl; cin>>n; if(n==-1) break; else { cout<<"请输入结点元素"<<endl; p1=new node; cin>>ch1; p1->ch=ch1; if(head==0) { head=p1; p2=p1; } else { p2->next=p1; p2=p1; } } } if(head) p2->next=0; } int linklength() { ptrn tem; tem=head; int count=0; while(1) { if(tem!=0) { count++; tem=tem->next; } else return count; } } void accesslink() { ptrn temp; temp=head; cout<<"链表的内容为"<<endl; for(int n=0;n<linklength();n++) { cout<<temp->ch<<endl; temp=temp->next; } } bool empty() { return linklength()!=0; } void accessnode(int position) { ptrn temm; temm=head; for(int i=0;i<position-1;i++) { temm=temm->next; } cout<<temm->ch<<endl; } void accesselem(char &ch1) { ptrn tempp; tempp=head; for(int j=0;;j++) { if(tempp->ch==ch1) { cout<<"元素"<<ch1<<"的位置是"<<j+1<<endl; break; } else { if(j=linklength()-1) cout<<"此链表中无"<<ch1<<endl; else continue; } } } void insertnode(int position,char &ch2) { ptrn te,newnode,pbac; newnode=new node; newnode->ch=ch2; if(position<1||position>linklength()+1) { cout<<"插入的结点超出链表范围"<<endl; } else { te=head; for(int i=0;i<position-2;i++) { te=te->next; } newnode->next=te->next; te->next=newnode; } } void delenode(int position) { ptrn tee,deleno; if(position<1||position>linklength()) { cout<<"要删除的结点超出链表范围"<<endl; } else { tee=head; for(int i=0;i<position-2;i++) { tee=tee->next; } deleno=tee->next; tee->next=deleno->next; delete deleno; } } }; void main() { link link1; char ch3='a',ch4='f'; link1.creatlink(); link1.accesslink(); cout<<"链表的长度为"<<link1.linklength()<<endl; cout<<"链表是否为空"<<link1.empty()<<endl; cout<<"链表中第三个元素为"<<endl; link1.accessnode(3); link1.accesselem(ch3); cout<<"在第四个位置插入元素f"<<endl; link1.insertnode(4,ch4); link1.accesslink(); cout<<"删除链表中第三个元素后链表为"<<endl; link1.delenode(3); link1.accesslink(); }*/***************************************************************** #include<iostream.h> typedef struct node { char ch; node *next,*pre; }*ptrn; class link { private: ptrn head,p2; public: void creatlink() { ptrn p1; head=0; int n; char ch1; while(1) { cout<<"1表示继续输入,-1表示终止输入"<<endl; cin>>n; if(n==-1) break; else { p1=new node; cout<<"请输入结点"<<endl; cin>>ch1; p1->ch=ch1; if(head==0) { head=p1; p2=p1; } else { p2->next=p1; p1->pre=p2; p2=p1; } } } if(head)p2->next=0; } int linklength() { ptrn tem; tem=head; int count=0; for(int i=0;;i++) { if(tem==0) break; else { count++; tem=tem->next; } } return count; } bool empty() { return linklength()!=0; } void accesslink() { ptrn temp; temp=head; for(int i=0;i<linklength();i++) { cout<<temp->ch<<endl; temp=temp->next; } } void accessnode(int position) { ptrn temm; temm=head; if(position<1||position>linklength()) cout<<"你访问的位置不存在"<<endl; else { for(int i=0;i<position-1;i++) { temm=temm->next; } } cout<<"链表中第三个元素为"<<temm->ch<<endl; } void accesselem(char ch2) { ptrn tee=head; for(int i=0;i<linklength();i++) { if(tee->ch==ch2) cout<<ch2<<"的位置为"<<i+1<<endl;break; else { tee=tee->next; } } } void insertnode(int position,char ch3) { ptrn pbac,teem,newnode; teem=head; if(position<1||position>linklength()) cout<<"插入的位置超出范围"<<endl; else { newnode=new node; newnode->ch=ch3; if(position==1) { newnode->next=head; head->pre=newnode; head=newnode; } else { for(int i=0;i<position-2;i++)********************************************双链表 { teem=teem->next; } pbac=teem->next; teem->next=newnode; newnode->pre=teem; newnode->next=pbac; pbac->pre=newnode; } } } void delenode(int position) { ptrn prac,pbac,te; te=head; if(position==1) { head=head->next; head->pre=0; delete te; } else { for(int i=0;i<position-1;i++) { te=te->next; } prac=te->pre; pbac=te->next; prac->next=pbac; pbac->pre=prac; delete te; } } }; void main() { char ch1='a',ch2='f'; link link1; link1.creatlink(); cout<<"链表的长度为"<<link1.linklength()<<endl; cout<<"链表的内容为"<<endl; link1.accesslink(); cout<<"链表是否为空"<<link1.empty()<<endl; link1.accessnode(3); link1.accesselem(ch1); cout<<"在第四个位置插入"<<ch2<<endl; link1.insertnode(4,ch2); cout<<"链表的内容为"<<endl; link1.accesslink(); cout<<"删除第三个元素后"<<endl; link1.delenode(3); link1.accesslink(); }************************************************************************************ #include<iostream.h> typedef struct node { char ch; node *next; }*ptrn; ptrn creatlink() { char ch1; int n; ptrn head,p2,p1; head=0; while(1) { cout<<"1表示继续输入,-1表示终止输入"<<endl; cin>>n; p1=new node; if(n==-1)break; else { cout<<"请输入结点"<<endl; cin>>ch1; p1->ch=ch1; if(head==0) { head=p1; p2=p1; } else { p2->next=p1; p2=p1; } } } if(head)p2->next=head; return head; } ptrn combine(ptrn &a,ptrn &b,ptrn &c) { a=creatlink(); b=creatlink(); c=a; ptrn temp; temp=c; while(1) { if(temp->next->ch!=c->ch) temp=temp->next; else break; } temp->next=b; temp=temp->next; while(1) { if(temp->next->ch!=b->ch) temp=temp->next; else break; } temp->next=c; return c; } void outputlink(ptrn &link1) { ptrn tem; tem=link1; while(1) { if(tem->next->ch!=link1->ch) { cout<<tem->ch<<endl; tem=tem->next; } else break; } cout<<tem->ch<<endl; } void main() { ptrn ha,hb,hc; combine(ha,hb,hc); outputlink(hc); }