-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackAPI.cpp
More file actions
145 lines (97 loc) · 2.44 KB
/
StackAPI.cpp
File metadata and controls
145 lines (97 loc) · 2.44 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
using namespace std;
struct Stack {
int *item;
int top;
int size;
}s;
// Function prototype declaration:
void createStack();
void printInformations();
void push();
int pop();
int top();
void printStack();
void init(Stack *, int);
int main() {
bool temp = true;
while(temp){
// Decision coding starts here :
cout << "Enter your choice : " << endl;
cout << "*********************" << endl;
printInformations();
int choice;
cin >> choice;
cout << '\n';
// Decision coding ends here:
switch(choice){
case 0 : createStack();
break;
case 1 : printStack();
break;
case 2 : push();
break;
case 3 : cout << pop() << endl;
break;
case 4 : cout << top() << endl;
break;
default : temp = false;
break;
}
}
return 0;
}
void printInformations() {
cout << "0. Create a stack : " << endl;
cout << "1. Print Stack : " << endl;
cout << "2. Push an element to stack : " << endl;
cout << "3. Pop the stack : " << endl;
cout << "4. Top element : " << endl;
cout << "5. Exit the Stack : " << endl;
}
void init(Stack *sp, int size) {
sp->item = new int [size];
sp->top = -1;
sp->size = size;
cout << "A stack of " << size << " element has been initialized. " << endl;
}
void push(){
if(s.top == s.size-1){
cout << "Stack Overflow...." << endl;
return;
}
cout << "Enter the number to be pushed : " << endl;
int pushNumber;
cin >> pushNumber;
s.top++;
s.item[s.top] = pushNumber;
return;
}
int pop() {
if(s.top < 1){
cout << "Stack Underflows : " << endl;
}
int temp = s.item[s.top];
s.top--;
return temp;
}
int top() {
return s.top;
}
void printStack() {
if(s.top < 1){
cout << "Stack is Empty...Try again." << endl;
return;
}
int i;
for(i=0; i <= s.top; ++i){
cout << s.item[i] << endl;
}
return;
}
void createStack() {
cout << "Enter the size of stack you want : " << endl;
int size;
cin >> size;
init(&s,size);
}