Stack using Linked List in C
Here in this example you will get code to implement the Stack using Linked List in C programming with Data structure. This program can be implemented by Stack using Array.
Program code to implement stack using linked list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | //Stack using Linked List in c #include<stdio.h> #include<stdlib.h> void push(struct node **,int); int pop(struct node **); void stack_display(struct node *q); int count(struct node *q); struct node { int data; struct node *link; }; main() { struct node *top;//point to top of a stack top=NULL;//empty stack int item; int ch; do { printf("\nStack using Linked List"); printf("\nEnter 1 to push "); printf("\nEnter 2 to pop "); printf("\nEnter 3 to count number of elements in stack"); printf("\nEnter 4 for display"); printf("\nEnter 5 for exit"); printf("\nEnter your choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the element:"); scanf("%d",&item); push(&top,item); break; case 2: item=pop(&top); printf("%d",item); printf("\n"); break; case 3: item=count(top); printf("\n%d item avaliable in list.",item); printf("\n"); break; case 4: stack_display(top); break; case 5: exit(0); break; } }while(ch>=1 && ch<=5); } //add new element on the top of stack void push(struct node **s,int item) { struct node *q; q=(struct node *)malloc(sizeof(struct node)); q->data=item; q->link=*s; *s=q; } //remove element from top of stack int pop(struct node **s) { int item; struct node *q; //if empty stack if(*s==NULL) printf("stack is empty"); else { q=*s; item=q->data; *s=q->link; free(q); return(item); } } //print data of stack void stack_display(struct node *q) { printf("\n"); //traverse linked list while (q!=NULL) { printf("\t%2d",q->data); q=q->link; } printf("\n"); } //counts the no. of nodes in linked list int count(struct node *q) { int c=0; //traverse the linked list while (q!=NULL) { q=q->link; c++; } return c; } |
Output
Stack using Linked List
Enter 1 to push
Enter 2 to pop
Enter 3 to count number of elements in stack
Enter 4 for display
Enter 5 for exit
Enter your choice : 1
Enter the element:10
Stack using Linked List
Enter 1 to push
Enter 2 to pop
Enter 3 to count number of elements in stack
Enter 4 for display
Enter 5 for exit
Enter your choice : 1
Enter the element:12
Stack using Linked List
Enter 1 to push
Enter 2 to pop
Enter 3 to count number of elements in stack
Enter 4 for display
Enter 5 for exit
Enter your choice : 1
Enter the element:14
Stack using Linked List
Enter 1 to push
Enter 2 to pop
Enter 3 to count number of elements in stack
Enter 4 for display
Enter 5 for exit
Enter your choice : 3
3 item avaliable in list.
Stack using Linked List
Enter 1 to push
Enter 2 to pop
Enter 3 to count number of elements in stack
Enter 4 for display
Enter 5 for exit
Enter your choice : 4
14 12 10
Stack using Linked List
Enter 1 to push
Enter 2 to pop
Enter 3 to count number of elements in stack
Enter 4 for display
Enter 5 for exit
Enter your choice : 2
14
Stack using Linked List
Enter 1 to push
Enter 2 to pop
Enter 3 to count number of elements in stack
Enter 4 for display
Enter 5 for exit
Enter your choice : 4
12 10
Stack using Linked List
Enter 1 to push
Enter 2 to pop
Enter 3 to count number of elements in stack
Enter 4 for display
Enter 5 for exit
Enter your choice : 5