0% found this document useful (0 votes)
2 views2 pages

Implementation of Stack Using Array

The document contains a C program that implements a stack data structure using an array. It provides functionalities such as push, pop, peek, check if the stack is empty or full, and display the stack elements. The program runs in a loop, allowing the user to interact with the stack through a menu-driven interface.

Uploaded by

Kalyani Ghadge
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)
2 views2 pages

Implementation of Stack Using Array

The document contains a C program that implements a stack data structure using an array. It provides functionalities such as push, pop, peek, check if the stack is empty or full, and display the stack elements. The program runs in a loop, allowing the user to interact with the stack through a menu-driven interface.

Uploaded by

Kalyani Ghadge
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

1 #include <stdio.

h>
2 #include <stdlib.h>
3
4 #define MAX 5
5
6 int stack[MAX], top = -1;
7
8 void push() {
9 int val;
10 if (top == MAX - 1) {
11 printf("\nStack Overflow! Cannot add more elements.");
12 } else {
13 printf("Enter value to push: ");
14 scanf("%d", &val);
15 top++;
16 stack[top] = val;
17 printf("Inserted %d", val);
18 }
19 }
20
21 void pop() {
22 if (top == -1) {
23 printf("\nStack Underflow! No elements to delete.");
24 } else {
25 printf("Popped element: %d", stack[top]);
26 top--;
27 }
28 }
29
30 void peek() {
31 if (top == -1) {
32 printf("\nStack is empty.");
33 } else {
34 printf("Top element is: %d", stack[top]);
35 }
36 }
37
38 void isEmpty() {
39 if (top == -1) printf("\nStack is Empty.");
40 else printf("\nStack is not Empty.");
41 }
42
43 void isFull() {
44 if (top == MAX - 1) printf("\nStack is Full.");
45 else printf("\nStack is not Full.");
46 }
47
48 void display() {
49 if (top == -1) {
50 printf("\nStack is empty.");
51 } else {
52 printf("\nStack elements: ");
53 for (int i = top; i >= 0; i--)
54 printf("%d ", stack[i]);
55 }
56 }
57
58 int main() {
59 int choice;
60 while (1) {
61 printf("\n\n--- Stack Array Menu ---");
62 printf("\n1. Push\n2. Pop\n3. Peek\n4. isEmpty\n5. isFull\n6. Display\n7. Exit");
63 printf("\nEnter choice: ");
64 scanf("%d", &choice);
65
66 switch (choice) {
67 case 1: push(); break;
68 case 2: pop(); break;
69 case 3: peek(); break;
70 case 4: isEmpty(); break;
71 case 5: isFull(); break;
72 case 6: display(); break;
73 case 7: exit(0);
74 default: printf("\nInvalid choice!");
75 }
76 }
77 return 0;
78 }

You might also like