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

Stack using Array

The document is a C program that implements a stack data structure with basic operations such as push, pop, and display. It allows the user to initialize the stack with a specified number of elements and interact with it through a menu-driven interface. The program includes error handling for full and empty stack conditions and invalid menu choices.

Uploaded by

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

Stack using Array

The document is a C program that implements a stack data structure with basic operations such as push, pop, and display. It allows the user to initialize the stack with a specified number of elements and interact with it through a menu-driven interface. The program includes error handling for full and empty stack conditions and invalid menu choices.

Uploaded by

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

#include <stdio.

h>

#define MAX 5

typedef struct stack {

int a[MAX];

int top;

} stack;

void push(stack *, int);

int pop(stack *);

int isfull(stack *);

int isempty(stack *);

void display(stack *);

int main() {

stack s;

s.top = -1; // Initialize stack with top as -1 (empty)

int n, ch, num_elements; // `n` for the element and `ch` for choice

printf("Enter number of elements to initialize the stack (max %d): ", MAX);

scanf("%d", &num_elements); // Read number of elements

if (num_elements > MAX) {

printf("Cannot initialize more than %d elements!\n", MAX);

return 1; // Exit the program

printf("Enter %d elements: \n", num_elements);

for (int i = 0; i < num_elements; i++) {


scanf("%d", &n); // Read element from user

push(&s, n); // Push each element into the stack

do {

printf("\n--- Stack Menu ---\n");

printf("1. Push\n2. Pop\n3. Print\n4. Quit\n");

printf("Enter your choice: ");

scanf("%d", &ch); // Read the user's choice

switch (ch) {

case 1: // Push operation

printf("Enter element to push: ");

scanf("%d", &n); // Get element from user

if (!isfull(&s)) {

push(&s, n); // Push if the stack is not full

printf("Element %d pushed.\n", n);

} else {

printf("Stack is full! Cannot push more elements.\n");

break;

case 2: // Pop operation

if (!isempty(&s)) {

n = pop(&s); // Pop the element

printf("Element popped: %d\n", n);

} else {

printf("Stack is empty! No elements to pop.\n");

break;
case 3: // Display the stack

display(&s);

break;

case 4: // Quit the program

printf("Exiting the program...\n");

break;

default:

printf("Invalid choice! Please enter a valid option.\n");

} while (ch != 4); // Continue until user selects Quit

return 0;

void push(stack *p, int n) {

p->top++; // Increment the top index

p->a[p->top] = n; // Add the element to the stack

int pop(stack *p) {

int x = p->a[p->top]; // Store the top element to pop

p->top--; // Decrement the top index

return x; // Return the popped element

int isfull(stack *p) {

return (p->top == MAX - 1); // Check if stack is full

}
int isempty(stack *p) {

return (p->top == -1); // Check if stack is empty

void display(stack *p) {

if (isempty(p)) {

printf("Stack is empty! Nothing to display.\n");

} else {

printf("Stack elements are:\n");

for (int i = p->top; i >= 0; i--) {

printf("a[%d] = %d\n", i, p->a[i]); // Display each element

OUTPUT:

/tmp/b2ktdLsJ17.o

Enter number of elements to initialize the stack (max 5): 3

Enter 3 elements:

463

--- Stack Menu ---

1. Push

2. Pop

3. Print

4. Quit

Enter your choice: 1

Enter element to push: 5

Element 5 pushed.

--- Stack Menu ---

1. Push
2. Pop

3. Print

4. Quit

Enter your choice: 3

Stack elements are:

a[3] = 5

a[2] = 3

a[1] = 6

a[0] = 4

--- Stack Menu ---

1. Push

2. Pop

3. Print

4. Quit

Enter your choice: 2

Element popped: 5

--- Stack Menu ---

1. Push

2. Pop

3. Print

4. Quit

Enter your choice: 6

Invalid choice! Please enter a valid option.

--- Stack Menu ---

1. Push

2. Pop

3. Print

4. Quit
Enter your choice: 4

Exiting the program...

=== Code Execution Successful ===

You might also like