nguyentruong98

Untitled

Dec 3rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 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 Stack
  10. {
  11.       struct Node *head;
  12.       struct Node *tail;
  13. } Stack;
  14. Node *makeNode(char *s1, char *s2, char *s3)
  15. {
  16.       Node *p = (Node *)malloc(sizeof(Node));
  17.       strcpy(p->Name, s1);
  18.       strcpy(p->Phone, s2);
  19.       strcpy(p->Email, s3);
  20.       p->next = NULL;
  21.       return p;
  22. }
  23. void printfnode(Node *p)
  24. {
  25.       if (p == NULL)
  26.             return;
  27.       printf("%s     %s      %s\n", p->Name, p->Phone, p->Email);
  28. }
  29. Node* top(Stack *s)
  30. {
  31.       return s->tail;
  32. }
  33. Stack* makeStack()
  34. {
  35.       Stack *s;
  36.       s = (Stack *)malloc(sizeof(Stack));
  37.       s->head = NULL;
  38.       return s;
  39. }
  40. void pop(Stack *s)
  41. {
  42.       Node* iterator;
  43.       if(s->tail == NULL)
  44.       {
  45.             printf("ERROR STACK");
  46.             return;
  47.       }
  48.       iterator = s->head;
  49.       while (iterator->next != s->tail || iterator->next != NULL)
  50.       {
  51.             iterator = iterator->next;
  52.       }
  53.       iterator->next = NULL;
  54.       s->tail = iterator;
  55.       return;
  56. }
  57. void push(Stack *s, Node* newNode)
  58. {
  59.       if(s->head == NULL)
  60.       {
  61.             s->head = newNode;
  62.             s->tail = newNode;
  63.             return;
  64.       }
  65.       s->tail->next = newNode;
  66.       s->tail = newNode;
  67.       return;
  68. }
  69. int main()
  70. {
  71.       Stack *s = makeStack();
  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.                   if (feof(f))
  85.                         break;
  86.                   Node *newNode = makeNode(s1, s2, s3);
  87.                   push(s, newNode);
  88.             } while (!feof(f));
  89.             Node *node = top(s);
  90.             pop(s);
  91.             while (node != NULL)
  92.             {
  93.                   printfnode(node);
  94.                   node = top(s);
  95.                   if (node != NULL)
  96.                         pop(s);
  97.             }
  98.       }
  99.       return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment