收藏 分销(赏)

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

上传人:仙人****88 文档编号:12012808 上传时间:2025-08-27 格式:DOC 页数:6 大小:52.50KB 下载积分:10 金币
下载 相关
单链表的相关操作(数据结构).doc_第1页
第1页 / 共6页
单链表的相关操作(数据结构).doc_第2页
第2页 / 共6页


点击查看更多>>
资源描述
#include <cstdlib> #include <iostream> using namespace std; typedef struct Lnode { int data; struct Lnode *next; }LNode; int Get_i(LNode *L) { cout<<"输入查找的是第几个元素"; int a,i=1; LNode *k=L; cin>>a; while(i<=a&&k->next!=NULL) { i++; k=k->next; } cout<<"第"<<a<<"个元素是"<<k->data<<endl; } int Get_value(LNode *L) { cout<<"输入查找元素的值是:"; int count=1,x; LNode *k=L; cin>>x; while(k->next->data!=x&&k->next!=NULL) { count++; k=k->next; } cout<<"所查找的元素"<<x<<"是第"<<count<<"个"<<endl; } void Insert(LNode *L) { cout<<"输入需要插入数据的位置:"; int i,j=1,k; //i是插入的位置 ,k是插入的数据 LNode *p,*q,*r; r=L; p=L; cin>>i; cout<<" "<<"需要插入的数据是:"; cin>>k; while(j<i) { p=p->next; j++; } q=(LNode*)malloc(sizeof(LNode)); q->data=k; q->next=p->next; p->next=q; cout<<"现在的链表中数据是"<<endl; while(r->next!=NULL) { cout<<r->next->data<<" "; r=r->next; } } void Delete_i(LNode *L) { cout<<"输入需要删除数据的位置:"; int i,j=0; cin>>i; LNode *p=L,*q,*r; r=L; while(j<i) { j++; q=p; p=p->next; } q->next=p->next; free(p); cout<<"现在的链表中数据是"<<endl; while(r->next!=NULL) { cout<<r->next->data<<" "; r=r->next; } cout<<endl; } void Delete_value(LNode *L) { cout<<"输入需要删除数据的值:"; int x; 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<<"现在的链表中数据是"<<endl; while(r->next!=NULL) { cout<<r->next->data<<" "; r=r->next; } cout<<endl; } void Join(LNode *L) { int x; LNode *head,*p,*q,*r; head=(LNode*)malloc(sizeof(LNode)); head->next=NULL; q=head; cout<<"请输入数据建立另一个链表并同样以输入数字0为结束标志:"<<endl; while(1) { 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<<"第二个链表存数为:"<<endl; r=head; while(r->next!=NULL) { cout<<r->next->data<<" "; r=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<<"现在的链表中数据是"<<endl; while(b->next!=NULL) { cout<<b->next->data<<" "; b=b->next; } cout<<endl; } int main(int argc, char *argv[]) { int x; LNode *head,*p,*q,*r; head=(LNode*)malloc(sizeof(LNode)); head->next=NULL; q=head; cout<<"请输入数据建链表并以输入数字0为结束标志:"<<endl; while(1) { 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<<"链表存数为:"<<endl; r=head; while(r->next!=NULL) { cout<<r->next->data<<" "; r=r->next; } cout<<endl; cout<<"************************************************"<<endl; cout<<"0:退出系统"<<endl; cout<<"1:按位置查找元素"<<endl; //Get_i(head); cout<<"2:按值查找元素"<<endl; //Get_value(head); cout<<"3: 执行插入操作"<<endl; //Insert(head); cout<<"4: 按位置删除元素"<<endl; //Delete_i(head); cout<<"5: 按值删除数据"<<endl; //Delete_value(head); cout<<"6: 建立一个新的链表并与前一个链表连接"; //Join(head); cout<<endl<<"************************************************"<<endl; int t; do{ cout<<"请输入选项执行相应的操作:"; cin>>t; switch(t) { case 0: cout<<"***********退出系统************"<<endl; break; case 1: Get_i(head); break; case 2: Get_value(head); break; case 3: Insert(head); break; case 4: Delete_i(head); break; case 5: Delete_value(head); break; case 6: Join(head); break; default : cout<<"ERROR"<<endl; break; } }while(t!=0); system("PAUSE"); return EXIT_SUCCESS; } 相应的截图为:
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服