Thread: Linked List Structs

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    100

    Linked List Structs

    I'm getting a segmentation fault when I run this. It is supposed to take in a pointer to an array of structures which contain a weight variable with varying amounts in them. listAddInOrder is supposed to store the weights in increasing weight starting at the struct element in the array with the lowest weight and going up storing them in one linked list. A portion of my linked list function was originally written for strings, so I'm having difficulty modifying it to work with an array of structs. This is in my linkedlist.c file, the last chunk of code attached. Any help would be greatly appreciated.

    hcompress.h
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        typedef struct tnode {
            double weight;
            int c;
            struct tnode* left;
            struct tnode* right;
            struct tnode* parent;
        } singleNode;
        
        //prototypes
        singleNode* createFreqTable(char* filename);
        singleNode* createHuffmanTree(singleNode* leafNodes);
    linkedlist.h
    Code:
    #include "hcompress.h"
    
    typedef struct node {
      singleNode* value;
      struct node* next;
    } LinkedList;
    
    //function prototypes
    LinkedList* llCreate();
    void listAddInOrder(LinkedList** ll, singleNode* value);
    linkedlist.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "linkedlist.h"
    
    int main() {
        LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
        singleNode* first;
        singleNode* second;
        singleNode* third;
        first->weight = 3.0;
        second->weight = 7.0;
        third->weight = 12.0;
       
        listAddInOrder(&list, first);
        listAddInOrder(&list, second);
        listAddInOrder(&list, third);
       
       
        printf("weight: %f\n", first->weight);
       // llDisplay(list);
    }
    
    void listAddInOrder(LinkedList** ll, singleNode* value){
    //add in order function - needs work
    LinkedList* nn = (LinkedList*)malloc(sizeof(LinkedList));
    nn->value = value;
    
    if (*ll == NULL) {
       *ll = nn;
    } else {
       // General case - find the end
       LinkedList* p = *ll;
       while (p->next != NULL) {
        p = p->next;
       }
       p->next = nn;
    }
    singleNode temp;
    for (int i = 0; i < 127; i++) {
           for (int j = i + 1; j < 127; j++) {
               if ((*(value+i)).weight > (*(value+j)).weight) {
                   temp = value[i];
                   value[i] = value[j];
                   value[j] = temp;
               }
           }
       }
    }
    
    
    
    LinkedList* llCreate() {
      return NULL;
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Think about what you are doing here:
    Code:
    singleNode* first;
    singleNode* second;
    singleNode* third;
    first->weight = 3.0;
    second->weight = 7.0;
    third->weight = 12.0;
    You're assigning values to uninitalized pointers.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include "linkedlist.h"
    
    int main() {
        LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
    Am I wrong, or shouldn't this be?:

    Code:
    node * list = malloc( sizeof( list ) );
    And isn't LinkedList in this scenario a declared variable, not the data type?

    Edit: I am wrong! Disregard above....
    Last edited by jwroblewski44; 03-05-2013 at 02:43 AM.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by johngoodman View Post
    I'm getting a segmentation fault when I run this.
    Isn't it time to learn to use a debugger which will help you to find the errors in your program yourself?

    At least you would be able to tell us where the program crashes instead of telling us that it doesn't work.

    Even just compiling with warnings should have told you that you are using the pointers uninitialised:
    Code:
    $ cat foo.c
    #include<stdio.h>
    
    int main(void)
    {
        struct test {int a;} *t;
        t->a = 1;
    
        return 0;
    }
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:6:10: warning: ‘t’ is used uninitialised in this function [-Wuninitialized]
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inter-dependent structs for doubly-linked list
    By creek23 in forum C Programming
    Replies: 2
    Last Post: 06-24-2010, 05:12 AM
  2. Replies: 9
    Last Post: 04-03-2010, 11:44 AM
  3. Help crating a linked list of array of structs.
    By doubty in forum C Programming
    Replies: 1
    Last Post: 07-05-2009, 08:49 PM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  5. CAN'T FIGURE IT -- structs on a linked list
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-01-2002, 05:22 AM