i have a program going that is supposed to take an equation say (8+9) and tell weather it has the proper grouping. Where as (8+9} is not proper nor is {(8+9}).
my pseudo code for the last part i need is
if(array[i] != to a corresponding right symbol) then break out of the loop.
i just don't really know exactly how to code this part
here is what i have:
As to that question: You would change the return value to type, create a new local type variable, assign *head to it, and return it at the end of the function after deleting the old head.
As to the other one, if you encounter an ')', you check whether pop returns '(', if you encounter a '}' you do the same thing with '{'.
Oh, and this forum has an edit button. Please don't quadruple post, it's not forbidden but it's very annoying.
How does this work? If pop is implemented properly (which it isn't... you are causing memory leaks again...) this would crash your program with a pretty much 100% chance. Or rather than crash, it would pretty much give you that result. Please look at your pop function again and look at what you do in your code, and figure out for yourself what you are doing wrong. Think about what pop does, and what the return statement does.
it is supposed to add the '(' or '{' to the stack when it encounters one. and then when it encounters the closing form of those it will pop from the stack and if what it is popping is the corresponding closing brace then the grouping is correct so for example
({})
would push the first ( and the { to the stack then when it encounters the closing it will pop it off so then it reaches } and should pop { but what i have now the push function adds from the back but i changed it to push from the beginning so the stack looks like {( after words
at the moment my program works with using () or {} but once i mix the two for ex. ({}) it doesn't work
in the pop function for some reason its always saying cur is null even when i know im putting something in so i just put a cout; for now just to get rid of the extra output
#include <iomanip>
#include <iostream>
#include <typeinfo>
#include <cstdlib>
#include <string>
usingnamespace std;
// Specification file for a linked list abstract data type class.
// This linked list is actually a stack, since all additions and
// removals are at the head.
template<class type>
struct node // Each node has two fields:
{
type data; // a data field,
node<type> *next; // and a pointer field.
};
template<class type>
class stack
{
private:
node<type> *head; // Pointer to the first cell.
public:
stack();
void push(type item);
type pop();
void view();
};
template<class type>
stack<type>::stack()
{
head=NULL;
}
template<class type>
void stack<type> :: push (type item)
{
node<type> *t = new node<type>;
t -> data = item;
t -> next = head;
head = t;
}
// Function to remove the first element from the stack and
// return it to the caller.
template<class type>
type stack<type> :: pop ()
{
node<type>* cur;
cur = head;
if(cur == NULL)
{
cout;
}
else
{
head = cur -> next;
return cur->data;
delete cur;
}
}
// Accessor functions.
// Function to see whether an element is on the list.
template<class type>
void stack<type> :: view ()
{
node<type>* tmp = head;
while(tmp != NULL){
cout << tmp -> data;
tmp = tmp -> next;
}
cout<<endl;
}
int main()
{
char array[50];
cout<<"Enter your function"<<endl;
cin>>array;
int i=0;
stack<char> b;
while(array[i] != '\0'){
if(array[i] == '(')
b.push('(');
elseif(array[i] == ')')
b.pop();
if(array[i]==')' && b.pop()=='(')
{
cout<<"correct grouping"<<endl;
}
elseif(array[i]==')' && b.pop() != '('){
cout<<"Incorrect grouping"<<endl;
break;
}
if(array[i] == '{')
b.push('{');
elseif(array[i] == '}')
b.pop();
if(array[i]=='}' && b.pop()=='{')
{
cout<<"correct grouping"<<endl;
}
elseif(array[i]=='}' && b.pop() != '{')
{
cout<<"Incorrect grouping"<<endl;
break;
}
i++;
}
b.view();
system("PAUSE");
}
i keep writing it down on paper and it seems to work fine but its not working