C语言实现链表的创建、计算链表长度及两个链表的合并
2017-12-10 13:39
519 查看
问题描述:从键盘输入两个系列,构成两个单链表
(1).计算两个单链表的长度
(2).输出较长链表的最大值、最小值和平均值
(3).统计两个链表中相同元素的个数
(4).将两个链表合并后输出
程序代码:
#include<stdio.h> #include<stdlib.h> #include<conio.h>
struct mylist { int data; struct mylist *next; };
/*......................................尾插法创建链表......................................*/ struct mylist *createlist(void) { int i=0; int x; struct mylist *s,*q,*h; h=(struct mylist *)malloc(sizeof(struct mylist)); h->next=NULL; q=h; printf("输入第%d个节点的值(输入0结束):\n",i+1); scanf("%d",&x); while(x!=0) { i=i+1; s=(struct mylist *)malloc(sizeof(struct mylist)); s->data=x; q->next=s; printf("输入第%d个节点的值(输入0结束):\n",i+1); scanf("%d",&x); s->next=NULL; q=s; } printf("\n"); return(h); } /*...................................求链表表长......................................*/ int Getlength(struct mylist *head) { int i=0; struct mylist *p; if(head->next==NULL) return 0; else { p=head->next; while(p) { i++; p=p->next; } return i; } } /*.....................求链表中所有元素的最大值、最小值和平均值....................*/ void GetMNA(struct mylist *head1) { int max,min; float average; int sum=0,N=0; int i; int a[100]; if(head1->next==NULL) { printf("链表为空链表!\n"); exit(0); } while(head1->next!=NULL) { a =head1->next->data; N++; head1=head1->next; } max=a[0]; min=a[0]; sum=a[0]; for(i=1;i<N;i++) { sum=sum+a[i]; if(a[i]>max) max=a[i]; if(a[i]<min) min=a[i]; } average=sum/(N*1.0); printf("该链表的最大值、最小值和平均值分别为:%d,%d,%.3f\n",max,min,average); printf("\n"); } /*.........................统计两个链表中相同元素的个数......................*/ void StaLink(struct mylist *head2,struct mylist *head3) { int count=0,i=0,j=0; int M,N; int arr1[100]; int arr2[100]; while(head2->next!=NULL) { arr1[i]=head2->next->data; i++; head2=head2->next; } M=i; while(head3->next!=NULL) { arr2[j]=head3->next->data; j++; head3=head3->next; } N=j; for(i=0;i<M;i++) { for(j=0;j<N;j++) { if(arr1[i]==arr2[j]) count=count+1; } } printf("两个链表中相同的元素个数为:%d \n",count); printf("\n"); } /*..................................将两个链表合并并输出.....................................*/ void ADDLink(struct mylist *head4,struct mylist *head5) { struct mylist *pr; pr=head4; while(head4->next!=NULL) head4=head4->next; head4->next=head5->next; printf("两个合并之后的链表为:\n"); while(pr->next!=NULL) { printf("%d ",pr->next->data); pr=pr->next; } printf("\n\n"); } /*.....................................主程序...............................................*/ int main(void) { int length1; int length2; struct mylist *my1; struct mylist *my2; printf("输入第一个序列:\n"); my1=createlist(); printf("输入第二个序列:\n"); my2=createlist(); length1=Getlength(my1); length2=Getlength(my2); printf("第一个链表和第二个链表的长度分别为%d和%d\n",length1,length2); printf("\n"); if(length1>=length2) { printf("现求第一个链表的最大值、最小值和平均值:\n"); GetMNA(my1); } else { printf("现求第二个链表的最大值、最小值和平均值:\n"); GetMNA(my2); } printf("现比较两个链表中相同元素的个数:\n"); StaLink(my1,my2); printf("现将两个链表合并:\n"); ADDLink(my1,my2); return 0; }
程序运行结果:
相关文章推荐
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- PAT 2-11 两个有序链表序列的合并(C语言实现)
- 合并两个单链表,其中包括尾插法建表、计算单链表长度、输出单链表,给出完整代码
- 将两个非递减的有序链表合并为一个非递增的有序链表(C语言编程实现)
- C/C++,数据结构实现两个链表的合并(尾插法建立单链表,求链表长度,直接插入排序)
- 将两个递增的有序链表合并为一个递增的有序链表(C语言编程实现)
- 将两个递增的有序链表合并为一个递增的有序链表(C语言编程实现)
- C/C++,数据结构实现两个链表的合并(尾插法建立单链表,求链表长度,直接插入排序)
- 将两个非递减的有序链表合并为一个非递增的有序链表(C语言编程实现)
- 两个有序链表合并递归实现及非递归实现
- K个有序链表共N个结点在O(NlgK)时间合并为一个新的有序链表实现文件C语言
- C++实现两个已经排序的链表进行合并
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- 链表的创建、测长、排序、插入、逆序的实现(C语言)
- C语言实现链表的创建,初始化,插入,删除,查找
- 逆转交替合并两个链表的解析与实现
- 【数据结构】C语言实现链表的创建及遍历链表
- python实现合并两个排序链表
- C++之实现两个链表合并(迭代和递归版本)(19)---《那些奇怪的算法》
- 合并两个排序的链表(Java实现)