C語言實現鏈表的建立、計算鏈表長度及兩個鏈表的合并__C語言

來源:互聯網
上載者:User
問題描述:從鍵盤輸入兩個系列,構成兩個單鏈表
 
(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[N]=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;}
程式運行結果:
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.