-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_Sir_method.cpp
80 lines (69 loc) · 1.24 KB
/
stack_Sir_method.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
#include<iostream>
#define size 10
class Stack{
int top;
int s[size];
public:
Stack();
void push(int val);
int pop();
bool is_full();
bool is_empty();
};
Stack::Stack()
{
top = -1;
}
bool Stack::is_empty()
{
if(top < 0)
return true;
return false;
}
bool Stack::is_full()
{
if(size == top+1)
return true;
return false;
}
void Stack::push(int val)
{
if(is_full())
throw "Stack Overflow ";
s[++top] = val;
}
int Stack::pop()
{
if(is_empty())
throw "Stack Underflow ";
return s[top--];
}
int main()
{
Stack s;
try{
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
s.push(7);
s.push(8);
s.push(9);
s.push(10);
std::cout << s.pop() << " Popped " << std::endl;
std::cout << s.pop() << " Popped " << std::endl;
std::cout << s.pop() << " Popped " << std::endl;
std::cout << s.pop() << " Popped " << std::endl;
std::cout << s.pop() << " Popped " << std::endl;
s.push(100);
std::cout << s.pop() << " Popped " << std::endl;
std::cout << s.pop() << " Popped " << std::endl;
std::cout << s.pop() << " Popped " << std::endl;
std::cout << s.pop() << " Popped " << std::endl;
std::cout << s.pop() << " Popped " << std::endl;
}catch(const char* msg){
std::cout << msg;
}
}