nguyentruong98

Untitled

Dec 2nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 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 printfnode(Node *p)
  29. {
  30.       if (p == NULL)
  31.             return;
  32.       printf("%s     %s      %s\n", p->Name, p->Phone, p->Email);
  33. }
  34. Queue *makeQueue()
  35. {
  36.       Queue *q;
  37.       q = (Queue *)malloc(sizeof(Queue));
  38.       q->head = NULL;
  39.       return q;
  40. }
  41. void pop(Queue *q)
  42. {
  43.       Node *tmp;
  44.       if (q->head == NULL)
  45.       {
  46.             printf("ERROR QUEUE\n");
  47.             return;
  48.       }
  49.       tmp = q->head;
  50.       q->head = q->head->next;
  51.       free(tmp);
  52. }
  53. void push(Queue *q, Node *newNode)
  54. {
  55.       Node *iterator;
  56.       if (q->head == NULL)
  57.       {
  58.             q->head = newNode;
  59.             return;
  60.       }
  61.       iterator = q->head;
  62.       while (iterator->next != NULL)
  63.       {
  64.             iterator = iterator->next;
  65.       }
  66.       iterator->next = newNode;
  67.       return;
  68. }
  69. int main()
  70. {
  71.       Queue *q = makeQueue();
  72.       FILE *f;
  73.       f = fopen("phonebook.txt", "r");
  74.       if (f == NULL)
  75.       {
  76.             printf("FILE NOT FOUND");
  77.       }
  78.       else
  79.       {
  80.             char s1[80], s2[80], s3[80];
  81.             do
  82.             {
  83.                   fscanf(f, "%s %s %s", s1, s2, s3);
  84.                   Node *newNode = makeNode(s1, s2, s3);
  85.                   push(q, newNode);
  86.             } while (feof(f));
  87.             Node *node = front(q);
  88.             pop(q);
  89.             while (node != NULL)
  90.             {
  91.                   node = front(q);
  92.                   printfnode(node);
  93.                   if (node != NULL)
  94.                         pop(q);
  95.             }
  96.       }
  97.       return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment