Thread: Linked List help

  1. #1
    Registered User Bad_Scooter's Avatar
    Join Date
    Mar 2003
    Posts
    15

    Question Linked List help

    I as having some trouble with linked list. What I am basically trying to do is create a doubly linked list with 10 nodes. Each node represents a frame of bowling. Since I am not adding or removing nodes throughout the program a vector seems like a better choice to me, but its not up to me. First I just want to create a list with ten nodes (frames). This is what I have (pertaining to this issue, I have more):

    frame *head; //declarations in class header
    frame *back;
    ---------------------------------------------------
    int e=0;
    head = new frame;
    while (e<9)
    {
    head->next =new frame;
    e++;
    }

    ----------------------------------------------------
    player:layer() //constructor in class player
    {
    head = new frame;
    head->score=0;
    head->ball1=0;
    head->ball2=0;
    head->ball3=0;
    back = head;
    }

    ----------------------------------------------------

    declarations is frame:

    class frame
    {
    public:

    int score;
    int ball1;
    int ball2;
    int ball3;
    frame *prev;
    frame *next;
    };

    I've tried to cut out a lot of the program and just get to the heart of the matter. All I am really asking is is this a proper way to build a linked list? I am having trouble visualizing the list. My program actually goes through this section, but doesn't assign my constructor values to each frame. Also any suggestions on how to move my pointer along through a list. Still a little shakey on pointers. A generic answer, or one using these examples would be appreciated. Thanks for reading.......




  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I think this should help you to build the list if I didn't make any stupid mistakes. You should always remember to initialize all of those next and prev pointers as soon as possible after creating the nodes using new or you could end up in trouble later on if you forget.
    Code:
    frame *head; //declarations in class header
    frame *back;
    ---------------------------------------------------
    int e=0;
    head = new frame;
    head->prev = NULL;    // Should always get into the habit of initializing you pointers
    head->next = NULL;    // Initialize pointer
    back = head;
    while( e++ < 9 )
    {
        back->next = new frame;      // Add new node to end
        back->next->prev = back;    // Set new nodes prev pointer to point to previous last node
        back->next->next = NULL;    // Initialize new nodes next pointer
        back = back->next;          // Bump up our last node to the newly created node
    }
    Last edited by hk_mp5kpdw; 03-06-2003 at 09:24 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User Bad_Scooter's Avatar
    Join Date
    Mar 2003
    Posts
    15

    Thanks

    Thanks for the quick reply. That helps alot in my understanding, I really didn't have a good example to work through before.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM