-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.cpp
54 lines (54 loc) · 1.01 KB
/
x.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
#include <iostream>
using namespace std;
class Stack{
int size;
int *a;
int top;
public:
Stack(int n){
top=-1;
size=n;
a=new int[n];
}
void push(int num){
if(top==size-1){
cout<<"Stack full"<<endl;
return;
}
top++;
a[top]=num;
}
int pop(){
if(top==-1){
cout<<"Stack empty"<<endl;
return -1;
}
int x=a[top];
top--;
return x;
}
int peek(){
if(top==-1){
cout<<"Stack empty"<<endl;
return -1;
}
return a[top];
}
~Stack(){
delete []a;
}
};
int main(){
Stack s(5);
s.push(1);
s.push(2);
s.push(4);
s.push(5);
s.push(6);
s.push(7);
// s.push(5);
cout<<s.peek()<<endl;
s.pop();
cout<<s.peek()<<endl;
return 0;
}