Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- struct Node
- {
- char *data;
- struct Node *link;
- };
- struct Node *top;
- void push(char *data)
- {
- struct Node *temp;
- temp = (struct Node *)malloc(sizeof(struct Node));
- if (!temp)
- {
- printf("\nHeap Overflow");
- abort();
- }
- temp->data = _strdup(data);
- temp->link = top;
- top = temp;
- }
- int isEmpty()
- {
- return top == NULL;
- }
- char *peek()
- {
- if (!isEmpty(top))
- return top->data;
- else
- abort();
- }
- void pop()
- {
- struct Node *temp;
- if (top == NULL)
- {
- //printf("\nStack Underflow");
- abort();
- }
- else
- {
- // top assign into temp
- temp = top;
- // assign second node to top
- top = top->link;
- // destroy connection between first and second
- temp->link = NULL;
- // release memory of top node
- free(temp);
- }
- }
- void display() // remove at the beginning
- {
- struct Node *temp;
- // check for stack underflow
- if (top == NULL)
- {
- //printf("\nStack Underflow");
- abort();
- }
- else
- {
- temp = top;
- printf("Stack contains:\n");
- while (temp != NULL)
- {
- // print node data
- printf("%s\n", temp->data);
- // assign temp link to temp
- temp = temp->link;
- }
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement