-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_arrays.c
58 lines (53 loc) · 1.01 KB
/
stack_arrays.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
#include<stdio.h>
#include<stdlib.h>
#define max 10
int st[max], top = -1, i; //initially set to -1. will be incremented when elements r added to stack
void display(){
printf("\nStack now :\n");
for(i = top; i>=0; i--){
printf("\t%d",st[i]);
}
}
void push(int n){
if (top == max-1){
printf("Overflow\n");
}
else{
st[++top] = n; // put ++ before top else top is incremented after value is stored.
printf("\nValue added to stack.");
display();
}
}
void pop(){
if (top == -1){
printf("Underflow\n");
}
else{
top--;
display();
}
}
int peek(){
if (top == -1){
printf("\nUnderflow");
}
else{
return st[top];
}
}
int main(){
int n;
printf("Stack size : 10\n");
printf("Enter -1 to stop.\nEnter elements in the stack\n");
scanf("%d", &n);
while(n != -1){
st[++top] = n;
scanf("%d", &n);
}
printf("\nFirst element poping from stack...");
pop();
printf("\nEnter element to push to stack : ");
scanf("%d", &n);
push(n);
return 0;
}