0% found this document useful (0 votes)
10 views

StackArrayImplementation C

The document presents a C program that implements a stack using an array. It defines functions to push, pop, peek and display elements in the stack. The main function contains a menu to call these functions and demonstrate pushing numbers onto the stack, popping them off, and handling overflow and underflow cases.

Uploaded by

gamerrakib98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

StackArrayImplementation C

The document presents a C program that implements a stack using an array. It defines functions to push, pop, peek and display elements in the stack. The main function contains a menu to call these functions and demonstrate pushing numbers onto the stack, popping them off, and handling overflow and underflow cases.

Uploaded by

gamerrakib98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

17/01/2024, 02:30 StackArrayImplementation.

DSA LAB\StackArrayImplementation.c

1 //Question: Write a C program to implement stack operation push,pop,display on array.


2 //Code:
3
4 #include<stdio.h>
5
6
7 #define MAX 3 //Maximum size of stack array
8 int stack[MAX], top=-1; //top is initially null i.e., -1
9 void push(int stack[],int val){
10 if(top==MAX-1)
11 printf("\nStack Overflow");
12 else{
13 top++;
14 stack[top]=val;}
15 }
16 int pop(int stack[]){
17 int x;
18 if(top==-1){
19 printf("\nStack Underflow");
20 return -1;}
21 else{
22 x=stack[top];
23 top--;
24 return x;}
25 }
26 void peek(int stack[]){
27 if(top==-1)
28 printf("\nStack is empty.");
29 else
30 printf("\nThe value stored at the top of the stack: %d",stack[top]);
31 }
32 void display(int stack[]){
33 if(top==-1){
34 printf("\nStack is empty.");
35 }
36 else{
37 for(int i=top;i>=0;i--)
38 printf("\n%d",stack[i]);
39 }
40 }
41 int main(){
42 int option,val;
43
44 do{
45 printf("\n*****Main Menu*****");
46 printf("\n1. PUSH");
47 printf("\n2. POP");
48 printf("\n3. PEEK");
49 printf("\n4. DISPLAY");
50 printf("\n5. EXIT");
51 printf("\nEnter your option: ");
52 scanf("%d",&option);
53
54
55 switch(option){
56
57 case 1:

localhost:51970/9d0aee79-9e55-4a94-ace7-824b5f23d77b/ 1/4
17/01/2024, 02:30 StackArrayImplementation.c
58 printf("\nEnter the element to be added on stack: ");
59 scanf("%d",&val);
60 push(stack,val);
61 break;
62 case 2:
63 val=pop(stack);
64 if(val!=-1)
65 printf("\nThe element is removed from the stack: %d",val);
66 break;
67 case 3:
68 peek(stack);
69 break;
70 case 4:
71 display(stack);
72 }
73 }while(option!=5);
74
75 }
76
77 //Output:
78 /*
79
80 *****Main Menu*****
81 1. PUSH
82 2. POP
83 3. PEEK
84 4. DISPLAY
85 5. EXIT
86 Enter your option: 1
87
88 Enter the element to be added on stack: 5
89
90 *****Main Menu*****
91 1. PUSH
92 2. POP
93 3. PEEK
94 4. DISPLAY
95 5. EXIT
96 Enter your option: 1
97
98 Enter the element to be added on stack: 9
99
100 *****Main Menu*****
101 1. PUSH
102 2. POP
103 3. PEEK
104 4. DISPLAY
105 5. EXIT
106 Enter your option: 1
107
108 Enter the element to be added on stack: 8
109
110 *****Main Menu*****
111 1. PUSH
112 2. POP
113 3. PEEK
114 4. DISPLAY
115 5. EXIT
116 Enter your option: 1
117

localhost:51970/9d0aee79-9e55-4a94-ace7-824b5f23d77b/ 2/4
17/01/2024, 02:30 StackArrayImplementation.c
118 Enter the element to be added on stack: 2
119
120 Stack Overflow
121 *****Main Menu*****
122 1. PUSH
123 2. POP
124 3. PEEK
125 4. DISPLAY
126 5. EXIT
127 Enter your option: 4
128
129 8
130 9
131 5
132 *****Main Menu*****
133 1. PUSH
134 2. POP
135 3. PEEK
136 4. DISPLAY
137 5. EXIT
138 Enter your option: 2
139
140 The element is removed from the stack: 8
141 *****Main Menu*****
142 1. PUSH
143 2. POP
144 3. PEEK
145 4. DISPLAY
146 5. EXIT
147 Enter your option: 2
148
149 The element is removed from the stack: 9
150 *****Main Menu*****
151 1. PUSH
152 2. POP
153 3. PEEK
154 4. DISPLAY
155 5. EXIT
156 Enter your option: 2
157
158 The element is removed from the stack: 5
159 *****Main Menu*****
160 1. PUSH
161 2. POP
162 3. PEEK
163 4. DISPLAY
164 5. EXIT
165 Enter your option: 3
166
167 Stack is empty.
168 *****Main Menu*****
169 1. PUSH
170 2. POP
171 3. PEEK
172 4. DISPLAY
173 5. EXIT
174 Enter your option: 2
175
176 Stack Underflow
177 *****Main Menu*****

localhost:51970/9d0aee79-9e55-4a94-ace7-824b5f23d77b/ 3/4
17/01/2024, 02:30 StackArrayImplementation.c
178 1. PUSH
179 2. POP
180 3. PEEK
181 4. DISPLAY
182 5. EXIT
183 Enter your option: 5
184 PS C:\Users\iraki\Desktop\My Program\DSA LAB>
185 */

localhost:51970/9d0aee79-9e55-4a94-ace7-824b5f23d77b/ 4/4

You might also like