-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
71 lines (70 loc) · 1.51 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
#include<iostream>
using namespace std;
class Stack{
public:
int* arr;
int top;
int size;
Stack(int stackSize){
stackSize = size;
arr = new int[size];
top = -1;
}
void push(int data){
if(top==size-1){
cout<<"stack full \n";
return;
}
top++;
arr[top]=data;
}
void pop(){
if(top == -1){
cout<<"Stack empty \n";
return;
}
int data = arr[top];
top--;
}
int stackTop(){
if(top == -1){
cout<<"stack empty \n";
return -1;
}
return arr[top];
}
};
int main(){
int n;
int data;
Stack stk = Stack(5);
do
{
cout<<"1. PUSH\n";
cout<<"2. POP\n";
cout<<"3. TOP\n";
cout<<"enter number = ";
cin>>n;
switch(n){
case 1:{
cout<<"enter data : ";
cin>>data;
stk.push(data);
break;
}
case 2:{
stk.pop();
cout<<"data popped\n";
break;
}
case 3:{
cout<<"Data = "<<stk.stackTop()<<"\n";
break;
}
default:{
cout<<"wrong number \n";
break;
}
}
} while (n != 4);
}