nguyentruong98

Untitled

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