You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<cstring>//for applying the string function
3
3
usingnamespacestd;
4
4
5
-
classNode {
5
+
classNode {// structure for stack using linkedlist
6
6
public:
7
-
char data;
8
-
Node* next;
7
+
char data;// data part of the node
8
+
Node* next;// pointer to the next node
9
9
};
10
10
11
11
classStack {
12
12
private:
13
13
Node* top;
14
14
public:
15
-
Stack();
16
-
~Stack();
17
-
voidpush(char x);
18
-
charpop();
19
-
charpeek(int index);
20
-
intisEmpty();
21
-
intisFull();
22
-
charstackTop();
15
+
Stack();// non parametrized constructor
16
+
~Stack();// destructor
17
+
voidpush(char x);// declaration of push operation
18
+
charpop();// declaration of the pop operation
19
+
charpeek(int index);// declaration for getting the element at an given index
20
+
intisEmpty();// declaration for checking the stack empty codition
21
+
intisFull();// declaration for checking the stack full condition
22
+
charstackTop();// declaration for getting the topmost value of the stack
23
23
};
24
24
25
25
Stack::Stack() {
26
-
top = nullptr;
26
+
top = nullptr;// initilize top pointer as null
27
27
}
28
28
29
29
Stack::~Stack() {
30
-
Node* p = top;
30
+
Node* p = top;// create a new pointer p and make it point on top
31
31
while (top) {
32
-
top = top->next;
33
-
delete p;
34
-
p = top;
32
+
top = top->next;// everytime move top to next
33
+
delete p;// free p
34
+
p = top;// move p on the top
35
35
}
36
36
}
37
37
38
-
voidStack::push(char x) {
39
-
Node* t = new Node;
40
-
if (t == nullptr) {
41
-
cout << "Stack Overflow!" << endl;
38
+
voidStack::push(char x) {// function for pushing element in the stack
39
+
Node* t = new Node;// creating a new node
40
+
if (t == nullptr) {// if node is not created // i.e if heap memory is full
41
+
cout << "Stack Overflow!" << endl;// print that stack is overflow
42
42
}
43
43
else {
44
-
t->data = x;
45
-
t->next = top;
46
-
top = t;
44
+
t->data = x;// fill the node data part with data
45
+
t->next = top;// point the next of the node to the top of the stack
46
+
top = t;// and move top to the newly created node
47
47
}
48
48
}
49
49
50
-
charStack::pop() {
51
-
Node* p;
52
-
char x = -1;
53
-
if (top == nullptr) {
54
-
cout << "Stack Underflow!" << endl;
50
+
charStack::pop() {// function for deleting the element from the stack
51
+
Node* p;// ccreate a new node
52
+
char x = -1;// assign x with -1;
53
+
if (top == nullptr) {// if top is null// i.e stack is empty
54
+
cout << "Stack Underflow!" << endl;// print that stack is underflow
55
55
}
56
56
else {
57
-
p = top;
58
-
x = p->data;
59
-
top = top->next;
60
-
delete p;
57
+
p = top;// move p on to the top
58
+
x = p->data;// take out the data from top of the stack
59
+
top = top->next;// everytime move top its next
60
+
delete p;// at last free the space of p
61
61
}
62
-
return x;
62
+
return x;// return the value which has deleted
63
63
}
64
64
65
-
intStack::isFull() {
66
-
Node* t = new Node;
67
-
int r = t ? 1 : 0;
68
-
delete t;
69
-
return r;
65
+
intStack::isFull() {// function for checking the stack is full or not
66
+
Node* t = new Node;// creating a new node
67
+
int r = t ? 1 : 0;// if new node is created then stack is not full else stack is full
68
+
delete t;// free space consumed by the new node
69
+
return r;// return the condition
70
70
}
71
71
72
-
intStack::isEmpty() {
73
-
return top ? 0 : 1;
72
+
intStack::isEmpty() {// checking either stack is empty or not
73
+
return top ? 0 : 1;// if top is pointing to null the stack is empty else stack is full
74
74
}
75
75
76
-
charStack::stackTop() {
77
-
if (top) {
78
-
return top->data;
76
+
charStack::stackTop() {// returning the topmost element of the stack
77
+
if (top) {// if top is there
78
+
return top->data;// then return the value in the top pointer
79
79
}
80
80
return -1;
81
81
}
82
82
83
-
charStack::peek(int index) {
84
-
if (isEmpty()) {
83
+
charStack::peek(int index) {// function for taking the element at an particular index
84
+
if (isEmpty()) {// if stack is empty then return the value -1
85
85
return -1;
86
86
}
87
87
else {
88
-
Node* p = top;
88
+
Node* p = top;// create a new pointer p and make move that p onto the top
89
89
90
-
for (int i = 0; p != nullptr && i < index - 1; i++) {
91
-
p = p->next;
90
+
for (int i = 0; p != nullptr && i < index - 1; i++) {// iterating through the stack
91
+
p = p->next;// everytime move p to next
92
92
}
93
93
94
-
if (p != nullptr) {
95
-
return p->data;
94
+
if (p != nullptr) {// if p is not null then
95
+
return p->data;// return the value of that node
96
96
}
97
97
else {
98
-
return -1;
98
+
return -1;// else return -1 for stack empty
99
99
}
100
100
}
101
101
}
102
102
103
103
104
104
intisBalanced(char* exp) {
105
-
Stack stk;
105
+
Stack stk;// creating a local varible of stack type
106
106
107
-
for (int i = 0; i < strlen(exp); i++) {
108
-
if (exp[i] == '(') {
109
-
stk.push(exp[i]);
107
+
for (int i = 0; i < strlen(exp); i++) {// iterating throuhout the stack
108
+
if (exp[i] == '(') {// if there is starting parenthisis is there then
109
+
stk.push(exp[i]);// push that to the stack
110
110
}
111
-
elseif (exp[i] == ')') {
112
-
if (stk.isEmpty()) {
111
+
elseif (exp[i] == ')') {// whereas if closing parenthesis is there
112
+
if (stk.isEmpty()) {// check either stack is empty or not if stack is empty then return false as expression is mot balenced
113
113
returnfalse;
114
114
}
115
115
else {
116
-
stk.pop();
116
+
stk.pop();// pop out the closing parenthesis
117
117
}
118
118
}
119
119
}
120
-
return stk.isEmpty() ? true : false;
120
+
return stk.isEmpty() ? true : false;// if at last the stack is empty then return true for expression is balanced whereas false for expression is not balanced
121
121
}
122
122
123
123
124
124
intmain() {
125
125
126
-
char E[] = "((a+b)*(c-d))";
127
-
cout << isBalanced(E) << endl;
126
+
char E[] = "((a+b)*(c-d))";// expression to be evaluated
127
+
cout << isBalanced(E) << endl;// calling the function
0 commit comments