-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
56 lines (50 loc) · 949 Bytes
/
stack.cpp
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
#include<bits/stdc++.h>
using namespace std;
#define size 5
int top=-1;
void push(int arr[], int value){
if(top>=size-1){
printf("stack overflow\n");
}
else{
arr[++top]=value;
}
}
int pop(int arr[])
{
if(top<0)
{
printf("stack is empty\n");
return -1;
}
else{
return arr[top--];
}
}
void display(int stack[])
{
printf("\nPrint the full stack from TOP to BOTTOM:\n");
for(int i = top; i>=0; i--)
printf("%d\n",stack[i]);
}
int main()
{
int arr[size];
push(arr,1);
push(arr,4);
push(arr,3);
push(arr,2);
push(arr,5);
display(arr);
push(arr,56);
printf("pop=%d\n",pop(arr));
printf("pop=%d\n",pop(arr));
printf("pop=%d\n",pop(arr));
printf("pop=%d\n",pop(arr));
display(arr);
printf("pop=%d\n",pop(arr));
printf("pop=%d\n",pop(arr));
printf("pop=%d\n",pop(arr));
for(int i=0;i<top;i++)
printf("%d ",arr[i]);
}