-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep45_stack_1.cpp
More file actions
79 lines (69 loc) · 1.86 KB
/
practicep45_stack_1.cpp
File metadata and controls
79 lines (69 loc) · 1.86 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
#include <iostream>
using namespace std;
//stack and its operational functions
void push(int &top,int value,int stack[],int MaxStackSize){
if(top>=MaxStackSize-1){//stack overflow....stack starting from 0
cout<<"Stack is full"<<endl;
return;
}
stack[++top]=value; //first increment top then add to stack
};
void pop(int &top,int stack[]){
if(top==-1){
cout<<"Stack is empty"<<endl;
return;
};
cout<<stack[top--]<<endl;
};
void peek(int &top,int stack[]){
if(top==-1){
cout<<"Stack is empty"<<endl;
return;
};
cout<<stack[top]<<endl;
};
bool isEmpty(int &top){
return top==-1;
//another way to check
// if(top==-1){
// return true;
// }
// else{
// return false;
// }
};
bool isFull(int &top,int &MaxStackSize){
return top==MaxStackSize-1; //because top is starting from 0
};
void displayall(int &top,int stack[],int &MaxStackSize){
if(top==-1){
cout<<"Stack is empty"<<endl;
return;
};
for(int i=top;i>=0;i--){
cout<<stack[i]<<" ";
};
};
int main(){
cout<<"Enter stack size: ";
int MaxStackSize;
cin>>MaxStackSize;
cout<<endl;
int stack[MaxStackSize];
int top=-1;
for(int i=0;i<MaxStackSize;++i){
cout<<"Enter the value: ";
int value;
cin>>value;
push(top,value,stack,MaxStackSize);
};
pop(top,stack); //deletes the last element(here it also prints the deleted element)
peek(top,stack); //shows the last element(first on top)
cout<<isEmpty(top)<<endl; //0 if false and 1 if true
cout<<isFull(top,MaxStackSize)<<endl; //0 if false and 1 if true
push(top,16,stack,MaxStackSize);
push(top,177,stack,MaxStackSize);
cout<<isFull(top,MaxStackSize)<<endl;// 0 if false and 1 if true
displayall(top,stack,MaxStackSize);
return 0;
}