1. ## Dice & Linked List

General question:

Currently trying to write a comman line program that accepts the number of times to roll die in a linked list data structure. I am ok with the linked list structure but am unsure how to make it work without a predetermined amount. (i.e. I can do x y and 3 points of each but not sure how to write a random user input)

Any help of how to get started would be greatly appreciated.

2. With a linked list you don't care how many elements there are.

You just simply add each new one to (the beginning of) the list until there are no more.

3. So I understand that

Code:
```struct point{
int x;
int y;
struct point * next;
};```
This would allow you to set points (x,y) for the struct. I then allocate the memory for each point I want to include, then set the values.

But I'm unsure the syntax for write code if you do not know how many values there will be.

4. It looks like English, but I honestly can't understand what you're asking.

"number of times to roll die in a linked list "
Huh???

"I can do x y and 3"
What???

EDIT:
It's made a little clearer by your second post, but I'd still be guessing. Where does the "die" come in? You want values from 1 to 6 inclusive? Or are you using the word "die" just to mean random number???

5. I'm sorry. It's hard to ask the ask the question well because I'm not entirely sure what I need.

So the program is to allow the user to input in the comman line for how many times they would like to roll two die. The program is to accept the amount, and randomly roll the two die and store the number in the structure.

I understand how to write a linked list when you know the amount of value you would like to use and allocate the space. But I am unsure how to write if the amount of values in the structure is user dependent.

6. Originally Posted by carpeltunnel
...
But I'm unsure the syntax for write code if you do not know how many values there will be.
Just start as if you know how many elements will be in the linked list (imagine 86003 elements) and go from there.

7. Originally Posted by carpeltunnel
I understand how to write a linked list when you know the amount of value you would like to use and allocate the space. But I am unsure how to write if the amount of values in the structure is user dependent.
That doesn't sound like much of a linked list! The whole point of a linked list is that the amount of storage is dynamic. So it doesn't seem that you know what a linked list is.

Can you post some code that deals with what you consider to be a "linked list"?

8. This is the code my professor gave us before assigning the code

Code:
```#include <stdio.h>
#include <stdlib.h>

struct point{
int x;
int y;
struct point * next;
};

void print_list( struct point * );
struct point * make_point( int, int );
struct point * prepend_point( struct point *, struct point * );
struct point * remove_point( struct point * );
struct point * total_points( struct point * );
int list_size( struct point * );
struct point * remove_end( struct point * );

int main(){

struct point * p1 = make_point( 11, 11 );
struct point * p2 = make_point( 22, 22 );
struct point * p3 = make_point( 33, 33 );
struct point * p4 = make_point( 44, 44 );

print_list( p1 );

p1 = prepend_point( p1, p2 );
print_list( p1 );
p1 = prepend_point( p1, p3 );
print_list( p1 );
p1 = prepend_point( p1, p4 );
printf( "After last element:\n" );
print_list( p1 );

printf( "The size of the list is: %d\n", list_size( p1 ) );
struct point * the_total = total_points( p1 );
printf( "\nThe totals are x: %d, y: %d\n", the_total->x, the_total->y );

printf( "Removed last element\n" );
p1 = remove_end( p1 );
print_list( p1 );

p1 = remove_point( p1 );
printf( "Remove first element\n" );
print_list( p1 );
p1 = remove_point( p1 );
printf( "Remove first element\n" );
print_list( p1 );
p1 = remove_point( p1 );
printf( "Remove first element\n" );
print_list( p1 );
p1 = remove_point( p1 );
printf( "Remove first element\n" );
print_list( p1 );
p1 = remove_point( p1 );
printf( "Remove first element\n" );
print_list( p1 );
printf( "The size of the list is: %d\n", list_size( p1 ) );

getchar();
return 0;
}

void print_list( struct point * list ){
while( list != NULL ){
printf( "\tIn print list: %d, %d\n", list->x, list->y );
list = list->next;
}
}

struct point * make_point( int a, int b ){
struct point * temp = (struct point *)malloc( sizeof( struct point ) );
temp->x = a;
temp->y = b;
temp->next = NULL;
return temp;
}

struct point * prepend_point( struct point * a , struct point * b ){
b->next = a;
return b;
}

struct point * remove_point( struct point * toast ){
/*if( toast != NULL ){
struct point * temp = toast->next;
toast = NULL;
free( toast );
return temp;
}else{
return NULL;
}*/

if( !toast ){
return NULL;
}else{
struct point * temp = toast->next;
toast = NULL;
free( toast );
return temp;
}

}

struct point * total_points( struct point * list ){
struct point * temp = make_point( 0, 0 );
while( list ){
printf( "beep\n" );
temp->x = temp->x + list->x;
temp->y = temp->y + list->y;
list = list->next;
}
return temp;
}

int list_size( struct point * list ){
int counter = 0;
while( list ){
list = list->next;
counter++;
}
return counter;
}

struct point * remove_end( struct point * list ){
if( !list ){
return NULL;
}else if( !list->next ){
free( list );
return NULL;
}else{
struct point * temp = list;
while( temp->next->next ){
temp = temp->next;
}
free( temp->next );
temp->next = NULL;
return list;
}
}```
This is the code my professor gave before assigning the program. I was under the impression this was a linked list because he was for one to be inlcuded in the program

9. Okay. Now I get it!

Consider using the make_point and prepend_point functions in a loop. E.g.,
Code:
```struct point *list = NULL;
struct point *p;
for( i = 0; i < 10; i++ ){
p = make_point(x, y);
list = prepend_point(list, p);
}```

10. I believe we might be in the same class, working on the same project. Otherwise this is a huge coincidence. I am having the same issues. I understand the sample code regarding lists that the professor gave and I am alright with the dice-rolling. But I cannot seem to figure out how to actually do the program correctly with user-dependent input.

11. ## Clicked the wrong word in spell check

The way I'd Interpret the question (as you described it) is that you want to prompt for a number (the number of times to roll the dice). i.e. random number, where n is and element of [1,6].

Generate the the random number 'n'

Add this to the end of the linked list - This uses the function malloc(), so you don't need to know how many rolls there will be when you write your program.

For each roll of the dice, you add another element to the end of the linked list with a new random number - And at the end, you print them out (or something)

If I were you, I'd contact your teacher to clarify what they wanted.