您的位置:首页 > 编程语言 > C语言/C++

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;
}
程序运行结果:

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐