Dice & Linked List

This is a discussion on Dice & Linked List within the C Programming forums, part of the General Programming Boards category; General question: Currently trying to write a comman line program that accepts the number of times to roll die in ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    86

    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. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    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. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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???
    Last edited by oogabooga; 09-20-2012 at 03:13 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    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. #6
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by carpeltunnel View Post
    ...
    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. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by carpeltunnel View Post
    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"?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    86
    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 );     
             
        printf( "Before adding elements:\n" ); 
        print_list( p1 );     
             
        p1 = prepend_point( p1, p2 ); 
        printf( "After adding p2:\n" ); 
        print_list( p1 ); 
        p1 = prepend_point( p1, p3 ); 
        printf( "After adding p3:\n" ); 
        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. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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);
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User pmeledeo's Avatar
    Join Date
    Sep 2012
    Posts
    1
    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. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,497

    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.
    Last edited by Click_here; 09-20-2012 at 07:42 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 05:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

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