欢迎来到天天文库
浏览记录
ID:11777730
大小:49.00 KB
页数:19页
时间:2018-07-13
《c++链表、栈、队列用法示例》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、C++链表操作示例*#29·链表创建structts*Create(){structts*head=NULL,*tail=NULL,*newnode;//首节点newnode=newts;strcpy(newnode->name,"张三");strcpy(newnode->num,"13167543241");head=newnode;tail=newnode;tail->next=NULL;//第二个newnode=newts;strcpy(newnode->name,"李四");strcpy(ne
2、wnode->num,"13287543245");tail->next=newnode;tail=newnode;tail->next=NULL;returnhead;}//初始化*#30·链表输出//显示所有信息voiddisplay(ts*head){ts*p=head;if(head==NULL)//链表为空{cout<<"链表为空!"<name<<"t"<num<
3、<"t"<next;}}//display*#31·插入节点//开头插入节点voidadd(ts*&head,ts*neod){structts*tail=NULL;if(head==NULL){head=neod;tail=neod;tail->next=NULL;}else{neod->next=head;head=neod;}}//插入节点//结尾插入节点voidinst(ts*&head,ts*neod){structts*tail=NULL,*p=head;if(he
4、ad==NULL){head=neod;tail=neod;tail->next=NULL;}else{while(p->next!=NULL){p=p->next;}p->next=neod;neod->next=NULL;}}//插入节点*#32·链表删除//删除功能voiddel(ts*&head,charc[]){ts*p=head;//首节点if(strcmp(c,p->name)==0){head=p->next;//p=head;}//ifelse{while(p!=NULL){if(s
5、trcmp(c,p->next->name)==0){ts*q;q=p->next;p->next=q->next;break;}//ifp=p->next;}//while}//else;}//del//删除头结点voiddelh(ts*&head){if(head==NULL)return;elsehead=head->next;//p=head;}//delh//删除尾结点voiddelt(ts*&head){structts*tail=NULL,*p=head;if(head==NULL)re
6、turn;else{while(p->next->next!=NULL)p=p->next;p->next=NULL;}}//delt*#33·链表查找//查找byindexstructts*serch(ts*head,intr){structts*p=head;intj=1;if(r<0)cout<<"编号错误"<next;}if(r==j&&p!=NULL)returnp;else{cout<<"编号错误"<7、l;returnhead;}}}//serch*#34·数组函数#include#include#includestructtest{intx;//操作数1inty;//操作数2intz;//答案};//创建voidcre(inta[][10],intline)//数组,总数,范围上届{inti=0,j=0;srand(time(NULL));//时间种子for(i=0;i8、]=int(rand()%(100-1)+1);//操作数1}}//for}//crevoidbuild(inta[],intlen,intup)//数组,总数,范围上届{inti=0;srand(time(NULL));//时间种子for(i=0;i
7、l;returnhead;}}}//serch*#34·数组函数#include#include#includestructtest{intx;//操作数1inty;//操作数2intz;//答案};//创建voidcre(inta[][10],intline)//数组,总数,范围上届{inti=0,j=0;srand(time(NULL));//时间种子for(i=0;i8、]=int(rand()%(100-1)+1);//操作数1}}//for}//crevoidbuild(inta[],intlen,intup)//数组,总数,范围上届{inti=0;srand(time(NULL));//时间种子for(i=0;i
8、]=int(rand()%(100-1)+1);//操作数1}}//for}//crevoidbuild(inta[],intlen,intup)//数组,总数,范围上届{inti=0;srand(time(NULL));//时间种子for(i=0;i
此文档下载收益归作者所有