-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_class.h
105 lines (96 loc) · 1.71 KB
/
stack_class.h
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
using namespace std;
#define MAX_SIZE 5
class my_stack {
int top;
int *stack_data;
public:
/*void init()
{
cout << "In top_init()" << endl;
top = -1;
stack_data = new int[MAX_SIZE];
}*/
my_stack();
~my_stack();
/*void exit()
{
cout << "In exit()" << endl;
top = 0;
delete [] stack_data;
}*/
int is_empty()
{
if (top == -1) {
cout << "In is_empty(): Satck is empty" << endl;
return 1;
}
return 0;
}
int is_full()
{
if (top == MAX_SIZE) {
cout << "In is_full(): Satck is full" << endl;
return 1;
}
return 0;
}
int push(const int& data)
{
cout << "In push():" << endl;
stack_data[++top] = data;
}
int pop()
{
cout << "In pop()" << endl;
return (stack_data[top--]);
}
};
/* Below is special type of intialization
* Only possible in constructure
*/
my_stack :: my_stack() : top (-1) //This is called initialization list
//my_stack::my_stack()
{
//top = -1;
stack_data = new int[MAX_SIZE];
cout << "In Default constructure" << endl;
}
my_stack :: ~my_stack()
{
cout << "In Destructure" << endl;
top = 0;
delete [] stack_data;
}
/*
int main()
{
my_stack stack;
int choice;
stack.init();
do {
cout << "1: For Insert data\n2: For delete data\n:0: For exit" << endl;
cin >> choice;
switch (choice) {
case 1: int data;
cout << "Insert value" << endl;
cin >> data;
if (!(stack.is_full())) {
stack.push(data);
} else {
cout << "stack is full. Please do pop" << endl;
}
break;
case 2: if (!(stack.is_empty())) {
cout << "data ====>" << stack.pop() << endl;
} else {
cout << "stack is empty. Please do push" << endl;
}
break;
case 0: stack.exit();
break;
}
} while (choice);
return 0;
}
*/