nguyentruong98

Untitled

Nov 6th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 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.       struct Node *prev;
  9. } Node;
  10. Node *head;
  11. Node *tail;
  12. Node* makeNode(char *s1, char* s2, char* s3)
  13. {
  14.       Node*p = (Node*)malloc(sizeof(Node));
  15.       strcpy(p->Name,s1);
  16.       strcpy(p->Phone,s2);
  17.       strcpy(p->Email,s3);
  18.       p->next = NULL;
  19.       p->prev = NULL;
  20.       return p;
  21. }
  22. void addNode(Node* p)
  23. {
  24.       if(head == NULL)
  25.       {
  26.             head = p;
  27.             tail = p;
  28.             return;
  29.       }
  30.       tail->next = p;
  31.       p->prev = tail;
  32.       p = tail;
  33. }
  34. void printfll(Node* p)
  35. {
  36.       while(p != NULL)
  37.       {
  38.             printf("%s %s %s\n", p->Name, p->Phone, p->Email);
  39.             printfll(p->next);
  40.       }
  41. }
  42. int main(){
  43.       FILE *f;
  44.       f = fopen("phonebook.txt", "r");
  45.       if (f == NULL)
  46.       {
  47.             printf("ERROR");
  48.       }
  49.       else
  50.       {
  51.             char s1[80], s2[80], s3[80];
  52.             do{
  53.                   fscanf(f, "%s %s %s", s1, s2, s3);
  54.                   Node* newNode = makeNode(s1,s2,s3);
  55.                   addNode(newNode);
  56.             }while(!feof(f));
  57.       }
  58.       printfll(head);
  59.       return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment