-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacks_vitalOperations.c
80 lines (80 loc) · 2 KB
/
stacks_vitalOperations.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
76
77
78
79
80
#include <stdio.h>
int Push (int stack[10], int top);
void display (int stack[10], int top);
int pop (int stack[10], int top);
void peek (int stack[10], int top);
int main()
{
int stack[10];
int top = -1;
int choice;
do
{
printf("\n Press 1: For push operation");
printf("\n Press 2 : For pop operation");
printf("\n Press 3 : For peek operation");
printf("\n Press 4 : For displaying the stack");
printf ("\n Press 0 : For exit");
printf("\n Enter your choice here : ");
scanf("%d", &choice);
switch (choice)
{
case 1: top = Push(stack, top);
break;
case 2: top = pop(stack, top);
break;
case 3: peek(stack, top);
break;
case 4: display (stack, top);
break;
case 0: printf("\n The program is ended\n");
break;
default : printf("\n Wrong Choice");
break;
}
} while (choice != 0);
return 0;
}
int Push (int stack[10], int top)
{
int a;
printf("\n Enter the number : ");
scanf("%d", &a);
if (top == 9)
{
printf("\n Overflow Condition");
}
else
{
top ++;
stack [top] = a;
}
return top;
}
int pop (int stack[10], int top)
{
if (top == -1)
{
printf("\n Undeflow Condition");
}
else
{
top --;
}
printf("\n The topmost element of the stack is deleted");
return top;
}
void peek (int stack[10], int top)
{
int topMost = stack[top];
printf("The topmost element of the stack is %d", topMost);
}
void display (int stack[10], int top)
{
printf("\n*********Printing The Stack***************\n");
//printf("%d %d", top, stack[top]);
for (int i = top; i >= 0; i --)
{
printf("\n %d", stack[i]);
}
}