Thread: Chapter 15 - Practice Problem1

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    1

    Chapter 15 - Practice Problem1

    I'm a beginner at C++ and I'm currently reading the "Jumping into C++" book. I'm currently at chapter 15 doing practice problem 1.

    Practice problem 1 basically says to write a program to remove an element from a linked list; the remove function should take just the element to be removed.

    Here's my code so far:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    struct Spaceship 
    {
        int x;
        int y;
        int power;
        Spaceship* pNextSpaceShip;
    };
    
    
    Spaceship* addNewShipToList(Spaceship* pList);
    
    
    void removeShipFromList(Spaceship* pLast, Spaceship* pList);
    
    
    
    
    Spaceship* pShipList;
    
    
    int main()
    {
        pShipList = NULL;
        
        
        int counter = 0;
        // add 9 elements to the linked list
        for(int i = 0; i < 9; ++i)
        {
            pShipList = addNewShipToList(pShipList);
        }
    
    
        // set the current to the first element in the list
        Spaceship* pCurrent = pShipList;
        // traverse through our list to verify how many elements are in the list
        while(pCurrent != NULL)
        {
            pCurrent = pCurrent->pNextSpaceShip;
            counter++;
        }
    
    
        cout << "The linked list contains " << counter << " ships" << endl;
        
        // reset counter to 0
        counter = 0;
    
    
        // create a new pointer that points to the first element
        Spaceship* pLast = pShipList;
        // remove the first element from the list
        removeShipFromList(pLast, pShipList);
        
        // set the current to the 'new' first element of the list
        pCurrent = pShipList;
        
        // traverse through the list again to verify that one element from the list has been deleted
        while(pCurrent != NULL)
        {
            pCurrent = pCurrent->pNextSpaceShip;
            counter++;
        }
        cout << "The linked list contains " << counter << " ships" << endl;
        
    
    
        cin.ignore();
        cin.get();
    
    
        return 0;
    }
    
    
    
    
    
    
    Spaceship* addNewShipToList(Spaceship* pList)
    {
        // to use this function, use
        //list = addNewShipToList(list);
        //////////////////////////////////
        Spaceship* pShip = new Spaceship;
        pShip->x = 0;
        pShip->y = 0;
        pShip->power = 20;
        pShip->pNextSpaceShip = pList;
    
    
        return pShip;
    }
    
    
    void removeShipFromList(Spaceship* pLast, Spaceship* pList)
    {
        pList = pList->pNextSpaceShip;
        delete pLast;
    
    
    }
    I'm running to a run-time error when the program reaches the inside of the 2nd while-loop, specifically at line 57 which is

    Code:
    pCurrent = pCurrent->pNextSpaceShip;
    I think the way I wrote my removeShipFromList function is wrong. Can anyone help me figure this out?

    Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Removing a node from a linked list using only the node to delete as a starting point requires that the list be doubly linked. Then you can do these four lines:

    Code:
    if (old->next != NULL )
      old->next->prev = old->prev;
    if (old->prev != NULL )
      old->prev->next = old->next;
    Now as far as the list is concerned, old isn't in there. You can free old's memory.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I'm a little surprised that "Jumping Into C++" does not introduce classes earlier in the book. I just looked at the table of contents, and it appears that it discusses lists and trees before it talks about classes. this seems backwards to me, and this code in particular seems very C-style.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chapter 5 practice program 2 difficulties
    By DaveHubbard in forum C++ Programming
    Replies: 15
    Last Post: 05-17-2013, 01:24 PM
  2. More Chapter 5 practice problems!
    By tsdad in forum C++ Programming
    Replies: 13
    Last Post: 05-17-2013, 11:06 AM
  3. Chapter 2 practice problems
    By Kyle Spalding in forum C++ Programming
    Replies: 14
    Last Post: 03-19-2013, 06:12 AM
  4. K&R chapter 8
    By Tool in forum C Programming
    Replies: 9
    Last Post: 02-22-2010, 02:05 PM
  5. Please help! Newbie pointer problem1
    By robsmith in forum C Programming
    Replies: 6
    Last Post: 03-13-2005, 12:18 AM

Tags for this Thread