Hey,

i know you are all probably very bored with the infix to postfix program and its many incarnations but i seem to be having a problem getting the linked list we have to use as a stack to work.
More specifically; pushing to the stack, reading from the top of the stack and deleting from the stack are all ok, the problem comes from trying to read the empty stack to check that its empty (should return a "#") somewhere the error checking is not working and i cant for the life of me find out where!!

header file:

#include<string>
#include<cstdlib>


using namespace std;


class symbolNode {
	private:	symbolNode *next;
				string* symbol;							// pointer to next chained item
	public:		symbolNode();
				symbolNode (string item);				// constructor for list_base object
				void setNext(symbolNode * item);
				symbolNode * getNext();
				string* getSymbol();							// get the next item pointer
}	;										// set next item pointer




class symbolList {
	private:	symbolNode * first;											// pointer to first chained item
				symbolNode * last;											// pointer to last chained item
	public:		symbolList(); 
				bool isEmpty();						// constructor for chain_base object
				void push(string item);								// push item onto list at end
				string top();	
				void pop();
};										// pop item off front of list

//class expression {

#endif

symbol.cpp:

#include <iostream>
#include <string>
#include "OOPCPP.h"

using namespace std;

symbolNode::symbolNode()
{
symbol = NULL;
next = NULL;
}

symbolNode::symbolNode(string item)
{
	symbol = new string(item);
	next = NULL;
}

symbolNode* symbolNode::getNext()
{
	return next;
}

void symbolNode::setNext(symbolNode* nextNode)
{
	next = nextNode;
}

string* symbolNode::getSymbol()
{
	return symbol;
}



symbolList::symbolList()
{
	first=NULL;
	last=NULL;
}

bool symbolList::isEmpty()
{
   return first==NULL;
}


void symbolList::push(string item)										// push item onto list at front
{
	symbolNode* current;
	current = new symbolNode(item);
	if (last=NULL)	
	{
		current->setNext(NULL);
	first = current;
	last = current;
	}												// this item is the last item
	else
	{											// otherwise

	current->setNext(first);
	first = current;
	}											// set this item as the new first																// return to caller
}

string symbolList::top( )
    {
		string ch;

		symbolNode* current;
		

       if (!isEmpty())//||(current!=NULL))
	{
		current = first;
		ch=*(current->getSymbol());
}
	   else { ch = "#";}
	   
       return ch;
    }

void symbolList::pop()
{
symbolNode* current;
current = first;
if (first = last)
{
	first = NULL;
	last = NULL;
}
else
{
	first = current->getNext();
	first->setNext(first->getNext());
}
delete current;
}

program.cpp:

#include<iostream>
#include<string>
#include<cstdlib>
#include "OOPCPP.h"


using namespace std;




 
int main() {
	symbolList stack;
	string out;


stack.push("a");
system ("pause");
stack.push("b");
system ("pause");
stack.push("c");
system ("pause");
stack.push("d");
system ("pause");
out.append(stack.top());
system("pause");
stack.pop();
system("pause");
out.append(stack.top());
system ("pause");
stack.pop();
system("pause");
out.append(stack.top());
system ("pause");
stack.pop();
system("pause");
out.append(stack.top());
system ("pause");
stack.pop();
system("pause");
out.append(stack.top());
stack.pop();
system("pause");

cout<<out;
 system("pause");
	return 0;
 
}

it is trying to getNext a null Next though i suspect it is one of the pop/top functions that is the problem. any idea?

many thanks.

This article has been dead for over six months. Start a new discussion instead.