单链表的操作
来源:互联网 发布:淘宝玩具店 编辑:程序博客网 时间:2024/06/10 06:58
顺序建立单链表
<pre name="code" class="cpp">#include<stdio.h>#include<stdlib.h>struct node{ int data; struct node *next;};struct node *creat(int n){ struct node *head,*tail,*p; head=(struct node *)malloc(sizeof(struct node )); head->next=NULL; tail=head; while(n--) { p=(struct node *)malloc(sizeof(struct node )); scanf("%d",&p->data); p->next=NULL; tail->next=p; tail=p; } return head;}int main(){ int n; scanf("%d",&n); struct node *head=creat(n),*p; p=head->next; while(p!=NULL) { if(p->next!=NULL) printf("%d ",p->data); else printf("%d\n",p->data); p=p->next; } return 0;}
逆序建立单链表
<pre name="code" class="cpp">#include<stdio.h>#include<stdlib.h>struct node{ int data; struct node *next;};struct node *creat(int n){ struct node *head,*p; head=(struct node *)malloc(sizeof(struct node )); head->next=NULL; while(n--) { p=(struct node *)malloc(sizeof(struct node )); scanf("%d",&p->data); p->next=head->next; head->next=p; } return head;}int main(){ int n; scanf("%d",&n); struct node *head=creat(n),*p; p=head->next; while(p!=NULL) { if(p->next!=NULL) printf("%d ",p->data); else printf("%d\n",p->data); p=p->next; } return 0;}
链表的逆置
<pre name="code" class="cpp">#include <stdio.h>#include <stdlib.h>struct node{ int data; struct node *next;};struct node *creat(){ struct node *head,*tail,*p; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; tail=head; int i; scanf("%d",&i); while(i!=-1) { p=(struct node *)malloc(sizeof(struct node)); p->data=i; p->next=NULL; tail->next=p; tail=p; scanf("%d",&i); } return head;}void reser(struct node *head){ struct node *p,*q; p=head->next; head->next=NULL; q=p->next; while(p!=NULL) { p->next=head->next; head->next=p; p=q; if(q!=NULL) q=q->next; } p=head->next; while(p!=NULL) { if(p->next!=NULL) printf("%d ",p->data); else printf("%d\n",p->data); p=p->next; }}int main(){ int n; struct node *head=creat(); reser(head); return 0;}
有序链表的归并
<pre name="code" class="cpp">#include<stdio.h>#include<stdlib.h>struct node{ int data; struct node *next;};struct node *creat(int n){ struct node *head,*tail,*p; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; tail=head; while(n--) { p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); p->next=NULL; tail->next=p; tail=p; } return head;}void merg(struct node *head1,struct node *head2){ struct node *tail,*p,*q; p=head1->next; q=head2->next; tail=head1; head1->next=NULL; free(head2); while(p&&q) { if(p->data<q->data) { tail->next=p; tail=p; p=p->next; } else { tail->next=q; tail=q; q=q->next; } } if(p) { tail->next=p; } else if(q) { tail->next=q; } p=head1->next; while(p!=NULL) { if(p->next!=NULL) printf("%d ",p->data); else printf("%d\n",p->data); p=p->next; }}int main(){ int n,m; scanf("%d%d",&m,&n); struct node *head1=creat(m),*head2=creat(n); merg(head1,head2); return 0;}
单链表的拆分
<pre name="code" class="cpp">#include <stdio.h>#include <stdlib.h>struct node{ int data; struct node *next;};struct node *creat(int n){ struct node *head,*tail,*p; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; tail=head; while(n--) { p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); p->next=NULL; tail->next=p; tail=p; } return head;}void split(struct node *head){ struct node *head1,*head2,*tail1,*tail2,*p,*q; head1=(struct node *)malloc(sizeof(struct node)); head1->next=NULL; tail1=head1; head2=(struct node *)malloc(sizeof(struct node)); head2->next=NULL; tail2=head2; p=head->next; q=p->next; int cont1=0,cont2=0; while(p) { if(p->data%2==0) { tail1->next=p; tail1=p; p->next=NULL; p=q; if(q) q=q->next; cont1++; } else { tail2->next=p; tail2=p; p->next=NULL; p=q; if(q) q=q->next; cont2++; } } printf("%d %d\n",cont1,cont2); p=head1->next; while(p!=NULL) { if(p->next!=NULL) printf("%d ",p->data); else printf("%d\n",p->data); p=p->next; } q=head2->next; while(q!=NULL) { if(q->next!=NULL) printf("%d ",q->data); else printf("%d\n",q->data); q=q->next; }}int main(){ int n; scanf("%d",&n); struct node *head=creat(n); split(head); return 0;}
有序链表的建立--插入排序法
<pre name="code" class="cpp">#include <stdio.h>#include<stdlib.h>struct node{ int data; struct node *next;};int main(){ int n; scanf("%d",&n); struct node *head,*p,*r,*q; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; while(n--) { p=head; q=p->next; r=(struct node *)malloc(sizeof(struct node)); scanf("%d",&r->data); while(q) { if(r->data<q->data) { p->next=r; r->next=q; break; } p=q; q=q->next; } if(q==NULL) { p->next=r; r->next=NULL; } } p=head->next; while(p!=NULL) { if(p->next!=NULL) printf("%d ",p->data); else printf("%d\n",p->data); p=p->next; } return 0;}
插排--整理音乐
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="cpp">#include<stdio.h>#include<stdlib.h>struct node{ int data; char name[100]; struct node *next;};int main(){ int n,m; scanf("%d",&n); struct node *head,*p,*r,*q; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; while(n--) { scanf("%d",&m); while(m--) { p=head; q=p->next; r=(struct node *)malloc(sizeof(struct node)); scanf("%s%d",r->name,&r->data); while(q) { if(r->data>q->data) { p->next=r; r->next=q; break; } p=q; q=q->next; } if(q==NULL) { p->next=r; r->next=NULL; } } } p=head->next; while(p!=NULL) { if(p->next!=NULL) printf("%s ",p->name); else printf("%s\n",p->name); p=p->next; } return 0;}
循环链表--约瑟夫问题
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="cpp">#include<stdio.h>#include<stdlib.h>struct node{ int data; struct node *next;};struct node *creat(int n){ struct node *head,*tail,*p; head=(struct node *)malloc(sizeof(struct node)); head->data=1; head->next=NULL; tail=head; int i; for(i=2;i<=n;i++) { p=(struct node *)malloc(sizeof(struct node)); p->data=i; p->next=NULL; tail->next=p; tail=p; } tail->next=head; return head;}void fin(struct node *head,int n,int m){ struct node *p,*q; int num=1,cont=0; p=head; while(cont<n-1) { q=p->next; num++; if(num%m==0) { p->next=q->next; free(q); cont++; } else p=q; } printf("%d\n",p->data);}int main(){ int n,m; scanf("%d%d",&n,&m); struct node *head=creat(n); fin(head,n,m); return 0;}
循环链表--不敢死队问题
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre class="sh-cpp sh-sourceCode" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding: 5px 5px 5px 7px; overflow: auto; font-size: 16px; line-height: 17.600000381469727px; font-family: 'Courier New', Courier, monospace;"><pre name="code" class="cpp">#include<stdio.h>#include<stdlib.h>struct node{ int data; struct node *next;};struct node *creat(int n){ struct node *head,*tail,*p; head=(struct node *)malloc(sizeof(struct node )); head->data=1; head->next=NULL; tail=head; int i; for(i=2;i<=n;i++) { p=(struct node *)malloc(sizeof(struct node )); p->data=i; p->next=NULL; tail->next=p; tail=p; } tail->next=head; return head;}void sel(struct node *head,int n){ struct node *p,*q; int num=1,cont=0; p=head; while(cont<=n-1) { q=p->next; num++; if(num%5==0) { p->next=q->next; cont++; if(q->data==1) break; free(q); } else p=q; } printf("%d\n",cont);}int main(){ int n,m; while(scanf("%d",&m),m) { struct node *head=creat(m); sel(head,m); } return 0;}
单链表节点的删除
#include <stdio.h>#include <stdlib.h>#include <string.h>struct node{ int data; struct node *next;}*head;//顺序建立单链表struct node *creat(int n){ struct node *tail,*p; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; tail=head; while(n--) { p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); p->next=NULL; tail->next=p; tail=p; } return head;}//删除操作void del(struct node *head,int n){ struct node *p,*q,*r; int cont=0; p=head->next; q=p; while(p!=NULL) { while(q->next!=NULL) { if(p->data==q->next->data) { r=q->next; q->next=r->next; free(r); cont++; } else q=q->next; } p=p->next; q=p; } printf("%d\n",n-cont); p=head->next; while(p!=NULL) { if(p->next!=NULL) printf("%d ",p->data); else printf("%d\n",p->data); p=p->next; }}int main(){ int n; scanf("%d",&n); struct node *head=creat(n); del(head,n); return 0;}
双向链表
#include<stdio.h>#include<stdlib.h>struct node{ int data; struct node *next,*last;};struct node *creat(int n){ struct node *head,*p,*tail; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; head->last=NULL; tail=head; while(n--) { p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); p->next=NULL; p->last=tail; tail->next=p; tail=p; } return head;}void sear(struct node *head,int key){ struct node *p; p=head->next; while(p!=NULL) { if(p->data==key) { if(p->last!=head&&p->next!=NULL) { printf("%d %d\n",p->last->data,p->next->data); break; } else if(p->last!=head&&p->next==NULL) { printf("%d\n",p->last->data); break; } else { printf("%d\n",p->next->data); } } p=p->next; }}int main(){ int n,m; scanf("%d%d",&n,&m); struct node *head=creat(n); while(m--) { int key; scanf("%d",&key); sear(head,key); } return 0;}
0 0
- 简单的单链表操作
- 单链表的基本操作
- 简单的单链表操作
- 单链表的基本操作
- 单链表的操作(C#)
- 单链表的操作
- 单链表的各种操作
- 单链表的操作实现
- 单链表的 操作
- 单链表的基本操作
- 单链表的一些操作
- 单链表的操作
- 关于单链表的操作
- 单链表的各种操作
- 单链表的常用操作
- 单链表的一些操作
- 单链表的一些操作
- 单链表的操作
- OpenCV基础篇之绘图及RNG随机数对象
- 编程中的数值与对应符号的处理
- shell判断hdfs文件目录是否存在
- Matlab GUI编程
- Ubuntu下vim的安装与基本用法
- 单链表的操作
- unity学习之委托与事件的初步应用
- IOS开发基础
- PowerDesigner建模时name和code互换
- 防止UI线程阻塞的方法
- 挫折1
- OpenCV基础篇之像素访问
- select 监听
- 安装交叉编译工具链arm-linux-gcc-3.4.5-glibc-2.3.6