Simple Stack Program in C Programming
Definition
A stack is a basic computer science data structure and can be defined in an abstract, implementation-free manner, or it can be generally defined as a linear list of items in which all additions and deletion are restricted to one end that is Top.
Simple Stack Program in C Programming
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
int main() {
int item, choice, i;
int arr_stack[MAX_SIZE];
int top = 0;
int exit = 1;
printf("\nSimple Stack Example - Array");
do {
printf("\n\nnStack Main Menu");
printf("\n1.Push \n2.Pop \n3.Display \nOthers to exit");
printf("\nEnter Your Choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (top == MAX_SIZE)
printf("\n## Stack is Full!");
else {
printf("\nEnter The Value to be pushed : ");
scanf("%d", &item);
printf("\n## Position : %d , Pushed Value : %d ", top, item);
arr_stack[top++] = item;
}
break;
case 2:
if (top == 0)
printf("\n## Stack is Empty!");
else {
--top;
printf("\n## Position : %d , Popped Value : %d ", top, arr_stack[top]);
}
break;
case 3:
printf("\n## Stack Size : %d ", top);
for (i = (top - 1); i >= 0; i--)
printf("\n## Position : %d , Value : %d ", i, arr_stack[i]);
break;
default:
exit = 0;
break;
}
} while (exit);
return 0;
}
Sample Output:
Simple Stack Example - Array
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 34
## Position : 0 , Pushed Value : 34
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 89
## Position : 1 , Pushed Value : 89
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 900
## Position : 2 , Pushed Value : 900
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 450
## Position : 3 , Pushed Value : 450
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
Enter The Value to be pushed : 789
## Position : 4 , Pushed Value : 789
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 1
## Stack is Full!
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 4 , Popped Value : 789
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 3 , Popped Value : 450
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 2 , Popped Value : 900
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 1 , Popped Value : 89
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Position : 0 , Popped Value : 34
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 2
## Stack is Empty!
Main Menu
1.Push
2.Pop
3.Display
Others to exit
Enter Your Choice : 4
------------------
(program exited with code: 0)
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )