单链表的相关操作(数据结构)

单链表的相关操作(数据结构)

ID:34166842

大小:64.55 KB

页数:7页

时间:2019-03-04

单链表的相关操作(数据结构)_第1页
单链表的相关操作(数据结构)_第2页
单链表的相关操作(数据结构)_第3页
单链表的相关操作(数据结构)_第4页
单链表的相关操作(数据结构)_第5页
资源描述:

《单链表的相关操作(数据结构)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

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

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。