Push Back() and Pop Back() on a link list

I've the concept of this down and by appearances it looks like my code should work(at least the debugger shows that it should work when I run it). However when I run it, the original list is shown without the new nodes on the back of the list. Here's my push_back()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void List::push_back(Node* p)
	{
		if(lastNode == nullptr)
		{
			firstNode = p;
			lastNode = p;
		}
		else
		{
			p->setNextNode(lastNode);
			lastNode = p;
		}
                numberOfNodes++;
	}

I can tell that my nodes are increasing to 7 and when I pop the back off, they decrease to six. Advice would be appreciative.
Last edited on
I think you have line 10 backwards. Perhaps it should be:
 
lastNode->setNextNode(p);
Looks like your not setting the node before lastNode next to p.
Thanks that fixed it. Didn't even realize I had it backwards there.
I probably have this written completely wrong but I'm working on a pop_back() so that I can remove the last node from the list. This is what is required for the function
A pop_back function that removes the last node from the list, and returns a pointer to this node. Important, do not delete the node in this function. Simply unlink it from the list.

I have this written:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Node* List::pop_back()
{
	Node* saveLast = nullptr;
	if( numberOfNodes == 0 )
	{
		return nullptr;
	}
	else if(numberOfNodes == 1)
	{
		saveLast = lastNode;
		lastNode->setNextNode(NULL);
		numberOfNodes--;
		return saveLast;
	}
	else
	{
		saveLast = lastNode;
		lastNode->setNextNode(NULL);
		numberOfNodes--;
		return saveLast;
	}
}

I get the idea of a new pointer pointing to the last node and setting the original pointer to NULL. I'd appreciate it if someone could help me out here.

Also does anyone know of a good forum to post help ?s to for LC-3 programming?
Topic archived. No new replies allowed.