-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackListSTL.cpp
More file actions
126 lines (117 loc) · 2.48 KB
/
Copy pathStackListSTL.cpp
File metadata and controls
126 lines (117 loc) · 2.48 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
#include <iostream>
#include <list>
using namespace std;
template <class T>
class LLStack
{
public:
LLStack() {}
void clear() { lst.clear(); }
bool isEmpty() const { return lst.empty(); }
T &peek() { return lst.back(); }
T pop()
{
T el = lst.back();
lst.pop_back();
return el;
}
void push(const T &el)
{
lst.push_back(el);
}
void display()
{
list<int>::iterator itr;
for (itr = lst.begin(); itr != lst.end(); ++itr)
cout << *itr << " ";
cout << endl;
}
void display1()
{
list<int>::iterator itr;
for (itr = lst.end(); itr != lst.begin(); itr--)
cout << *itr << " ";
cout << endl;
}
private:
list<T> lst;
};
char matchingBracket(char b1, char b2)
{
if (b1 == '(' && b2 == ')')
return true;
if (b1 == '[' && b2 == ']')
return true;
if (b1 == '{' && b2 == '}')
return true;
return false;
}
bool delimiterMatching(string str)
{
LLStack<char> S;
int i = 0;
char ch = str[i++];
while (ch != '\0')
{
cout << "Charachter read: " << ch << endl;
if (ch == '(' || ch == '[' || ch == '{')
S.push(ch);
else if (ch == ')' || ch == ']' || ch == '}')
if (!matchingBracket(S.pop(), ch))
return false;
ch = str[i++];
}
if (S.isEmpty())
return true;
else
return false;
}
void addLargeNumbers(string num1, string num2)
{
LLStack<int> S1, S2, S3;
int i = 0, carry = 0;
while (num1[i] != '\0')
S1.push(num1[i++] - '0');
i = 0;
while (num2[i] != '\0')
S2.push(num2[i++] - '0');
S1.display();
S2.display();
int n1 = 0, n2 = 0;
while (!S1.isEmpty() || !S2.isEmpty())
{
n1 = 0, n2 = 0;
if (!S1.isEmpty())
n1 = S1.pop();
if (!S2.isEmpty())
n2 = S2.pop();
carry = carry + n1 + n2;
S3.push(carry % 10);
carry = carry / 10;
}
if (carry != 0)
{
S3.push(carry);
S3.display1();
}
}
int main()
{
addLargeNumbers("592", "978");
}
/*
int main() {
if (delimiterMatching("while (m < (n[8] + o)) { p = 7; r = 6; }"))
cout<< "Valid expression";
else cout << "Invalid expression";
return 0;
}
*/
/*int main()
{
LLStack<int> S;
S.push(1); S.push(2); S.push(3); S.push(4);
cout << S.peek() <<endl;
S.pop(); S.pop();
S.display();
}*/