单链表的方法函数实现-(严蔚敏版)
来源:互联网 发布:星野竜一 知乎 编辑:程序博客网 时间:2024/06/08 16:47
这学期我们学数据结构课程,这些代码是平时的练习代码。
这个是室友大神写的,写的真好看!
我感觉重点是 1 链表的逆置,不用开辟新的空间,很好用。
2 用pair类型接受两个返回值,起到确保的作用。
主要有 逆置,删偶数,输出,输入,合并非递减链表。等等
#include <bits/stdc++.h>using namespace std;typedef int ElemType;typedef struct node{ ElemType data; struct node *next;}Node,*linklist;//左面是结构体变量的名字,右边是结构体指针的新名字。pair<linklist,bool> creatlist() //创建操作{ //该创建函数返回两个变量,一个是是否创建成功的头结点,一个是证明成功的bool型。 //本质没有啥区别。只是更稳妥一点,并且需要c++对pair类型的支持。。 pair<linklist,bool> m; linklist head,p,q; ElemType num; head=(linklist)malloc(sizeof(Node)); if(head==NULL) {m.second=false; m.first=head; return m; } else m.second=true; p=head; scanf("%d",&num); while(num!=-1) { q=(linklist)malloc(sizeof(Node)); q->data=num; p->next=q; p=q; scanf("%d",&num); } p->next=NULL;//封顶。 m.second=true; m.first=head; return m;}bool nizhi(linklist ha)/*将链表尼至*/{//要求不能创建新的空间。如果可以的话直接来个数组存一下妥妥的。 linklist p,q; p=ha->next; ha->next=NULL; while(p!=NULL) { q=p; p=p->next; q->next=ha->next; ha->next=q; } //haha=ha; return true;}void deleted(linklist h) //删除结点{ linklist p,q; q=h; p=h->next; while(p!=NULL) { if((p->data%2==0)) {q->next=p->next; p=q->next;} else { p=p->next; q=q->next; } }}bool display(linklist h) /*输出链表*/{ linklist p=h->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } cout<<endl; return true;}void add(linklist h,linklist a){ linklist p; p=h; while(p->next!=NULL) { if(p->next->data>=a->data) { a->next=p->next; p->next=a; break; } p=p->next; } if(p->next==NULL&&p->data<=a->data) { p->next=a; a->next=NULL; }}linklist creatlist_h() //创建递增链表{ linklist h=(linklist)malloc(sizeof(Node)); linklist q; q=(linklist)malloc(sizeof(Node)); int x; cin>>x; q->data=x; h->next=q; q->next=NULL; scanf("%d",&x); while(x!=-1) { q=(linklist)malloc(sizeof(Node)); q->data=x; add(h,q); scanf("%d",&x); } return h;}linklist MergeList() //合并两个递增链表{ //pair<linklist,bool> h,l; linklist h=creatlist_h(); linklist l=creatlist_h(); linklist ha,la,head,hd; ha=h->next; la=l->next; head=hd=h; while(ha&&la) { if(ha->data<=la->data) { hd->next=ha; hd=ha; ha=ha->next; } else { hd->next=la; hd=la; la=la->next; } } hd->next=ha?ha:la; free(l); return head;}void seq(linklist &h,linklist &l) // 分离一个链表为奇偶两个链表{ pair<linklist,bool>head; head=creatlist(); linklist la,ha,hd; h=head.first; hd=head.first->next; l=(linklist)malloc(sizeof(Node)); l->next=NULL; la=l; ha=h; while(hd!=NULL) { if(hd->data%2==0) { ha->next=hd; ha=hd; hd=hd->next; } else { la->next=hd; la=hd; hd=hd->next; } } ha->next=NULL; la->next=NULL;}int main(){ int n; linklist head,h; linklist l=(linklist)malloc(sizeof(Node)); pair<linklist,bool>haha; cin>>n; while(1) { system("pause"); cout<<"要想关闭按crtl+z"<<endl; cout<<"***************功能菜单******************"<<endl; cout<<"创建一个无序链表按1"<<endl; cout<<"2删除偶数结点按2"<<endl; cout<<"逆置链表按3"<<endl; cout<<"在非递减有序链表中插入一个元素使链表元素仍有序按4"<<endl; cout<<"建立两个非递减有序单向链表,然后合并成一个非递增链表按5"<<endl; cout<<"分解链表,其中一个全部为奇数,另一个全部为偶数按6"<<endl; cout<<"输出按7"<<endl; cout<<"我不想选了88"<<endl; cout<<endl; int m; while(cin>>m) {switch(m) { case 1:cout<<"请输入链表元素,以“-1”结束"<<endl; haha=creatlist(); if(!haha.second) cout<<"eror"<<endl; continue; case 2:deleted(haha.first);continue; case 3:nizhi(haha.first);continue; case 4:cout<<"请输入需要插入元素:"<<endl; cin>>l->data; add(haha.first,l); continue; case 5:cout<<"请输入两个链表,分别以“-1”结束:"<<endl; haha.first=MergeList(); continue; case 6:cout<<"请输入链表元素,以“-1”结束"<<endl; seq(haha.first,h); display(haha.first); display(h); continue; case 7: display(haha.first); continue; case 8:goto modo; } } } modo:cout<<"祝您万事如意,hiahiahiahia"<<endl; return 0;}
0 0
- 单链表的方法函数实现-(严蔚敏版)
- 虚函数的实现方法
- atoi的方法实现函数
- strcmp函数的实现方法
- 方法(函数)的声明与实现、方法(函数)的调用
- 字符串操作函数的实现方法(代码)
- js 实现定时对函数的调用setInterval()方法
- 多种实现函数回调的方法
- C函数实现返回多个值的方法
- MySQL rand函数实现随机数的方法
- 函数指针实现的两种方法
- strlen函数实现的几种方法
- atoi和itoa函数的实现方法
- strcpy函数的最优实现方法
- strcpy函数的最优实现方法
- atoi和itoa函数的实现方法
- strlen函数实现的两种方法
- 函数方法实现的取出汉字首字母
- 批量上传图片前展示
- BZOJ4816 [Sdoi2017]数字表格
- Android——使用android-support-multidex解决Dex超出方法数的限制问题
- jenkins添加ldap
- 使用JS,如何给下拉列表动态的添加一个option 选项?
- 单链表的方法函数实现-(严蔚敏版)
- ssm框架 详细整合教程(spring+spring-mvc+mybatis)
- java中string与date(日期)格式之间的转换
- HTTP
- 二叉树的层次遍历 II
- 手动从svn删除文件后,重新向工程添加提示文件已存在冲突
- classpath和classpath*区别
- RESTful架构详解
- Ubuntu系统命令熟悉