Advertisement
anambrito

92.c

Jun 7th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #define tam 5
  5.  
  6. typedef struct inteiro {
  7. int a;
  8. struct inteiro *next;
  9. }num;
  10.  
  11. void gerador (int *v) {
  12.    
  13.  int i=0;
  14.  
  15.     while(i++!= tam)
  16.     v[i]= rand()%10;
  17.  
  18. }
  19.  
  20. num* constroi (int n, int *v) {
  21.  
  22.  num *inicio;
  23.  num *p, *j;
  24.  int i =0;     
  25.  
  26.     inicio = (num*)malloc(sizeof(num));
  27.  
  28.     p = inicio;
  29.  
  30.         if(n==0)
  31.             return NULL;
  32.  
  33.  
  34.         while(i++ != n) {
  35.         p->a = v[i];
  36.         p->next = (num*)malloc(sizeof(num));
  37.         j = p;
  38.         p = p->next;
  39.         }
  40.  
  41.         p->next= NULL ;
  42.         p=j;
  43.         p->next = NULL;
  44.        
  45.        
  46.         p = inicio;
  47.  
  48.    
  49.     return (num*)p;
  50.  
  51. }
  52.  
  53. main() {
  54.  
  55.  srand(time(NULL));
  56.  
  57. int i=0, v[tam], n = tam;
  58.  num *p;
  59.  
  60.     gerador(v);
  61.     printf("Elementos do vetor[5]:");
  62.     while(i++ != tam){
  63.     printf(" %d", v[i]);
  64.     }
  65.     printf("\n");
  66.  
  67.     p = constroi(n,v);
  68.    
  69.     printf("\nVetor em lista:\n");
  70.  
  71.     while (p != NULL) {
  72.     printf(" a: %d\n", p->a);
  73.     p = p->next;
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement