New Posts  All Forums:Forum Nav:

C++ Linked list help

post #1 of 3
Thread Starter 
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...
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.

Code:

int RemoveRoom (Room *FirstRoom)
{

FirstRoom *nodePtr, *delptr;
  
   if(nodePtr == head)
      {
         delptr = nodePtr;
         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;
                }
       }

    


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.
Edited by skatingrocker17 - 3/15/11 at 1:41pm
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
post #2 of 3
It kinda depends on the implementation of the Room struct, but I'll take a crack at it.

1st thing I see is that you have node and del declared as FirstRoom, while it should be Room, if that is the type that is being passed.

2nd thing is this will leak memory if nodeptr is never deleted(I'm assuming when you do instantiate these two pointers you will be using new).

Algorithmically it seems correct but the way you are handling the pointers needs cleaning up, and without knowing how Room is defined I can't really help you much, but definitely just do a quick google search on managing pointers in c++ and there are tons of lists of common flaws.
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
Lappy 3001
(13 items)
 
  
CPUMotherboardGraphicsRAM
i3-560 @ 3.85 GHz ASRock H55M-LE PNY XLR8 GTX 460 4G Corsair XMS3 DDR3 @ 1600 MHz 
OSMonitorPowerCase
Windows 7 / Ubuntu Samsun SyncMaster 226bw 22" Corsair CX600 Cooler Master HAF 932 
  hide details  
Reply
post #3 of 3
Thread Starter 
Quote:
Originally Posted by LetThereBeDwight View Post
It kinda depends on the implementation of the Room struct, but I'll take a crack at it.

1st thing I see is that you have node and del declared as FirstRoom, while it should be Room, if that is the type that is being passed.

2nd thing is this will leak memory if nodeptr is never deleted(I'm assuming when you do instantiate these two pointers you will be using new).

Algorithmically it seems correct but the way you are handling the pointers needs cleaning up, and without knowing how Room is defined I can't really help you much, but definitely just do a quick google search on managing pointers in c++ and there are tons of lists of common flaws.
I've been trying to reference this. Should I just get rid of delptr and just keep nodePtr and delete that?

All I need is a pointer that will point to the last node so I can delete the last node but I'm not sure how to do that.



EDIT:

Okay I think I've made some progress....
Code:

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.
Edited by skatingrocker17 - 3/15/11 at 3:31pm
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Core i7 930 @ 4004Mhz 1.29 V Gigabyte GA-X58A-UD3R XFX Radeon 6970 nVidia 8800GTX 
RAMHard DriveOptical DriveOptical Drive
G. SKILL 6GB 1333Mhz @ 1455Mhz OCZ Vertex 3 120GB / Seagate 500GB Lite-On DVD-RW TSSTcorp DVD/RW 
CoolingOSMonitorMonitor
Prolimatech Megahalems Windows 7 x64 Professional Samsung S24A350H Dell 20" LCD 
MonitorMonitorKeyboardPower
MAG 17" LCD Samsung 50" Plasma  Focus FK-2001 Mechanical Corsair TX750 
CaseMouseMouse PadAudio
Cooler Master Storm Scout (Modded) Razer Abyssus Disney's Mickey Mouse with circuits JVC MX-GT700 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming