problem Description: Input two series from keyboard, form two single linked list
(1). Calculates the length of two single linked lists (2). Output the maximum, minimum, and average (3) of a long list. Counts two of the same elements in the list (4). Merge two linked lists and output
Program code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct MyList
{
int data;
struct MyList *next;
/*......................................
The tail inserts the method to create the chain list ... .... * * * 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 ("Enter the value of the%d node (enter 0 end): \ 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 ("Enter the value of the%d node (enter 0 end): \ n", i+1);
scanf ("%d", &x);
s->next=null;
S=1;
printf ("\ n");
return (h); }
/*...................................
Find the list of the table long ... ..... * * * int getlength (struct mylist *head) {int i=0;;;.. * * * * * * int.) (+).
struct MyList *p;
if (Head->next==null) return 0;
else {p=head->next;
while (p) {i++;
p=p->next;
return i; }
}
/*.....................
To find the maximum, minimum, and average value of all elements in a list ... * * * 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 ("Linked list is empty list!\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 ("The maximum, minimum and average value of the list is:%d,%d,%.3f\n", max,min,average);
printf ("\ n"); }
/*.........................
Count the number of the same elements in two linked lists .... * * * * 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;
The number of the same elements in two linked lists is:%d \ n, count.
printf ("\ n"); }
/*.................................. Merge and export two linked lists ........... ......... */void Addlink (struct mylist *head4,struct mylist *head5) {struct mylist;;;;;
PR=HEAD4;
while (Head4->next!=null) head4=head4->next;
head4->next=head5->next;
printf ("Two merged list: \ n");
while (Pr->next!=null) {printf ("%d", pr->next->data);
pr=pr->next;
printf ("\ n \ nthe"); }
/*.....................................
The main program ....... */int main (void) {int length1;;..... * * * (=) (=)--------.
int length2;
struct MyList *my1;
struct MyList *my2;
printf ("Enter first sequence: \ n");
My1=createlist ();
printf ("Enter second sequence: \ n");
My2=createlist ();
Length1=getlength (MY1);
Length2=getlength (MY2);
printf ("First linked list and second linked list are%d and%d\n in length, respectively", length1,length2);
printf ("\ n");
if (length1>=length2) {printf ("the maximum, minimum, and average value of the first linked list: \ n");
Getmna (MY1);
else {printf ("the maximum, minimum, and average value of the second list: \ n");
Getmna (MY2);
printf ("Now compares the number of identical elements in two linked lists: \ n");
Stalink (MY1,MY2);
printf ("Two linked lists are merged: \ n");
Addlink (MY1,MY2);
return 0; }
Program Run Result: