-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstack_using_array.c
More file actions
80 lines (63 loc) · 1.15 KB
/
stack_using_array.c
File metadata and controls
80 lines (63 loc) · 1.15 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
#include <stdio.h>
#define MAXSIZE 100
int stack[MAXSIZE];
int top = -1;
int isEmpty()
{
return (top == -1); //if top is -1 return 1 else return 0
}
int isfull()
{
return (top == MAXSIZE); //if top is equal to MAXSIZE return 1 else return 0
}
int peek()
{
int data = -1;
if(!isEmpty())
data = stack[top];
return data;
}
int pop()
{
int data = peek();
if(data != -1)
top--;
return data;
}
void push(int data)
{
if(!isfull()) {
top = top + 1;
stack[top] = data;
}
else
{
printf("Could not insert data, Stack is full.\n");
}
}
int main()
{
int value, action;
while (1) {
printf("Enter Action: \n 1.Insert \n 2.Get Top Value \n 3.Exit\n");
scanf("%d", &action);
if(action == 1) //Insertion
{
printf("Enter Value: ");
scanf("%d", &value);
push(value);
printf("Element at top of the stack: %d\n", peek());
}
else if(action ==2)
{
value = pop();
if(value == -1)
printf("Could not retrieve data, Stack is empty.\n");
else
printf("Popped value: %d\n", value);
}
else
break;
}
return 0;
}