[go: up one dir, main page]

0% found this document useful (0 votes)
18 views3 pages

Mahesh C File

Uploaded by

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

Mahesh C File

Uploaded by

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

#include <stdio.

h>

int stack[100], choice, n, top = -1, x;

void push(void);

void pop(void);

void display(void);

int main() {

printf("\nEnter the size of stack: ");

scanf("%d", &n);

printf("\n\tStack operations using array:");

printf("\n\t-----------------------------------------");

printf("\n\t1. Push\n\t2. Pop\n\t3. Display\n\t4. Exit");

do {

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

push();

break;

case 2:

pop();

break;
case 3:

display();

break;

case 4:

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

break;

default:

printf("Invalid choice! Try again.\n");

} while (choice != 4);

return 0;

void push() {

if (top >= n - 1) {

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

} else {

printf("Enter value to push: ");

scanf("%d", &x);

top++;

stack[top] = x;

printf("%d pushed to stack\n", x);

}
void pop() {

if (top <= -1) {

printf("Stack Underflow! Cannot pop\n");

} else {

printf("%d popped from stack\n", stack[top]);

top--;

void display() {

if (top >= 0) {

printf("Stack elements:\n");

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

printf("%d\n", stack[i]);

} else {

printf("Stack is empty\n");

You might also like