-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_stack_using_array.c
More file actions
88 lines (80 loc) · 1.8 KB
/
10_stack_using_array.c
File metadata and controls
88 lines (80 loc) · 1.8 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<stdio.h>
#define MAX 15
int STACK[MAX];
// Function to Push Element into Stack
int push(int top)
{
if(top == (MAX - 1))
printf("\nStack Overflow\n\n");
else
{
printf("\nEnter Item : ");
scanf("%d", &STACK[++top]);
printf("Item Pushed Successfully\n\n");
}
return top;
}
// Function to Pop Element from Stack
int pop(int top)
{
if(top == -1)
printf("\nStack Underflow\n\n");
else
{
printf("\nPopped Item : %d\n", STACK[top--]);
printf("Item Popped Successfully\n\n");
}
return top;
}
// Function to Peek into Stack
int peek(int top)
{
if(top == -1)
printf("\nStack Underflow\n\n");
else
printf("\nTop-Most Item : %d\n\n", STACK[top]);
return 0;
}
/*
// Function to Display Element of Stack
int display(int top)
{
int index;
if(top == -1)
printf("\nStack Underflow\n\n");
else
{
printf("\nElements\n");
for(index = 0 ; index < top + 1 ; index++)
printf("Element %d : %d\n", index + 1, STACK[index]);
printf("\n");
}
return 0;
}
*/
int main()
{
int choice;
int top = -1;
while(1)
{
printf("Enter\n");
printf("1. To Push an Element\n");
printf("2. To Pop an Element\n");
printf("3. To Peek into the Stack\n");
// printf("4. Display Stack\n");
printf("0. To Exit\n");
printf("Enter Your Choice : ");
scanf("%d", &choice);
switch(choice)
{
case 0 : return 0;
case 1 : top = push(top); break;
case 2 : top = pop(top); break;
case 3 : peek(top); break;
// case 4 : display(top); break;
default : printf("\nInvalid Choice, Try Again\n\n");
}
}
return 0;
}