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 Stack
- {
- struct Node *head;
- struct Node *tail;
- } Stack;
- 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 printfnode(Node *p)
- {
- if (p == NULL)
- return;
- printf("%s %s %s\n", p->Name, p->Phone, p->Email);
- }
- Node* top(Stack *s)
- {
- return s->tail;
- }
- Stack* makeStack()
- {
- Stack *s;
- s = (Stack *)malloc(sizeof(Stack));
- s->head = NULL;
- return s;
- }
- void pop(Stack *s)
- {
- Node* iterator;
- if(s->tail == NULL)
- {
- printf("ERROR STACK");
- return;
- }
- iterator = s->head;
- while (iterator->next != s->tail || iterator->next != NULL)
- {
- iterator = iterator->next;
- }
- iterator->next = NULL;
- s->tail = iterator;
- return;
- }
- void push(Stack *s, Node* newNode)
- {
- if(s->head == NULL)
- {
- s->head = newNode;
- s->tail = newNode;
- return;
- }
- s->tail->next = newNode;
- s->tail = newNode;
- return;
- }
- int main()
- {
- Stack *s = makeStack();
- 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);
- if (feof(f))
- break;
- Node *newNode = makeNode(s1, s2, s3);
- push(s, newNode);
- } while (!feof(f));
- Node *node = top(s);
- pop(s);
- while (node != NULL)
- {
- printfnode(node);
- node = top(s);
- if (node != NULL)
- pop(s);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment