I've been working for a while now on a module assesment which is due in tomorrow. The trouble I have is that I have an extension, the module has finished and there is no one in my school to ask since it's been the Easter holiday :( Instead of 'borrowing' a program from the internet I wanted to make mine from scratch, because of this I've had trouble using the information currently on the web. Therefore if there is anyone around who would be willing to take the time to actually read my current implementation and assist I would greatly appreciate it.

My current Push function:

Symbol* Stack::push(string name)
{
	Symbol* current;
	current = new Symbol(name);

	cout << "Adding symbol to stack." << name << endl;
	if (previous == NULL)
	{
		start = current;
		end = current;
		previous = current;
	}
	else
	{
		current->setNext(previous);
		start = current;
		previous = current;
	}
	Symbol* first = new Symbol();
	first = current;
	return first;
}

(please if the start, end and previous do not make perfect sense do not worry about this, it works - i'm satisfied with that part! XD)

This is my POP function - it's rubbish and I can't get my head around what I need to do to make sure the correct LIFO node is at the top after the top node is popped. (I was trying for ages to pass an object or a pointer to an object but that led me to code I couldn't understand.)

string Stack::pop(Stack* StackP)
{
	string s;
	s = *(StackP->first->getName());
	delete &StackP->first;
	StackP->first = StackP->start->getNext();
	return s;
}

What this has left me with is the following current implementation within my Expression class (which holds the infix and postfix strings):

string Expression::convertToPostfix(string s)
{
	Stack stackTEMP;
	int i;
	int inL = infix.length();
	int j = 0;
	string tempPostfix;

	for(i=0; i<=inL; i++)
	{//deal with it as a string or a char?
		if(infix[i] !=  '+' || '-' || '*' || '/' || '(' || ')')
		{
			postfix[j]=infix[i];
			postfix[j]++;
		}

		if(infix[i] == '(')
		{
			stringstream ss1;
			string s1;
			ss1 << infix[i];
			ss1 >> s1;
			stackTEMP.push(s1);
		}

		if(infix[i] == ')')
		{
			stringstream ss2;
			string s2;
			ss2 << infix[i];
			ss2 >> s2;
			stackTEMP.push(s2);
		}
	}
	return tempPostfix;
	//delete &stackTEMP;
}

I'm sorry for the length but I figured if anyone was willing to help they'd want the whole shebang!

As it stands i've only got about 3 months experience of c++ (which felt to me like a ridiculous amount of time to try and teach Object Oriented c++ at university, but hey i'm not that experienced with programming so maybe it's not so bad!) so if you fancy helping, please try and keep it simple, i'm a massive noob! thanks to anyone who is still reading!! :D

Recommended Answers

All 2 Replies

I have some observations about the pop function.... May or may not apply to you

Say you have 1,2,3,4,5 in the stack with the 5 being at the top. If I wanted to do a pop operation I would:

1. Copy the variable 5 in a temp variable. (Check for underflow)
2. Currently the stack pointer should be pointing to 5. I would store the memory address of 5 in a temporary location.
3. I would move the stack pointer to 4.
4. Delete the memory being used by the 5.
5. Return the temp variable being used to store the value 5

As always use printf / cout to narrow down your error

I have some observations about the pop function.... May or may not apply to you

Say you have 1,2,3,4,5 in the stack with the 5 being at the top. If I wanted to do a pop operation I would:

1. Copy the variable 5 in a temp variable. (Check for underflow)
2. Currently the stack pointer should be pointing to 5. I would store the memory address of 5 in a temporary location.
3. I would move the stack pointer to 4.
4. Delete the memory being used by the 5.
5. Return the temp variable being used to store the value 5

As always use printf / cout to narrow down your error

Ok thanks, i'll follow those clear steps and see, thanks for replying.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.