-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack-using-Array.cpp
90 lines (77 loc) · 1.58 KB
/
Stack-using-Array.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
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 <iostream>
using namespace std;
struct stack {
int size;
int top;
int *s;
}first;
void CreateStack(struct stack *st){
std::cout << "Type the required size of the stack" << std::endl;
cin>>(st->size);
st->s= new int [(st->size)];
st->top=-1;
}
void DisplayStack(struct stack st){
for(int i=(st.top); i>=0; i--){
cout<<(st.s[i])<<endl;
}
}
void PushInStack (struct stack *st,int x){
if((st->top)==(st->size-1)){
cout<<"Stack Overflow"<<endl;
}
else{
st->top++;
st->s[st->top]=x;
}
}
int StackPop(struct stack *st){
int x=-1;
if(st->top==-1){
cout<<"Stack Underflow"<<endl;
return 0;
}
else{
x=st->s[st->top];
st->top--;
return x;
}
}
void Peek(struct stack *st, int pos){
if(st->(top-pos+1)<0){
cout<<"Invalid Index"<<endl;
}
else{
cout<<st->s[st->(top-pos+1)]<<endl;
}
}
bool IsEmpty(struct stack *st){
if(st->top==-1){
return TRUE;
}
return FALSE;
}
bool IsFull(stack *st){
if(st->top==(st->size)-1){
return TRUE;
}
return FALSE;
}
int StackTop(stack st){
if(st.top==-1){
cout<<"No top"<<endl;
return 0;
}
else{
return st.s[st.top];
}
}
int main(){
//Write a function that creates stack from a given array
CreateStack(&first);
PushInStack(&first,12);
PushInStack(&first,24);
StackPop(&first); //Pops 24
DisplayStack(first); //Displays the stack as 24 and 12 (if pop wasn't there)
return 0;
}