C PROGRAM
Implement Stack using array |Data Structure in C|
Introduction
Stacks are essential data structures in computer science, facilitating last-in, first-out (LIFO) operations. In this blog post, we'll unravel a C program that implements basic stack operations, providing users with a friendly interface for pushing, popping, and displaying elements within a stack.
Code
#include<stdio.h>
#include<conio.h>
#define max 50
void push();
void pop();
void display();
int stack[max], top = -1;
void main() {
int ch;
clrscr();
while(1) {
printf("\n Stack Operations");
printf("\n 1. Push\t 2. Pop\t 3. Display\t 4. Exit");
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch(ch) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit();
break;
default:
printf("\n Invalid choice! Please enter a valid option.");
}
}
getch();
}
void push() {
if (top == max - 1)
printf("\n Stack Overflow");
else {
int element;
printf("Enter element: ");
scanf("%d", &element);
top++;
stack[top] = element;
printf("\n Element (%d) has been pushed at position %d", element, top);
}
}
void pop() {
if (top == -1)
printf("\n Stack Underflow");
else {
printf("\n Element (%d) has been popped out!", stack[top]);
top--;
}
}
void display() {
if (top == -1)
printf("\n Stack is empty!");
else {
printf("\n Stack elements:\n");
for (int i = 0; i <= top; i++)
printf("%d\n", stack[i]);
}
}
Output
Stack Operations
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 1
Enter element: 5
Element (5) has been pushed at position 0
Stack Operations
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 1
Enter element: 8
Element (8) has been pushed at position 1
Stack Operations
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 3
Stack elements:
5
8
Stack Operations
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 2
Element (8) has been popped out!
Stack Operations
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 3
Stack is empty!
Stack Operations
1. Push 2. Pop 3. Display 4. Exit
Enter your choice: 4
Explanation
1. Header Files and Constants:
The program includes the standard input/output header <stdio.h>, the console I/O header <conio.h>, and defines a constant max for the maximum stack size.
2. Function Prototypes:
Function prototypes for push, pop, and display are declared to organize the code structure.
3. Main Function:
The main function serves as the program's entry point.
It includes a loop that presents users with a menu of stack operations, allowing them to choose the desired action.
4. Stack Operations:
Functions push, pop, and display handle the respective stack operations.
push: Adds an element to the top of the stack.
pop: Removes the element from the top of the stack.
display: Shows the current elements in the stack.
5. User Interaction:
Users are prompted to choose an operation and provided with feedback on the success or failure of each operation.
Conclusion
This C program offers a user-friendly interface for basic stack operations, providing a foundation for understanding stack data structures. Users can experiment with pushing, popping, and displaying elements in the stack, gaining insights into how these fundamental operations work.
Feel free to run the program, try different stack operations, and observe how the stack evolves with each interaction. This example serves as a practical introduction to stack implementation in programming. Happy coding!
Post a Comment
0 Comments