1#include <stdio.h>
2#include <stdlib.h>
3#define TRUE 1
4#define FALSE 0
5
6struct node
7{
8 int data;
9 struct node *next;
10};
11typedef struct node node;
12
13node *top;
14
15void initialize()
16{
17 top = NULL;
18}
19
20void push(int value)
21{
22 node *tmp;
23 tmp = malloc(sizeof(node));
24 tmp -> data = value;
25 tmp -> next = top;
26 top = tmp;
27}
28
29int pop()
30{
31 node *tmp;
32 int n;
33 tmp = top;
34 n = tmp->data;
35 top = top->next;
36 free(tmp);
37 return n;
38}
39
40int Top()
41{
42 return top->data;
43}
44
45int isempty()
46{
47 return top==NULL;
48}
49
50void display(node *head)
51{
52 if(head == NULL)
53 {
54 printf("NULL\n");
55 }
56 else
57 {
58 printf("%d\n", head -> data);
59 display(head->next);
60 }
61}
62
63int main()
64{
65 initialize();
66 push(10);
67 push(20);
68 push(30);
69 printf("The top is %d\n",Top());
70 pop();
71 printf("The top after pop is %d\n",Top());
72 display(top);
73 return 0;
74}
75
1/*
2 * C Program to Implement a Stack using Linked List
3 */
4#include <stdio.h>
5#include <stdlib.h>
6
7struct node
8{
9 int info;
10 struct node *ptr;
11}*top,*top1,*temp;
12
13int topelement();
14void push(int data);
15void pop();
16void empty();
17void display();
18void destroy();
19void stack_count();
20void create();
21
22int count = 0;
23
24void main()
25{
26 int no, ch, e;
27
28 printf("\n 1 - Push");
29 printf("\n 2 - Pop");
30 printf("\n 3 - Top");
31 printf("\n 4 - Empty");
32 printf("\n 5 - Exit");
33 printf("\n 6 - Dipslay");
34 printf("\n 7 - Stack Count");
35 printf("\n 8 - Destroy stack");
36
37 create();
38
39 while (1)
40 {
41 printf("\n Enter choice : ");
42 scanf("%d", &ch);
43
44 switch (ch)
45 {
46 case 1:
47 printf("Enter data : ");
48 scanf("%d", &no);
49 push(no);
50 break;
51 case 2:
52 pop();
53 break;
54 case 3:
55 if (top == NULL)
56 printf("No elements in stack");
57 else
58 {
59 e = topelement();
60 printf("\n Top element : %d", e);
61 }
62 break;
63 case 4:
64 empty();
65 break;
66 case 5:
67 exit(0);
68 case 6:
69 display();
70 break;
71 case 7:
72 stack_count();
73 break;
74 case 8:
75 destroy();
76 break;
77 default :
78 printf(" Wrong choice, Please enter correct choice ");
79 break;
80 }
81 }
82}
83
84/* Create empty stack */
85void create()
86{
87 top = NULL;
88}
89
90/* Count stack elements */
91void stack_count()
92{
93 printf("\n No. of elements in stack : %d", count);
94}
95
96/* Push data into stack */
97void push(int data)
98{
99 if (top == NULL)
100 {
101 top =(struct node *)malloc(1*sizeof(struct node));
102 top->ptr = NULL;
103 top->info = data;
104 }
105 else
106 {
107 temp =(struct node *)malloc(1*sizeof(struct node));
108 temp->ptr = top;
109 temp->info = data;
110 top = temp;
111 }
112 count++;
113}
114
115/* Display stack elements */
116void display()
117{
118 top1 = top;
119
120 if (top1 == NULL)
121 {
122 printf("Stack is empty");
123 return;
124 }
125
126 while (top1 != NULL)
127 {
128 printf("%d ", top1->info);
129 top1 = top1->ptr;
130 }
131 }
132
133/* Pop Operation on stack */
134void pop()
135{
136 top1 = top;
137
138 if (top1 == NULL)
139 {
140 printf("\n Error : Trying to pop from empty stack");
141 return;
142 }
143 else
144 top1 = top1->ptr;
145 printf("\n Popped value : %d", top->info);
146 free(top);
147 top = top1;
148 count--;
149}
150
151/* Return top element */
152int topelement()
153{
154 return(top->info);
155}
156
157/* Check if stack is empty or not */
158void empty()
159{
160 if (top == NULL)
161 printf("\n Stack is empty");
162 else
163 printf("\n Stack is not empty with %d elements", count);
164}
165
166/* Destroy entire stack */
167void destroy()
168{
169 top1 = top;
170
171 while (top1 != NULL)
172 {
173 top1 = top->ptr;
174 free(top);
175 top = top1;
176 top1 = top1->ptr;
177 }
178 free(top1);
179 top = NULL;
180
181 printf("\n All stack elements destroyed");
182 count = 0;
183}