单链表

来源:互联网 发布:淘宝假冒品牌退款处理 编辑:程序博客网 时间:2024/06/18 07:26

好烦啊,这几天做题,碰见一道就不会,就算会的,也得给我wa几遍,让我慢悠悠地在那儿debug。单链表是数据结构课的内容,之前一直没上课,现在发现学很多东西要用到书上的东西,所以开始学了。

写得不怎么规范,自娱自乐罢了。

创建并输出一个单链表:

#include<stdio.h>#include<stdlib.h>struct node{int data;node* next;};node* creatlist()//创建链表{node *h,*s,*r;s=(node *)malloc(sizeof(node));h=s;scanf("%d",&s->data);while(s->data!=-1){r=s;s=(node *)malloc(sizeof(node));scanf("%d",&s->data);r->next=s;}r->next=NULL;return h;}int main(){node *h;h=creatlist();while(h!=NULL)//输出链表{printf("%d ",h->data);h=h->next;}printf("\n");return 0;}

取出链表中的元素:

#include<stdio.h>#include<stdlib.h>struct node{int data;node *next;};node* creatlist(){node *h,*s,*r;s=(node *)malloc(sizeof(node));scanf("%d",&s->data);h=s;while(s->data!=-1){r=s;s=(node *)malloc(sizeof(node));scanf("%d",&s->data);r->next=s;}r->next=NULL;return h;}void pri(node* a,int n){int count;count=1;while(a!=NULL){if(count==n){printf("%d\n",a->data);break;}count++;a=a->next;}return ;}int main(){node *a;int n;a=creatlist();while(scanf("%d",&n)!=EOF)pri(a,n);return 0;}

删除链表中的结点:

#include<stdio.h>#include<stdlib.h>struct node{int data;node *next;};node* creatlist(){node *h,*s,*r;s=(node *)malloc(sizeof(node));h=s;scanf("%d",&s->data);while(s->data!=-1){r=s;s=(node *)malloc(sizeof(node));scanf("%d",&s->data);r->next=s;}r->next=NULL;return h;}node* delenode(node *a,int n){int count;node *s,*h;count=1;h=a;if(count==n){h=a->next;return h;}while(a!=NULL){count++;if(count==n){s=a->next;a->next=s->next;break;}a=a->next;}return h;}void pri(node *a){while(a!=NULL){printf("%d ",a->data);a=a->next;}printf("\n");return ;}int main(){node *a;int n;a=creatlist();pri(a);while(scanf("%d",&n)!=EOF){a=delenode(a,n);pri(a);}return 0;}

在链表中插入结点:

#include<stdio.h>#include<stdlib.h>struct node{int data;node *next;};node* creatlist()//创建链表{node *h,*s,*r;s=(node *)malloc(sizeof(node));h=s;scanf("%d",&s->data);while(s->data!=-1){r=s;s=(node *)malloc(sizeof(node));scanf("%d",&s->data);r->next=s;}r->next=NULL;return h;}node *insenode(node *a,int x,int n){int count,flag;node *s,*h;h=a;count=1;if(n==1){s=(node *)malloc(sizeof(node));s->data=x;s->next=a;h=s;return h;}flag=0;while(a!=NULL){count++;if(count==n){s=(node *)malloc(sizeof(node));s->data=x;s->next=a->next;a->next=s;flag=1;break;}a=a->next;}if(flag)return h;else{s=(node *)malloc(sizeof(node));a->next=s;s->next=NULL;s->data=x;return h;}}void pri(node *a)//输出链表{while(a!=NULL){printf("%d ",a->data);a=a->next;}printf("\n");return ;}int main(){node *a;int x,n;a=creatlist();pri(a);while(scanf("%d%d",&x,&n)!=EOF){a=insenode(a,x,n);pri(a);}return 0;}

逆置一个链表

#include<stdio.h>#include<stdlib.h>struct node{int data;node *next;};node* creatlist(){node *h,*s,*e;s=(node *)malloc(sizeof(node));scanf("%d",&s->data);h=s;while(s->data!=-1){e=s;s=(node *)malloc(sizeof(node));scanf("%d",&s->data);e->next=s;}e->next=NULL;return h;}node *reverselist(node *a){node *s,*t;s=a->next;a->next=NULL;while(s->next!=NULL){t=s->next;s->next=a;a=s;s=t;}s->next=a;a=s;return a;}void pri(node *a){while(a->next!=NULL){printf("%d ",a->data);a=a->next;}printf("%d",a->data);printf("\n");}int main(){node *a;a=creatlist();pri(a);a=reverselist(a);pri(a);return 0;}

学习状态很不好,但是也想不来该怎么调整。

原创粉丝点击