-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstack.cpp
109 lines (76 loc) · 1.33 KB
/
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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define maxsize 10
class Stack {
public:
int top;
int n;
int data;
int array[maxsize];
//constructor
Stack() {
top=-1; //initially
}
//defining the operations of stack
int isEmpty();
int isFull();
int Push();
int pop();
int displayStack();
};
int Stack :: isEmpty() {
return(top==-1) ;
}
int Stack::isFull() {
return(top > maxsize-1) ;
}
int Stack :: Push() {
if(isFull()) {
cout<<"Stack is full"<<endl;
}
else {
cout<<"Enter no of elements in stack"<<endl;
cin>>n;
cout<<"Enter the data in stack"<<endl;
for(int i = 0; i < n ; i++) {
cin>>data;
array[++top] = data ;
cout<<"\n";
}
}
}
int Stack::pop() {
top==-1;
if(isEmpty()) {
cout<<"Stack is empty"<<endl;
}
else {
//remove element from top of stack
return(array[top--]);
}
}
int Stack::displayStack() {
if(isFull()) {
cout<<"Stack is Full"<<endl;
}
else if(isEmpty()) {
cout<<"Stack is empty"<<endl;
}
else {
cout<<"Top"<<"\n";
cout<<"|";
for(int i=n-1 ; i >= 0 ;i--) {
cout<<"\n"<<array[i];
}
cout<<"\n"<<"Bottom"<<endl;
}
}
int main() {
Stack s;
s.Push();
s.displayStack();
cout<<"\n"<< s.pop() <<" " << "Popped from top of stack"<<endl;
s.displayStack();
return 0;
}