-
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:)
-
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
-
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
}
-
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........
:o
trial and error and asking question = the way to learn.......