Thread: Help with debugging my Linked List code.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    12

    Help with debugging my Linked List code.

    I am having trouble getting my homework assignment to compile. There are about 10 errors that I cant seem to fix and I have been working for a while. I am not very good at programming so please bare with me!! But I appreciate all the help no matter what feedback you give! Thanks. Here we go:

    Here is the sample input file and what the program is supposed to output:
    Sample Input file:
    7
    6 0
    0 5
    5 1
    3 0
    2 6
    4 3
    5 3
    4 1
    6 1
    3 5
    1 3
    6 2
    5 2
    5 3
    1 3
    5 2
    0 0
    3 1
    6 4
    6 5

    Sample Output:

    $ gcc hw2.c
    $ ./a.out bad_input.txt
    Unable to open file, enter a new file name: bad_input_2.txt
    Unable to open file, enter a new file name: input.txt
    Pre Sort
    -
    |0| 0 ->5 ->NULL = 5
    -
    -
    |1| 3 ->3 ->NULL = 6
    -
    -
    |2| 6 ->NULL = 6
    -
    -
    |3| 1 ->5 ->0 ->NULL = 6
    -
    -
    |4| 1 ->3 ->NULL = 4
    -
    -
    |5| 2 ->3 ->2 ->3 ->1 ->NULL = 11
    -
    -
    |6| 5 ->4 ->2 ->1 ->0 ->NULL = 12
    -


    Post Sort
    -
    |0| 1 ->3 ->NULL = 4
    -
    -
    |1| 0 ->5 ->NULL = 5
    -
    -
    |2| 3 ->3 ->NULL = 6
    -
    -
    |3| 6 ->NULL = 6
    -
    -
    |4| 1 ->5 ->0 ->NULL = 6
    -
    -
    |5| 2 ->3 ->2 ->3 ->1 ->NULL = 11
    -
    -
    |6| 5 ->4 ->2 ->1 ->0 ->NULL = 12



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

    Here is my code that I have written so far. I commented what the functions are SUPPOSED to do. I have around 10 errors, most of them being pointer types( I SUCK at pointers).
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    #define MAX_FILE_LENGTH 20
    
    
    typedef struct node_{
            int value;
    /*1)    struct node_ next;*/
            struct node_* next;
    }Node;
    
    
    typedef Node* List;
    
    
    int create_list(List**,FILE*);
    void print_list(List*, int);
    void free_list(List*, int);
    int list_sum(Node*);
    void insertion_sort(List*, int);
    Node* new_node(int value);
    
    
    /* Main will read in an input file from the command line, 
     * if the file fails to open the program should prompt the user for a new file, 
     * at no point should the program quit. If the program reads in a new file name, 
     * it can be up to size MAX_FILE_LENGTH. 
     * From there the program should read in the file, print it out, sort it, 
     * print it out again and then free it. */
    int main (int argc, char* argv[]){
    
    
            if(argc != 2){
                    printf("Incorrect number of command line arguments\n");
                    return 1;
            }
    
    
            FILE* fp = fopen(argv[0], "r");
    
    
            while(fp == NULL){
                    char file[MAX_FILE_LENGTH];
                    printf("Unable to open file, enter a new file name: ");
                    scanf("%d", file);
                    fp = fopen(file, "r");
            }
    
    
            /*2)List array;*/
            List **array;
            int length = create_list(array, fp);
    
    
            printf("Pre Sort\n");
            print_list(array, length);
    
    
            insertion_sort(array,length);
            printf("\nPost Sort\n");
          print_list(array, length);
    
    
            return 0;
    }
    
    
    /*This function takes in a pointer to a list and a file pointer. 
     * The first line of the input file is the length of the array to be created.
     * Each subsequent line is composed of two numbers, an index and a value. 
     * The index is the index of the linked list where a node with the value of value should be inserted. 
     * So for example, if index = 0 and value = 3, and before the insertion array[0] = 1 -> 2 -> NULL, 
     * after the insertion array[0] = 3 -> 1 -> 2 -> NULL. This function returns the length of the array.*/
    int create_list(List** array,FILE* fp){
            int length, i, index, value;
    
    
            fscanf(fp, "%d", &length);
    
    
            array = malloc(sizeof(Node));
    
    
            for(i = 0; i < length; i++)
                    (*array)[i] = NULL;
    
    
            while(1){
                    fscanf(fp, "%d %d", &index, &value);
    
    
                    Node* node = new_node(value);
    
    
                    node->next = array[index];
    
    
                    array[index] = node;
            }
    
    
            return length;
    }
    
    
    /*Creates a new node of type Node, sets node->value = value and returns it. */
    Node* new_node(int value){
            Node* node;
            node->value = value;
            node->next = NULL;
    
    
            return node;
    }
    
    
    /*Takes in the head to a single linked list and returns the sum of the values of each of its nodes. 
     * Ex: Node* head = 1 -> 2 -> 3 -> NULL then list_sum(head) = 6.*/
    int list_sum(Node* head){
    
    
            return head->value + list_sum(head->next);
    }
    
    
    /*For each index in the array, print the index, the linked list it points to,
     * and the sum of its nodes. See the sample output for an example.*/
    void print_list(List* array, int length){
            int i;
    
    
            for(i = 0; i < length; i++){
                    Node *curr = array[i];
    
    
                    printf(" -\n|%d| ", i);
    
    
                    printf("%d ->", curr->value);
    
    
                    curr = curr->next;
    
    
                    printf("NULL = %d\n -\n", list_sum(array[i]));
            }
    
    
            /*3)return curr;*/
    }
    
    
    /*Sorts the array using insertion sort in ascending order by the sums of each linked list. */
    void insertion_sort(List* array, int length){
            int* sum = malloc(sizeof(int) * length);
            int i, j, value;
            Node* node;
    
    
            for(i = 1; i < length; i++){
                    value = sum[i];
                    node = array[i];
                    for(j = i; j > 0 && value < sum[j + 1]; j++)
                    {
                            sum[j] = sum[j - 1];
                            array[j] = array[j - 1];
                    }
            }
    
    
            printf("\n");
            free(sum);
    }
    /*Free all allocated memory.*/
    /*4)void free_list(List array, int length){*/
    void free_list(List* array, int length){
            int i;
            Node *curr;
            while(curr != NULL){
                    Node* prev = curr;
                    curr = curr->next;
                    free(curr);
            }
    }
    here are the errors I am getting

    homework2.c: In function ‘main’:
    homework2.c:48: warning: passing argument 1 of ‘print_list’ from incompatible pointer type
    homework2.c:15: note: expected ‘struct Node **’ but argument is of type ‘struct Node ***’
    homework2.c:51: warning: passing argument 1 of ‘insertion_sort’ from incompatible pointer type
    homework2.c:18: note: expected ‘struct Node **’ but argument is of type ‘struct Node ***’
    homework2.c:53: warning: passing argument 1 of ‘print_list’ from incompatible pointer type
    homework2.c:15: note: expected ‘struct Node **’ but argument is of type ‘struct Node ***’
    homework2.c: In function ‘create_list’:
    homework2.c:79: warning: assignment from incompatible pointer type
    homework2.c:81: warning: assignment from incompatible pointer type

    THANK YOU ALL I APPRECIATE THE HELP!!!
    Last edited by nslice22; 03-26-2014 at 01:28 PM.

  2. #2
    Registered User
    Join Date
    Mar 2014
    Posts
    12
    someone surely can help me right?

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    12
    come on guys im trying my best. I have figured out a couple of errors So I will edit the original post!! Thanks a lot

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm sure we can. But helping people takes time. We volunteer our time, between work, school, family and other obligations and our personal lives. You should not expect instant answers, nor even answers within 25 minutes (which is how long you waited). A bit of patience is appreciated, somebody will come along eventually.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    12
    Quote Originally Posted by anduril462 View Post
    I'm sure we can. But helping people takes time. We volunteer our time, between work, school, family and other obligations and our personal lives. You should not expect instant answers, nor even answers within 25 minutes (which is how long you waited). A bit of patience is appreciated, somebody will come along eventually.
    youre right. I am sorry. I will be patient!

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Now I've had my lunch and can think a little more clearly about your errors.

    The bulk of the issue seems to be your confusion in how many levels of indirection (e.g. pointer, pointer-to-pointer, pointer-to-pointer-to-pointer) you should be using. Should it be *foo or **foo? Should it be bar or &bar? The biggest cause of this is probably
    Code:
    typedef Node* List;
    So a Node type is struct that represents an element in a list. A List type is a Node *. So a List * is really a Node ** and a List ** is a Node ***. You've graduated to a 3-star programmer! This causes trouble when you try to assign or pass parameters between things you declare as Node * and List...or was it List *...or List **? Or you just lose track of how many *s you need on something (e.g. in main array is a List **, but you pass it to print_list which takes a List *). See how that can get confusing?

    I recommend, especially for beginners, not typedef'ing away a pointer. That is, never put a * in a typedef, so you always remember how many levels of indirection (pointers) there are. Like most rules, there are exceptions (opaque types) but this is not such a case -- and by the time you're writing libraries/modules with opaque types, you probably have a sufficient grasp of pointers to not get so tripped up. Try removing the type definition of List and expressing everything in Node, Node *, Node **, etc. I highly doubt you need Node ***.

    It seems you also wrote much of this code in one pass, without compiling and testing (given that you have errors across several function). You should compile and test often, then you would only have a few mistakes to deal with at the most, and you would have caught on to this problem earlier, so it would take much less effort to straighten it out. I recommend:
    1. Study and understand the problem
    2. Work out a solution on paper
    3. Turn into pseudo code
    4. Write the actual code, in small steps (5-10 lines at a time).
    5. Compile (at maximum warning level) and fix all warnings and errors.
    6. Test the code to make sure it works, fix any bugs.
    7. Repeat steps 4-6 until finished.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nslice22
    homework2.c:48: warning: passing argument 1 of ‘print_list’ from incompatible pointer type
    Look at the function call:
    Code:
    print_list(array, length);
    Look at the declaration of argument 1, i.e., array:
    Code:
    List **array;
    Now look at the declaration of print_list:
    Code:
    void print_list(List* array, int length)
    Clearly, there's a type mismatch: print_list expects a pointer to List as the first argument, but you passed a pointer to pointer to List.

    I'd say the problem here is with the declaration of array. You should have written:
    Code:
    List *array;
    This would immediately fix the type mismatch here, but of course you would then have a problem here:
    Code:
    int length = create_list(array, fp);
    My initial inclination is that you should change it to:
    Code:
    int length = create_list(&array, fp);
    However, on further investigation, you seem to have more levels of indirection than is desirable. I spotted:
    Code:
    typedef Node* List;
    This can work, but it is problematic because the typedef hides the fact that List is actually a pointer. I would prefer:
    Code:
    typedef struct {
        Node *head;
    } List;
    But if you have to have this typedef, then you have a problem in that even if you declare array in main to be:
    Code:
    List *array;
    You need to allocate space for what array points to, and then allocate space for what the pointer that array points to points to. If you take my suggested definition of List as a struct instead, then you can declare array as:
    Code:
    List array;
    and then pass it to create_list:
    Code:
    int length = create_list(&array, fp);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2014
    Posts
    12
    Thank you both very much! I'm down to only a few errors!

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    2
    LOL we must be in the same class because I have this assignment too! I am about to post my questions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. Linked list - why this bit of code?
    By chris1985 in forum C Programming
    Replies: 2
    Last Post: 10-04-2005, 06:17 AM
  3. my linked list code - comments please
    By cdave in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 12:37 AM
  4. What's wrong with this linked-list code?
    By helplz in forum C Programming
    Replies: 4
    Last Post: 06-15-2003, 04:22 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM

Tags for this Thread