-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.cpp
54 lines (44 loc) · 867 Bytes
/
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
//Stack is a linear data structure which follows a particular order in which the operations are performed.
//Main operations:-
//Push
//Pop
//Peek or Top
#include <iostream>
#define MAX 1000
int top;
int a[MAX];
int pop(){
if (top<0){
std::cout<<"stack empty"<<std::endl;
}
else{
int x= a[top--];
return x;
}
}
int peek(){
int x= a[top];
return x;
}
bool push(int x){
if(top==MAX-1){
std::cout<<"overflow"<<std::endl;
return false;
}
else{
a[++top]=x;
std::cout<<x<<" pushed"<<std::endl;
return true;
}
}
int main()
{
push(10);
push(20);
push(30);
std::cout << peek() << " peek"<<std::endl;
std::cout << pop() << " popped"<<std::endl;
std::cout << pop() << " popped"<<std::endl;
std::cout << peek() << " peek"<<std::endl;
return 0;
}