Deleting last Linked list node?

I have to write a complete function for the CS class that will do the following:
Accept a pointer called FirstRoom by reference and remove the last room on the list.

1. If the list is empty, a message needs to say that and return from the function.
2. If the list has 1 room, the head is pointing to that room and it needs to be removed.
3. If the list has multiple rooms I need to work down the list until I reach the final room and remove it.

Here's what I've got so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int RemoveRoom (Room *FirstRoom)
{
   if(!head)
      {
         head = newNode;
         cout << "Sorry, no rooms left on the list";
       }

    while (nodeptr)
      {
        nodeptr = delptr;
        nodeptr = nodeptr -> next;
       }

    delete delptr; 
    delptr = NULL;

}



Would this work? I'm new to pointers and linked list so I'm not sure on how to make this right.

Thanks for any help.
There are many compile errors in the above code due to things not being declared (or maybe they are global, which means it is impossible to answer your question). Also, line 5 is surely semantically incorrect. Line 11 is probably also wrong.
Were just supposed to be writing the function, not an entire program.

What would you suggest that I do to make it function with the above requirements?
I've been referencing the Reference section here and some other threads but I can't seem to fully figure this out yet.
Well, I don't know what head, newNode, nodeptr, or delptr is because none of them are declared in the function.
For that matter, the parameter isn't even used.

I believe you are supposed to take FirstRoom as "head".
I believe you are supposed to take FirstRoom as "head".

It sounds like it. Then you check it for null. If it's null, print the error message (and return 0?). If it isn't, keep checking the next one until you hit a null, then remove it (the node where node->next == NULL).

I don't see why the assignment doesn't ask you to implement the linked list completely, though. It's a very simple task, and if you can handle something like removing a link, you probably understand enough to implement the list.
Last edited on
So I'm thinking something like this then? I know nothing is declared.
nodePtr is the current node
delPtr is the last node on the list (the one that's supposed to be deleted)
The head is (what I think points to the very first node)

Again I'm very bad at this, I emailed my teacher and he didn't respond surprise surprise.It's just a homework assignment but it's due tomorrow and I'd like to get a good grade on it but I'd also like to know how to do it. I probably learn more here than I do in class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int RemoveRoom (Room *FirstRoom)
{

FirstRoom *nodePtr, *delptr;
  
   if(nodePtr == head)
      {
         delptr = delptr;
         delete delptr;
         cout << "Sorry, no rooms left on the list";
       }

    while (nodeptr)
      {
        nodeptr = nodeptr -> next;
	 nodeptr = delptr;
	    if (nodeptr -> next == NULL)
		{
		   delete delptr; 
                   delptr = NULL;
                }
       }

    

}


This is what I'm referencing by the way...
http://www.cplusplus.com/forum/beginner/4443/
Last edited on
Line 6: what is head? What does nodePtr point to?
Line 8: what do you think delptr = delptr means? What does delptr point to?
Line 13: again, what do you think nodePtr points to?

And why is one pointer named nodePtr while the other is named delptr?
Line 6: what is head? What does nodePtr point to?
Line 8: what do you think delptr = delptr means? What does delptr point to?
Line 13: again, what do you think nodePtr points to?

And why is one pointer named nodePtr while the other is named delptr?


Sorry, head is what points to the first node (I think, now I'm not sure if I even need it)
It should be delptr = nodePtr;
I think nodePtr points to the current node, then it gets traversed down the list until it eventually points to the final node (not NULL). Then I set it equal to delptr and delete delptr.

Your final question gets me thinking, if I'm deleting the, do I even need delptr or should I just delete nodePtr.

nodePtr = the current node being pointed at
delptr = the pointer that needs to be delete, the final node on the list

I'm not sure why I named them that way.
Okay I think I've made some progress....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int RemoveRoom (Room *&head)
{

Room *nodePtr, *delptr;
  
   if(!head)
      {
         cout << "Sorry, no rooms left on the list";
       }

     

   if (head->next)
   {
       	  nodePtr = head->next;
          delete head;
          head = nodePtr;
          cout << "The last room has been removed";
   }



    while (nodePtr)
      {
        delptr = nodePtr; 
	nodePtr = nodePtr -> next;
          
        if (nodePtr -> next == NULL)
        {
           delete delptr; 
           delptr = NULL;
         }
       }

    

}  






The first if statement checks if the list is empty, if it is then it returns, the second if statement is where I'm confused. If there is only one node I'm not sure how to delete it.

The while statement I think is okay, it will traverse the list and delete the last node only.
Topic archived. No new replies allowed.