-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeader.h
150 lines (144 loc) · 1.8 KB
/
Header.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
#pragma once
#include<iostream>
#include<string.h>
using namespace std;
template<class T>
void Reverse(Mystack<T> ST ,string& s)
{
int siz;
for(int i =0;s[i]!='\0';i++)
siz++;
Mystack<T> tmp;
for(int i=siz;i>=0;i--)
tmp.push(s[i]);
for(int j=0;j<siz;j++)
{
ST.push(tmp.Top());
tmp.pop();
}
}
template<class T>
bool isBalanced(Mystack<T> ST,string exp)
{
int siz;
for(int i =0;s[i]!='\0';i++)
siz++;
St.push(exp[0]);
for(int j=1;j<siz;j++)
{
if(ST.Top=='[')
{
if(exp[j]==']')
ST.pop;
else
ST.push(exp[j]);
}
else if(ST.Top=='{')
{
if(exp[j]=='}')
ST.pop;
else
ST.push(exp[j]);
}
else if(ST.Top=='(')
{
if(exp[j]==')')
ST.pop;
else
ST.push(exp[j]);
}
}
if(ST.isEmpty())
return true;
else
return false;
}
template<class T>
class Node
{
public:
T data;
Node<T>* next;
Node()
{
data = 0;
next = 0;
}
Node(T element)
{
data = element;
}
Node(Node* obj)
{
data = obj->data;
next = obj->next;
}
};
template<class T>
class Mystack
{
private:
Node<T>* top;
static int size;
public:
Mystack()
{
top = 0;
size = 0;
}
int Size()
{
return size;
}
bool isEmpty()
{
if (size == 0)
return true;
return false;
}
bool Top(T &e)
{
if (top)
{
e = top->data;
return true;
}
return false;
}
void Pop()
{
if (top)
{
Node<T>* tmp(top);
top = top->next;
delete tmp;
size--;
}
else
cout << "Stack is Empty " << endl;
}
void Push(T const& e)
{
if (top == 0)
{
Node<T>* node = new Node<T>(e);
top = node;
top->next = 0;
}
else
{
Node<T>* node = new Node<T>(e);
node->next = top;
top = node;
}
size++;
}
void Print()
{
Node<T>* i(top);
for (; i != 0; i = i->next)
cout << i->data << " ";
}
};
template<class T>
int Mystack<T> ::size = 0;