forked from dharmanshu1921/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpStack.cpp
More file actions
50 lines (50 loc) · 659 Bytes
/
Copy pathimpStack.cpp
File metadata and controls
50 lines (50 loc) · 659 Bytes
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
#include <iostream>
using namespace std;
class Stack{
public:
int top;
int *arr;
int size;
Stack(int n){
top=-1;
arr=new int[n];
size=n;
}
void push(int val){
if(top>=size){
cout<<"Stack Overflow"<<endl;
return;
}
top++;
arr[top]=val;
}
void pop(){
if(top==-1){
cout<<"Stack Underflow"<<endl;
return;
}
top--;
}
void print(){
if(top==-1){
cout<<"Stack Underflow"<<endl;
return;
}
int k=top;
while(k>=0){
cout<<arr[k]<<" ";
k--;
}
}
};
int main(){
Stack s(5);
s.print();
s.push(5);
s.push(10);
s.print();
s.pop();
s.push(15);
s.print();
return 0;
}