Advertisement
Guest User

Untitled

a guest
May 7th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. #define MAX 20
  8.  
  9. typedef struct l
  10. {
  11.     int i;
  12.     struct l * next;
  13. } LIST;
  14.  
  15. void fill(int * a);
  16. void lcopy(int * a, LIST * head, LIST * tail);
  17. void list(int * a, char * c);
  18.  
  19. void main(int argc, char * argv[])
  20. {
  21.     srand(time(NULL));
  22.  
  23.     int a[255];
  24.     int b[255];
  25.     int s;
  26.  
  27.     s = sizeof(LIST);
  28.  
  29.     LIST * l;
  30.     LIST * head = NULL;
  31.     LIST * tail = NULL;
  32.  
  33.     // Fill array with random numbers. 
  34.     fill(a);
  35.  
  36.     // Fill array with random numbers.
  37.     fill(b);
  38.  
  39.     // Copy array to linked list.
  40.     lcopy(a, head, tail);
  41.  
  42.     l = head;
  43.    
  44.     if(l == NULL)
  45.     {
  46.         printf("Empty\n"); 
  47.     }
  48.     else
  49.     {
  50.         while(l != NULL)
  51.         {
  52.             printf("%d\n", l->i);
  53.             l=l->next;
  54.         }
  55.     }
  56. }
  57.  
  58. void fill(int * a)
  59. {
  60.     int i = 0;
  61.  
  62.     for(i=0; i<MAX; i++)
  63.     {
  64.         a[i] = rand() % 10000;
  65.     }
  66. }
  67.  
  68. void lcopy(int * a, LIST * head, LIST * tail)
  69. {
  70.     int i = 0;
  71.     LIST * l;
  72.  
  73.     for(i=0; i<MAX; i++)
  74.     {
  75.         l = malloc(sizeof(LIST));
  76.         l->i = a[i];
  77.  
  78.         if(head == NULL)
  79.         {
  80.             head = l;
  81.             tail = l;
  82.         }
  83.         else
  84.         {
  85.             tail->next = l;
  86.             tail = l;
  87.         }
  88.  
  89.         printf("%d\n", l->i);
  90.         l->next = NULL;
  91.     }
  92. }
  93.  
  94. void list(int * a, char * c)
  95. {
  96.     int i = 0;
  97.  
  98.     for(i=0; i<strlen(c); i++)
  99.     {
  100.         printf("%c", c[i]);
  101.     }
  102.  
  103.     for(i=0; i<MAX; i++)
  104.     {
  105.         printf("%d\n", a[i]);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement