I am trying to create a program which evaluates a stack of brackets for their correct implementation (i.e. if a bracket has opening and closing components, the bracket is valid, therefore it returns "true"). For example [()]{}{()()} is true while [({}) is false.
Currently, I am about to finish the program, although, the logical operator for stack evaluation does not work quite right.
I program in Xcode, and the problem can be related specifically for the IDE, but it is unlikely. The issue: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
The program is not complete. I am about to insert a string of brackets as an input, and use a recursion for the evaluation of stack, in order to delete all the elements in a stack, if the bracket logic is correct, which in this case ("[()]{}{()()}") is correct. The desired logic is below:
s.evaluate -> []{[]}
s.evaluate -> {}
s.evaluate -> true
#include<iostream>
using namespace std;
struct node
{
char bracket;
node* next;
};
class stack
{
node* top;
public:
// constructure
stack()
{
top = NULL;
}
void push(char bracket); // to insert an element
void pop(); // to delete an element
void evaluate(); // to evaluate the stack for brackets' logic
void show(); // to show the stack
bool isPair(node* n2, node* n1) {
if ((n1->bracket == '(' && n2->bracket == ')') || (n1->bracket == '[' && n2->bracket == ']') || (n1->bracket == '{' && n2->bracket == '}')) {
return true;
} else {
return false;
}
}
};
// insert an element
void stack::push(char bracket)
{
char value = bracket;
node* ptr;
ptr = new node;
ptr->bracket = value;
ptr->next = NULL;
if (top != NULL)
ptr->next = top;
top = ptr;
}
// delete an element
void stack::pop()
{
node* temp;
if (top == NULL)
{
cout << "\nThe stack is empty.";
}
temp = top;
top = top->next;
cout << "\nPOP Operation" << endl << "Poped value is " << temp->bracket;
delete temp;
}
// evaluate a stack for bracket logic
void stack::evaluate()
{
node* target = top;
node* targetNext = target->next;
node* temp;
node* tempNext;
if (target == NULL)
{
cout << "\nTrue" << endl;
}
while (target != NULL)
{
if (isPair(targetNext, target)) {
temp = targetNext->next;
tempNext = temp->next;
cout << target->bracket << " and " << targetNext->bracket << " are deleted" << endl;
delete target;
delete targetNext;
target = temp;
targetNext = tempNext;
} else {
target = target->next;
targetNext = target->next;
}
}
}
// Show stack
void stack::show()
{
node* ptr1 = top;
cout<<"\nThe stack is\n";
while(ptr1 != NULL)
{
cout << ptr1->bracket << " -> ";
ptr1 = ptr1->next;
}
cout << "NULL\n";
}
// Main function
int main()
{
stack s1;
string brackets = "[()]{}{[()()]()}";
for(char& c : brackets) {
s1.push(c);
}
s1.show();
s1.evaluate();
s1.show();
return 0;
}
For now, I concentrate on the if logic part. I have changed the notation from:
bool isPair(node* n2, node* n1) {
if ((n1->bracket == '(' && n2->bracket == ')') || (n1->bracket == '[' && n2->bracket == ']') || (n1->bracket == '{' && n2->bracket == '}')) {
return true;
} else {
return false;
}
}
to:
bool isPair(node* n2, node* n1) {
if (n1->bracket == '(' && n2->bracket == ')')
{
return true;
}
else if (n1->bracket == '[' && n2->bracket == ']')
{
return true;
}
else if (n1->bracket == '{' && n2->bracket == '}')
{
return true;
}
else
{
return false;
}
}
But it did not change anything (as expected). Why Xcode do not want to proceed my code fully?
Current output is:
The stack is
} -> ) -> ( -> ] -> ) -> ( -> ) -> ( -> [ -> { -> } -> { -> ] -> ) -> ( -> [ -> NULL
( and ) are deleted
{ and } are deleted
(lldb)
I know, the code is very noobie, I am new to C++ and data structures, although, your help is utterly appreciated!