Advertisement
Guest User

Untitled

a guest
May 7th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 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 i = 0; 
  26.     int s;
  27.  
  28.     s = sizeof(LIST);
  29.  
  30.     LIST * l;
  31.     LIST * head = NULL;
  32.     LIST * tail = NULL;
  33.  
  34.     // Fill array with random numbers. 
  35.     fill(a);
  36.  
  37.     // Fill array with random numbers.
  38.     fill(b);
  39.  
  40.     // Copy array to linked list.
  41.     lcopy(a, &head, &tail);
  42.  
  43.     for(i=0;i<MAX;i++)
  44.     {
  45.         printf("%d\t%d\n", a[i], b[i]);
  46.     }
  47.  
  48.     /*l = head;
  49.    
  50.     if(l == NULL)
  51.     {
  52.         printf("Empty\n"); 
  53.     }
  54.     else
  55.     {
  56.         while(l != NULL)
  57.         {
  58.             printf("%d\n", l->i);
  59.             l=l->next;
  60.         }
  61.     }*/
  62. }
  63.  
  64. void fill(int * a)
  65. {
  66.     int i = 0;
  67.  
  68.     for(i=0; i<MAX; i++)
  69.     {
  70.         a[i] = rand() % 10000;
  71.     }
  72. }
  73.  
  74. void lcopy(int * a, LIST ** head, LIST ** tail)
  75. {
  76.     int i = 0;
  77.     LIST * l;
  78.  
  79.     for(i=0; i<MAX; i++)
  80.     {
  81.         l = malloc(sizeof(LIST));
  82.         l->i = a[i];
  83.  
  84.         if(head == NULL)
  85.         {
  86.             *head = l;
  87.             *tail = l;
  88.         }
  89.         else
  90.         {
  91.             *tail->next = l;
  92.             *tail = l;
  93.         }
  94.  
  95.         l->next = NULL;
  96.     }
  97. }
  98.  
  99. void list(int * a, char * c)
  100. {
  101.     int i = 0;
  102.  
  103.     for(i=0; i<strlen(c); i++)
  104.     {
  105.         printf("%c", c[i]);
  106.     }
  107.  
  108.     for(i=0; i<MAX; i++)
  109.     {
  110.         printf("%d\n", a[i]);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement