Mihail Burduja
BAN USERPassionate about coding...
You can use a stack.
Stack *s = new Stack();
for (int i = 0; i < strlen(str): i++)
{
if ((str[i] == '{') || (str[i] == '[') || (str[i] == '('))
s->push(str[i]);
else
{
char tp = s->pop(); // get the element at top
if (tp == '{') && (sir[i] != '}')
return false;
if (tp == '[') && (sir[i] != ']')
return false;
if (tp == '[') && (sir[i] != ']')
return false;
}
if (s->empty())
return true;
return false;
}
The code doesn't work.
1. newNode doesn't have a value
2. after the while loop temp holds the first node bigger than the node you're inserting, so you're inserting the node in the wrong position
void insert(node* &root, int x)
{
node *start = root;
node *n = new node(x);
if (!root)
{
root = n;
return;
}
if (root->info > x)
{
n->next = root;
root = n;
return;
}
while ((start->next) && (start->next->info < x))
{
start = start->next;
}
n->next = start->next;
start->next = n;
}
Fixed it!
- Mihail Burduja January 24, 2013