Linked List Help

This is a discussion on Linked List Help within the C++ Programming forums, part of the General Programming Boards category; I am building a program tell score for a game of bowling. I have created a doubly linked list of ...

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

    Linked List Help

    I am building a program tell score for a game of bowling. I have created a doubly linked list of ten frames. Each frame includes three integers - ball1, ball2, and ball3 for the 10th frame only.
    For my scoring I need to look back one and possibly two frames to calculate the score, depending on strikes and spares previously thrown.

    My question is how do I retrieve the values of those balls?
    I'll strip it down to just one very basic example, because if I can figure out the syntax I can implement it throughout my program.

    total=head->prev->ball1+head->prev->ball2;


    head is a pointer
    frame *head;

    prev is a pointer in frame
    frame *prev;

    total is a local variable of type int

    Any help using this example or a generic one would be great. I am still figuring out how to move within linked lists so any suggestions would be helpful. Thanks for the time

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    What you have will only work if your list is circular (which I doubt it is). If you have a pointer to the end of your list, then you go back a frew frames using it. But it sounds like you need to get your understanding of pointers down a bit more.
    Review this, it may sharpen the picture a bit.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    current->ball //ball from this frame
    current->prev->ball //ball from one frame ago
    current->prev->prev->ball //ball from two frames ago

    make sure there is a ball from previous frames however before you call for them, otherwise you will get a sneaky error in the code.

    This will work if a node is defined something like this:

    struct node
    {
    int ball1;
    int ball2;
    node * prev;//points to previous node, if it exists
    node * next;//points to next node, if it exists
    }
    Last edited by elad; 03-06-2003 at 03:46 PM.

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

    Thanks for the help

    Appreciate the suggestions, my problem from earlier was that I should have been entering them like this:

    cin>>head->ball1;

    instead of calling a function to do this:

    cin>>ball1;

    Dumb mistake from staring at the screen to long........



    trial and error and asking question = the way to learn.......

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, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21