nguyentruong98

Untitled

Dec 2nd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct Node
  5. {
  6.       char Name[80], Phone[80], Email[80];
  7.       struct Node *next;
  8. } Node;
  9. typedef struct Queue
  10. {
  11.       struct Node *head;
  12. } Queue;
  13. Node *front(Queue* q)
  14. {
  15.       if (q->head == NULL)
  16.             return NULL;
  17.       return q->head;
  18. }
  19. Node *makeNode(char *s1, char *s2, char *s3)
  20. {
  21.       Node *p = (Node *)malloc(sizeof(Node));
  22.       strcpy(p->Name, s1);
  23.       strcpy(p->Phone, s2);
  24.       strcpy(p->Email, s3);
  25.       p->next = NULL;
  26.       return p;
  27. }
  28. void printfll(Node* p)
  29. {
  30.       if(p == NULL)
  31.       return;
  32.             printf("%s     %s      %s\n", p->Name, p->Phone, p->Email);
  33.             printfll(p->next);
  34. }
  35. Queue *makeQueue()
  36. {
  37.       Queue *q;
  38.       q = (Queue *)malloc(sizeof(Queue));
  39.       q->head = NULL;
  40.       return q;
  41. }
  42. void pop(Queue *q)
  43. {
  44.       Node *tmp;
  45.       if (q->head == NULL)
  46.       {
  47.             printf("ERROR QUEUE\n");
  48.             return;
  49.       }
  50.       tmp = q->head;
  51.       q->head = q->head->next;
  52.       free(tmp);
  53. }
  54. void push(Queue *q, Node *newNode)
  55. {
  56.       Node *iterator;
  57.       if (q->head == NULL)
  58.       {
  59.             q->head = newNode;
  60.             return;
  61.       }
  62.       iterator = q->head;
  63.       while (iterator->next != NULL)
  64.       {
  65.             iterator = iterator->next;
  66.       }
  67.       iterator->next = newNode;
  68.       return;
  69. }
  70. int main()
  71. {
  72.       Queue *q = makeQueue();
  73.       FILE *f;
  74.       f = fopen("phonebook.txt", "r");
  75.       if (f == NULL)
  76.       {
  77.             printf("FILE NOT FOUND");
  78.       }
  79.       else
  80.       {
  81.             char s1[80], s2[80], s3[80];
  82.             do
  83.             {
  84.                   fscanf(f, "%s %s %s", s1, s2, s3);
  85.                   Node *newNode = makeNode(s1, s2, s3);
  86.                   push(q, newNode);
  87.             } while (feof(f));
  88.                 Node *node = front(q);
  89.             pop(q);
  90.             while (node != NULL)
  91.             {
  92.                   node = front(q);
  93.                   printfll(node);
  94.                   if(node!=NULL)
  95.                   pop(q);
  96.             }
  97.       }
  98.       return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment