-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab9_Stack.h
198 lines (171 loc) · 2.35 KB
/
Lab9_Stack.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#pragma once
#include<iostream>
#include<fstream>
using namespace std;
//-------------------------------TASK1---------------------
template<class T1>
class node
{
private:
public:
T1 val;
node<T1>* next;
node(T1 val)
{
this->val = val;
next = NULL;
}
node()
{
this->val = 0;
next = NULL;
}
void setdata(T1 d) {
val = d;
}
T1 getData()
{
return val;
}
};
template<class T1>
class stack
{
public:
int size;
node<T1>* top;
stack()
{
size = -1;
top = NULL;
}
bool isEmpty()
{
if (top == NULL)
return true;
else
return false;
}
void push(T1 d)
{
node<T1>* temp = new node<T1>(d);
if (isEmpty())
{
top = temp;
size++;
}
else
temp->next=top;
top = temp;
size++;
}
void pop()
{
node<T1>* temp = new node<T1>();
if (!isEmpty())
{
temp = top;
top = top->next;
delete temp;
size--;
}
else
cout << "Stack is already empty";
}
node<T1>* Peek()
{
return top;
}
void clear()
{
while (isEmpty()!=true)
{
pop();
}
}
int sizeofstack()
{
return size;
}
~stack()
{
while (isEmpty() != true)
{
pop();
}
}
void print() {
node<T1>* temp;
temp = top;
while (temp)
{
cout << "The data of stack is : " << top->getData() << endl;
temp = temp->next;
}
}
};
//--------------------TASK3------------------
class MinStack
{
stack<int> s;
stack<int> aux;
public:
void push(int val)
{
if (s.isEmpty() && aux.isEmpty())
{
s.push(val);
aux.push(val);
}
else
s.push(val);
if(val<aux.top->getData())
aux.push(val);
}
void pop()
{
s.pop();
aux.pop();
}
int top()
{
return s.top->getData();
}
int size() {
return s.sizeofstack();
}
bool isEmpty() {
if (s.isEmpty())
{
return true;
}
else
return false;
}
int getMin() {
return aux.top->getData();
}
};
//--------------------TASK2---------------------
bool validate(string filename)
{
stack<char> s;
ifstream input;
input.open(filename);
char data;
while (input>>data) {
if (data == '{')
{
s.push(data);
}
else if (data == '}')
{
s.pop();
}
}
input.close();
if (s.isEmpty())
return true;
if (!s.isEmpty())
return false;
}