-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack_using_array.c
75 lines (70 loc) · 1.72 KB
/
stack_using_array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Implementation of stack data structure using array
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10 // Defaine the size of the stack
void push(int); // Function to push the element in the stack
void pop(); // Function to pop the element from the stack
void display(); // Function to display the stack
int stack[SIZE], top = -1; // Declare the stack and top
void main()
{
int value, choice;
while (1)
{
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: // Push the element in the stack
printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: // Pop the element from the stack
pop();
break;
case 3: // Display the stack
display();
break;
case 4: // Exit the program
exit(0);
default:
printf("\nWrong selection!!! Try again!!!");
}
}
}
void push(int value)
{
if (top == SIZE - 1)
printf("\nStack is Full!!! Insertion is not possible!!!");
else
{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
void pop()
{
if (top == -1)
printf("\nStack is Empty!!! Deletion is not possible!!!");
else
{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
void display()
{
if (top == -1)
printf("\nStack is Empty!!!");
else
{
int i;
printf("\nStack elements are:\n");
for (i = top; i >= 0; i--)
printf("%d\n", stack[i]);
}
}