Write a program to implement stack data structure using linked list.
A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In other words the item which in inserted(pushed) last, will be the first item to be popped.
Operations to be performed.
1] Push if stack is not full
2] Pop if stack is not empty
3] Top of stack
#include<stdio.h> #include<stdlib.h> #define MAX 5 unsigned int inc = 0; typedef struct SingleLinkList { int num; struct SingleLinkList *next; }SLL; void push(int,SLL**); int pop(SLL**); int top_of_stack(SLL*); int print(SLL*); int main() { SLL *hp = 0; int n,op; printf("1] Push 2] Pop 3] Top of stack 4] Print 0] Exit\n"); scanf("%d",&op); while(op != 0) { switch(op) { case 1: if(inc == 5) { printf("Stack is full\n"); break; } printf("Enter Value to Push = "); scanf("%d",&n); push(n,&hp); printf("Count = %d\n",print(hp)); break; case 2: if(inc == 0) { printf("Stack Empty\n"); break; } printf("Popped Value is = %d\n",pop(&hp)); printf("Count = %d\n",print(hp)); break; case 3: printf("Top of stack = %d\n",top_of_stack(hp)); break; case 4: printf("Count = %d\n",print(hp)); break; default: printf("Invalid option.\n"); } printf("1] Push 2] Pop 3] Top of stack 4] Print 0] Exit\n"); scanf("%d",&op); } return 0; } void push(int n,SLL**p) { SLL *p1; p1 = (SLL*)malloc(sizeof(SLL)); p1->num = n; inc++; p1->next = *p; *p = p1; } int pop(SLL**p) { SLL *p1; int n; p1 = *p; n = p1->num; *p = p1->next; free(p1); inc--; return n; } int top_of_stack(SLL*p) { return (p->num); } int print(SLL*p) { unsigned int c = 0; while(p) { c++; printf("%d ",p->num); p = p->next; } printf("\n"); return c; }
Output
1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 1 Enter Value to Push = 10 10 Count = 1 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 1 Enter Value to Push = 20 20 10 Count = 2 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 1 Enter Value to Push = 30 30 20 10 Count = 3 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 1 Enter Value to Push = 40 40 30 20 10 Count = 4 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 1 Enter Value to Push = 50 50 40 30 20 10 Count = 5 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 1 Stack is full 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 1 Stack is full 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 2 Popped Value is = 50 40 30 20 10 Count = 4 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 2 Popped Value is = 40 30 20 10 Count = 3 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 3 Top of stack = 30 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 2 Popped Value is = 30 20 10 Count = 2 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 4 20 10 Count = 2 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 2 Popped Value is = 20 10 Count = 1 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 2 Popped Value is = 10 Count = 0 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 2 Stack Empty 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 2 Stack Empty 1] Push 2] Pop 3] Top of stack 4] Print 0] Exit 0