Have you tried the offscreen approach (don't worry if you don't know the term, I just made it up)? Write a flow chart for your program to find out where you go wrong, it's 4 am and I don't really feel like debugging right now...
#include <iostream>
#include <typeinfo>
#include <cstdlib>
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.
struct node // Each node has two fields:
{
char data; // a data field,
node *next; // and a pointer field.
};
class stack
{
private:
node *head; // Pointer to the first cell.
public:
stack();
void push(char item);
bool pop();
void search();
void view();
bool empty();
bool full();
};
stack::stack()
{
/* node<type>* temp=new node<type>;
temp-> data;
temp-> next=head;
*/
}
void stack :: push (char item)
{
node *t = new node;
node *tmp = new node;
node *trail = new node;
trail->next = NULL;
tmp->next=NULL;
t->next=NULL;
if(head == NULL) //Startig the list
{
t-> data = item;
t -> next = head;
head = t;
}
elseif(head != NULL)
{
t = head;
while(t != NULL){ //Puts inside the list
trail = t;
t = t->next;
}
tmp->data = item;
tmp->next = t;
trail->next = tmp;
}
}
// Function to remove the first element from the stack and
// return it to the caller.
bool stack :: pop ()
{
node* cur;
cur = head;
if(cur == NULL)
{
returnfalse;
}
else
{
head = cur -> next;
delete cur;
returntrue;
}
}
// Accessor functions.
void stack :: search ()
{
int tmp=0;
int tmp2=0;
int tmp3=0;
int tmp4=0;
node* cur=head;
if(cur->next==NULL)
cout<<"You do not have a problem"<<endl;
if(cur!=NULL)do
{
cur=cur -> next;
if(cur-> data == '(')
tmp++;
if(cur -> data == ')')
tmp2++;
if(cur-> data == '{')
tmp3++;
if(cur -> data == '}')
tmp4++;
}
while(cur != NULL);
if (tmp == tmp2)
cout << "The operation has the correct amount of parenthesis." << endl;
else
cout << "You do not have the appropriate number of parenthesis." << endl;
if (tmp3 == tmp4)
cout << "The operation has the correct amount of braces." << endl;
else
cout << "You do not have the appropriate number of braces." << endl;
if(tmp==0 && tmp2==0 && tmp3==0 && tmp4==0)
cout<<"There is proper grouping"<<endl;
}
// Function to output the list for viewing. Assumes the
// data type is compatible with cout << .
void stack :: view ()
{
node* tmp = head;
while(tmp != NULL){
cout << tmp -> data;
tmp = tmp -> next;
}
}
// Is the list empty?
bool stack :: empty ()
{
if(head == NULL)
returntrue;
elsereturnfalse;
}
// Is the list full?
bool stack :: full ()
{
returnfalse;
}
int main()
{
stack b;
b.push('(');
b.push('8');
b.push('*');
b.push('9');
b.push('+');
b.view();
b.search();
}
yea i have been doing that on my white board... i cant find anything because other then the new if statements that i added its the same thing as my last program which works fine.
the segmentation fault i am getting is in the search function. what should the search function look like? or does that look right? the fault i am getting is line 109
sorth, have you tried moving the cur = cur-> next right above the } while ? cause the way it is right now it will inevitably cause segmentation faults.
if(cur!=NULL)do
{
if(cur-> data == '(')
tmp++;
if(cur -> data == ')')
tmp2++;
if(cur-> data == '{')
tmp3++;
if(cur -> data == '}')
tmp4++;
cur=cur -> next;
}
while(cur != NULL);
there? In that order?
@anderson... not you too. Char comparisons have nothing to do with segmentation faults at all. The point is that's the first time we are dereferencing cur there, so the fault is probably either in that function (which I doubt by now) or at some other point (probably in the push function, you did some really weird things there).