Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct Node
- {
- char Name[80], Phone[80], Email[80];
- struct Node *next;
- } Node;
- typedef struct Queue
- {
- struct Node *head;
- } Queue;
- Node *front(Queue* q)
- {
- if (q->head == NULL)
- return NULL;
- return q->head;
- }
- Node *makeNode(char *s1, char *s2, char *s3)
- {
- Node *p = (Node *)malloc(sizeof(Node));
- strcpy(p->Name, s1);
- strcpy(p->Phone, s2);
- strcpy(p->Email, s3);
- p->next = NULL;
- return p;
- }
- void printfll(Node* p)
- {
- if(p == NULL)
- return;
- printf("%s %s %s\n", p->Name, p->Phone, p->Email);
- printfll(p->next);
- }
- Queue *makeQueue()
- {
- Queue *q;
- q = (Queue *)malloc(sizeof(Queue));
- q->head = NULL;
- return q;
- }
- void pop(Queue *q)
- {
- Node *tmp;
- if (q->head == NULL)
- {
- printf("ERROR QUEUE\n");
- return;
- }
- tmp = q->head;
- q->head = q->head->next;
- free(tmp);
- }
- void push(Queue *q, Node *newNode)
- {
- Node *iterator;
- if (q->head == NULL)
- {
- q->head = newNode;
- return;
- }
- iterator = q->head;
- while (iterator->next != NULL)
- {
- iterator = iterator->next;
- }
- iterator->next = newNode;
- return;
- }
- int main()
- {
- Queue *q = makeQueue();
- FILE *f;
- f = fopen("phonebook.txt", "r");
- if (f == NULL)
- {
- printf("FILE NOT FOUND");
- }
- else
- {
- char s1[80], s2[80], s3[80];
- do
- {
- fscanf(f, "%s %s %s", s1, s2, s3);
- Node *newNode = makeNode(s1, s2, s3);
- push(q, newNode);
- } while (feof(f));
- Node *node = front(q);
- pop(q);
- while (node != NULL)
- {
- node = front(q);
- printfll(node);
- if(node!=NULL)
- pop(q);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment