Thread: Linked Lists Chapter 15 Practice Problem 1 Jumping Into C++

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    36

    Linked Lists Chapter 15 Practice Problem 1 Jumping Into C++

    OK, I give up. I've been researching for three days straight how to use linked lists, but nothing is helping. Any chance on of you can help me with the problem? The decryption is this:

    Write a program to remove an element from a linked list; the remove function should take just the element to be removed. Is this function easy to write--and will it be fast? Could this be made easier or faster by adding an additional pointer to the list?35

    ___________________________
    35Hint: what if you had a pointer to the previous node? Does that help?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Presumably you are to write your own linked list. What have you done so far? You know how linked lists work?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    36
    I do not know how linked lists work at all, really. That's the problem. Do you think maybe you could help? I can't find a resource that teaches it without assuming you know how classes work.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I know this site has strict homework policies but I'm going to give you the basic structure you need. Assuming a singly linked list, your data structure should look like this :
    Code:
    struct node {
    
       void *data;
       node *next;
    };
    OP, do you know about pointers? Do you know about allocating structures? Do you know how to create a class out of a structure?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    Assuming a singly linked list, your data structure should look like this :
    No, it should not. Don't teach people bad practices. The struct should look like:

    Code:
    struct XNode
    {
        T Data;
        XNode* Next;
        XNode* Prev; // Optional if you want a doubly linked list
    };
    Where T is the type of the data you intend to store in your linked list. Later on, as you learn templates, this struct would be a templated struct. Do not use void* in C++.
    For additional practice, you can also use smart pointers with a doubly linked list. Then you have to consider circular references.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ArtemisFowl2nd
    I do not know how linked lists work at all, really. That's the problem. Do you think maybe you could help? I can't find a resource that teaches it without assuming you know how classes work.
    You already know structs and new/delete, so gloss over the syntax for now. I see that Alex is a little short of diagrams for the linked list illustration, so if you don't understand based on what diagrams were provided, look for those resources with a more graphical approach.

    Also, I think the assignment is impossible to do if you are following the sample linked list implementation: with just a pointer to the node to be removed, you either need another parameter for the head, or you need to have a doubly linked list. Alex's demo was for a singly linked list.

    I suggest that you start with this: write a program to create, print and destroy a singly linked list of five elements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Oh my apologies then.
    Code:
    template<class T>
    struct node {
    
        T data;
        node<T> *next;
    };

  8. #8
    Registered User
    Join Date
    Apr 2014
    Posts
    36
    Quote Originally Posted by laserlight View Post
    I suggest that you start with this: write a program to create, print and destroy a singly linked list of five elements.
    Does anyone know of some simple linked list example programs that I can traverse with debugging so that I might be able to get some practice with the syntax? Because I can't do it on my own from scratch. I need some examples so that I can see it implemented. For instance, a program that creates, prints, and destroys a singly linked list of five elements?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is Prelude's linked lists tutorial, but it is written for C. Still, the basic ideas are the same.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    MutantJohn: I suggest that you start a new thread. In the first place, template programming is beyond the scope of this exercise, for now.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2014
    Posts
    36
    I may start crying. The Prelude's linked lists tutorial's example problems are not working when I type them into a C command line tool in Xcode. Do you think, maybe, you could give me a short program that creates, prints, and destroys a singled linked list?

  12. #12
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Lol the panic you feel means you are becoming a programmer

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ArtemisFowl2nd
    Do you think, maybe, you could give me a short program that creates, prints, and destroys a singled linked list?
    What would you like to learn after that? I suggested that as an initial exercise because they form the fundamental parts of what you need to know to implement a linked list. If I give you a tailored example of that, you might go over it and understand something, but you won't understand as much as if you tried (and maybe failed) first. If you don't understand enough, then whatever exercise beyond these fundamentals is given to you, you will still fail, except that you will fail harder because it will be more difficult.

    So, give it a try. The book you are using has an example of defining a singly linked list node in the section titled "Creating a linked list". Simplify that node by having just one int member for the struct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2014
    Posts
    36
    So, is this doing it right?

    Code:
    #include <iostream>usingnamespacestd;
    
    
    struct EnemySpaceShip {
        int weaponPower;
        EnemySpaceShip* p_next_enemy;
    };
    
    
    
    
    
    
    EnemySpaceShip* addNewEnemyToList (EnemySpaceShip* p_list) {
    EnemySpaceShip* p_ship = newEnemySpaceShip;
        p_ship->weaponPower = 20;
        p_ship->p_next_enemy = p_list;
        return p_ship;
    }
    
    
    int main () {
        EnemySpaceShip* p_enemies = NULL;
    addNewEnemyToList(p_enemies);
        EnemySpaceShip* p_newEnemy = NULL;
        p_newEnemy = addNewEnemyToList(p_newEnemy);
        cout << p_newEnemy->weaponPower;
    }
    I think that this is supposed to set a head and initialize it to NULL, then add another ship to the list and print it's weaponPower.

  15. #15
    Registered User
    Join Date
    Apr 2014
    Posts
    36
    Oh, wait. The above code makes two linked lists. This is better:

    Code:
    #include <iostream>usingnamespacestd;
    
    
    struct EnemySpaceShip {
        int weaponPower;
        EnemySpaceShip* p_next_enemy;
    };
    
    
    
    
    
    
    EnemySpaceShip* addNewEnemyToList (EnemySpaceShip* p_list) {
    EnemySpaceShip* p_ship = newEnemySpaceShip;
        p_ship->weaponPower = 20;
        p_ship->p_next_enemy = p_list;
        return p_ship;
    }
    
    
    int main () {
        EnemySpaceShip* p_enemies = NULL;
        p_enemies = addNewEnemyToList(p_enemies);
        cout << p_enemies->weaponPower << "\n";
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping Into C++ Chapter 14 Practice Problem 1
    By ArtemisFowl2nd in forum C++ Programming
    Replies: 5
    Last Post: 04-16-2014, 09:36 PM
  2. jumping into c++ chapter 5 problem 7
    By etricity in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2014, 11:23 PM
  3. Replies: 2
    Last Post: 02-24-2014, 05:51 PM
  4. "Jumping into C++" Chapter 5 Practice Problem
    By Grae in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2013, 01:46 PM
  5. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM