Advertisement
Guest User

dynamic

a guest
Oct 17th, 2013
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. typedef struct node node_t;
  6. struct node{
  7.     int *n;
  8.     node_t* next;
  9. };
  10. node_t* head;
  11.  
  12. int read_one_vote(int no_of_candidates, int one_vote[]);
  13. int main(int argc, char* argv[]){
  14.     head= NULL;
  15.     int no_of_candidates = 5;
  16.     int i;
  17.     int *one_vote_array;
  18.     one_vote_array = (int*)malloc(no_of_candidates*sizeof(int));
  19.     for(i=0; read_one_vote(no_of_candidates,one_vote_array); i++){
  20.         node_t* vote_node = (node_t*)malloc(sizeof(node_t));
  21.         vote_node->n = one_vote_array;
  22.         vote_node->next = head;
  23.         head=vote_node;
  24. /*Here I just made a linked list with each structure holding an array of no_of_candidates numbers*/
  25. }
  26.     /*When I try to print this*/
  27.     node_t *temp = head;
  28.     while(temp!=NULL){
  29.     for(i=0; i<no_of_candidates;i++){
  30.         printf("%d",temp->n[i]);
  31.     }
  32.     printf("%d",temp->next != NULL);
  33.     temp=temp->next;
  34. /*It only prints the last 5 numbers i inputted in the console*/
  35. }
  36.     return 0;
  37. }
  38.  
  39.  
  40. /*This function just reads 5 numbers in at once and puts then in the array one_vote, it returns 0 if no numbers are read, one if 5 numbers are read*/
  41.  
  42. int read_one_vote(int no_of_candidates, int one_vote[]){
  43.     int x,i =0;
  44.     if(scanf("%d",&x) == 0){
  45.         return 0;
  46.     }
  47.     else{
  48.         one_vote[i] = x;
  49.         for(i=1; i<no_of_candidates; i++){
  50.             scanf("%d",&x);
  51.             one_vote[i] = x;
  52.         }
  53.  
  54.     }
  55.     return 1;
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement