Skip to content

Commit be47af2

Browse files
authored
O(N)
1 parent 5d789ec commit be47af2

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed
Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,130 @@
11
#include <iostream>
2-
#include<cstring>
2+
#include<cstring> //for applying the string function
33
using namespace std;
44

5-
class Node {
5+
class Node { // structure for stack using linkedlist
66
public:
7-
char data;
8-
Node* next;
7+
char data; // data part of the node
8+
Node* next; // pointer to the next node
99
};
1010

1111
class Stack {
1212
private:
1313
Node* top;
1414
public:
15-
Stack();
16-
~Stack();
17-
void push(char x);
18-
char pop();
19-
char peek(int index);
20-
int isEmpty();
21-
int isFull();
22-
char stackTop();
15+
Stack(); // non parametrized constructor
16+
~Stack(); // destructor
17+
void push(char x); // declaration of push operation
18+
char pop(); // declaration of the pop operation
19+
char peek(int index); // declaration for getting the element at an given index
20+
int isEmpty(); // declaration for checking the stack empty codition
21+
int isFull(); // declaration for checking the stack full condition
22+
char stackTop(); // declaration for getting the topmost value of the stack
2323
};
2424

2525
Stack::Stack() {
26-
top = nullptr;
26+
top = nullptr; // initilize top pointer as null
2727
}
2828

2929
Stack::~Stack() {
30-
Node* p = top;
30+
Node* p = top; // create a new pointer p and make it point on top
3131
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
3535
}
3636
}
3737

38-
void Stack::push(char x) {
39-
Node* t = new Node;
40-
if (t == nullptr) {
41-
cout << "Stack Overflow!" << endl;
38+
void Stack::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
4242
}
4343
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
4747
}
4848
}
4949

50-
char Stack::pop() {
51-
Node* p;
52-
char x = -1;
53-
if (top == nullptr) {
54-
cout << "Stack Underflow!" << endl;
50+
char Stack::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
5555
}
5656
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
6161
}
62-
return x;
62+
return x; // return the value which has deleted
6363
}
6464

65-
int Stack::isFull() {
66-
Node* t = new Node;
67-
int r = t ? 1 : 0;
68-
delete t;
69-
return r;
65+
int Stack::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
7070
}
7171

72-
int Stack::isEmpty() {
73-
return top ? 0 : 1;
72+
int Stack::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
7474
}
7575

76-
char Stack::stackTop() {
77-
if (top) {
78-
return top->data;
76+
char Stack::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
7979
}
8080
return -1;
8181
}
8282

83-
char Stack::peek(int index) {
84-
if (isEmpty()) {
83+
char Stack::peek(int index) { // function for taking the element at an particular index
84+
if (isEmpty()) { // if stack is empty then return the value -1
8585
return -1;
8686
}
8787
else {
88-
Node* p = top;
88+
Node* p = top; // create a new pointer p and make move that p onto the top
8989

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
9292
}
9393

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
9696
}
9797
else {
98-
return -1;
98+
return -1; // else return -1 for stack empty
9999
}
100100
}
101101
}
102102

103103

104104
int isBalanced(char* exp) {
105-
Stack stk;
105+
Stack stk; // creating a local varible of stack type
106106

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
110110
}
111-
else if (exp[i] == ')') {
112-
if (stk.isEmpty()) {
111+
else if (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
113113
return false;
114114
}
115115
else {
116-
stk.pop();
116+
stk.pop(); // pop out the closing parenthesis
117117
}
118118
}
119119
}
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
121121
}
122122

123123

124124
int main() {
125125

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
128128

129129
char F[] = "((a+b)*(c-d)))";
130130
cout << isBalanced(F) << endl;
@@ -134,4 +134,4 @@ int main() {
134134

135135
return 0;
136136

137-
}
137+
}

0 commit comments

Comments
 (0)