Advertisement
Guest User

Untitled

a guest
Feb 26th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct listNode
  5.     {
  6.         int data;
  7.         struct listNode *next;
  8.     };
  9.  
  10. int main()
  11. {
  12.  
  13.     int i; int size = 10;
  14.     struct listNode *temp,*list1;
  15.  
  16.     list1 = malloc(sizeof(*list1));
  17.     temp = malloc(sizeof(*temp));
  18.  
  19.     temp = list1;
  20.     srand(time(NULL));
  21.  
  22.     for(i = 1;i<=size;i++)          /* Generate list with 10 elements */
  23.     {
  24.         if (i==size){
  25.             temp->data = rand() % 100;
  26.             temp->next = NULL;
  27.         }
  28.         else{
  29.             temp->data = rand() % 100;
  30.             temp->next = malloc(sizeof(*temp));
  31.             temp = temp->next;
  32.         }
  33.  
  34.     }
  35.  
  36.     temp = list1;
  37.     i = 1;
  38.  
  39.     printf("List 1:\n\n");
  40.  
  41.     while(temp!=NULL)               /* Print the 10 elements of the list */
  42.     {
  43.         printf("Element %d of list: %d\n",i,temp->data);
  44.         i++;
  45.         temp = temp->next;
  46.     }
  47.  
  48.     int max;                        /* Finding the max in the list */
  49.     max = list1->data;
  50.     temp = list1->next;
  51.  
  52.     for(i = 2;i <= size;i++)
  53.     {
  54.         if(temp->data>max){
  55.             max = temp->data;
  56.         }
  57.         temp = temp->next;
  58.     }
  59.  
  60.     printf("\n");
  61.     printf("The max value of the list is %d\n",max);
  62.     printf("\n");
  63.  
  64.     struct listNode *list2;
  65.  
  66.     list2 = malloc(sizeof(*list2));
  67.     temp = list2;
  68.  
  69.     int size2 = 5;
  70.  
  71.     for(i = 1;i<=size2;i++)          /* Generate second list with 5 elements */
  72.     {
  73.         if (i==size2){
  74.             temp->data = rand() % 100;
  75.             temp->next = NULL;
  76.         }
  77.         else{
  78.             temp->data = rand() % 100;
  79.             temp->next = malloc(sizeof(*temp));
  80.             temp = temp->next;
  81.         }
  82.  
  83.     }
  84.  
  85.     struct listNode *list3;
  86.  
  87.     list3 = list1;
  88.     temp = list3;
  89.  
  90.     while(temp->next!=NULL)           /* Merge list1 and list2 into list3*/
  91.     {
  92.         temp=temp->next;
  93.     }
  94.  
  95.     temp->next = list2;
  96.  
  97.     temp = list3;
  98.     i = 1;
  99.  
  100.     printf("List 3:\n\n");
  101.  
  102.     while(temp!=NULL)               /* Print the combined lists */
  103.     {
  104.         printf("Element %d of list: %d\n",i,temp->data);
  105.         i++;
  106.         temp = temp->next;
  107.     }
  108.  
  109.     printf("\n");
  110.  
  111.     system("PAUSE");
  112.     return 0;
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement