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
linkedlist.hCode:#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.cCode:#include "hcompress.h"
typedef struct node {
singleNode* value;
struct node* next;
} LinkedList;
//function prototypes
LinkedList* llCreate();
void listAddInOrder(LinkedList** ll, singleNode* value);
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;
}