-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacksUsingArrays.c
90 lines (86 loc) · 1.33 KB
/
stacksUsingArrays.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
81
82
83
84
85
86
87
88
89
90
include <stdio.h>
#include <stdlib.h>
#define STACK_MAX_SIZE 10
int stack[STACK_MAX_SIZE];
int top = -1;
int main() {
int op, x;
while(1) {
printf("1.Push 2.Pop 3.Display 4.Is Empty 5.Peek 6.Exit\n");
printf("Enter your option : ");
scanf("%d", &op);
switch(op) {
case 1:
printf("Enter element : ");
scanf("%d", &x);
push(x);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
isEmpty();
break;
case 5:
peek();
break;
case 6:
exit(0);
}
}
}
void push(int x)
{
if(top == STACK_MAX_SIZE - 1)
{
printf("Stack is overflow.\n");
return;
}
stack[++top] = x;
printf("Successfully pushed.\n");
}
void pop()
{
if(top == -1)
{
printf("Stack is underflow.\n");
return;
}
printf("Popped value = %d\n", stack[top--]);
}
void display()
{
int i;
if(top == -1)
{
printf("Stack is empty.\n");
return;
}
printf("Elements of the stack are : ");
for(i = top; i >= 0; i--)
{
printf("%d ", stack[i]);
}
printf("\n");
}
void isEmpty()
{
if(top == -1)
{
printf("Stack is empty.\n");
return;
}
printf("Stack is not empty.\n");
}
void peek()
{
if(top == -1)
{
printf("Stack is underflow.\n");
return;
}
printf("Peek value = %d\n", stack[top]);
}