Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct NODE {
- int data;
- struct NODE* next;
- }node;
- node* top = NULL;
- // Push Function
- void push(int x)
- {
- node *temp;
- temp = (node *) malloc(sizeof(node));
- temp -> data = x;
- temp -> next = top;
- top = temp;
- }
- // Pop function
- int pop()
- {
- node* temp;
- int num=0;
- temp = top;
- num = temp -> data;
- top = top -> next;
- free(temp);
- return num;
- }
- int Top()
- {
- return top -> data;
- }
- void display(node *head)
- {
- if(head == NULL) printf("NULL!n");
- else
- {
- printf("%dn", head -> data);
- display(head->next);
- }
- }
- int main() {
- int element,choice,val,tp;
- do
- {
- printf("---Stack Operations---n");
- printf("1.PUSHn");
- printf("2.POPn");
- printf("3.DISPLAYn");
- printf("4.Top elementn");
- printf("5.EXITn");
- printf("Enter an optionn");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- printf("Enter the element to pushn");
- scanf("%d",&val);
- push(val);
- break;
- case 2:
- element = pop();
- printf("Popped element is: %dn",element);
- break;
- case 3:
- display(top);
- break;
- case 4:
- tp = Top();
- printf("Top element is:%dn",tp);
- break;
- case 5:
- exit(0);
- default:
- printf("Enter a valid optionn");
- }
- }while(choice != 5);
- }
Add Comment
Please, Sign In to add comment