资源描述:
《单链表的相关操作(数据结构)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#includeusingnamespacestd;typedefstructLnode{intdata;structLnode*next;}LNode;intGet_i(LNode*L){cout«*'输入査找的是第几个元素”;inta,i=l;LNode*k=L;cin»a;while(i<=a&&k->next!=NULL){i++;k=k->next;}cout«H第”vvavv"个元素是"«k->data«endl;}intGet_value(LNode*L){cout«n输入查找元素的值是:”;intcount
2、=l,x;LNode*k=L;cin»x;while(k->next->data!=x&&k->next!=NULL){count++;k=k->next;}cout«*'所查找的元素M«x«"是第”vvcountvv”个H«endl;}voidInsert(LNode*L){cout«"输入需要插入数据的位置:”;inti,j=l,k;〃i是插入的位置,k是插入的数据LNoder=L;p二L;cin»i;cout«n需要插入的数据是:”;cin»k;while(jnext;j++;}q=(LNode*)malloc(sizeof(LNode));q->da
3、ta=k;q->next=p->next;p->next=q;cout«"现在的链表屮数据是M«endl;while(r->next!=NULL){cout«r->next->data«H”;r=i•->next;}}voidDelete_i(LNode*L){cout«,t输入需要删除数据的位置:”;inti,j=0;cin»i;LNode*p=L,*q,*r;r=L;while(jnext;}q・>next二p・>ncxt;free(p);cout«n现在的链表中数据是M«endl;while(r->next!=NULL){cout«r-
4、>next->data«Hn;r=r->next;}cout«endl;}voidDelete_value(LNode*L){cout«“输入需要删除数据的值:”;intx;cin»x;LNode*p=L,*q,*r=L;while(p->next->data!=x){q=p;p=p->next;}q=p;p=p->next;q->next=p->next;free(p);cout«"现在的链表中数据是M«endl;while(r->next!=NULL){cout«r->next->data«"r=i•->next;}cout«endl;}voidJoin(LNode*L)
5、{intx;LNode*head,*p,*q,*r;head=(LNode*)malloc(sizeof(LNode));head->next=NULL;q=head;cout«"请输入数据建立另一个链表并同样以输入数字0为结束标志vvendl;while(l){cin»x;if(x==0)break;else{p=(LNode*)malloc(sizeof(LNode));p->data=x;p->next=q->next;〃采用尾插法,正序输出链表中数据q->next=p;q二P;}}cout«H第二个链表存数为:M«endl;r=head;while(r->next!=
6、NULL)cout«r->next->data«nr=r->next;cout«endl;LNode*a,*b;a=L;b=L;while(a->next!=NULL){a=a->next;}a->next=head->next;free(head);cout«H现在的链表中数据是M«endl;while(b->next!=NULL){cout«b->next->data«HM;b=b->next;}cout«endl;}intmain(intargc,char*argv[]){intx;LNode*hcad,*p,*q,*r;head=(LNode*)malloc(size
7、of(LNode));head->next=NULL;H«endl;q=head;cout«t,请输入数据建链表并以输入数字0为结束标志:while(l){cin»x;if(x==0)break;else{p=(LNode*)malloc(sizeof(LNode));p->data=x;p->next=q->next;〃采用尾插法,正序输出链表中数据q->next=p;q=p;cout«n链表存数为:n«endl;r=head;while(r->next!=NULL){cout«r->next->da