This is the first time I have encountered a "no stack." error giving me a seg fault. Anyhow, I am scanning words into a binary search tree and it is giving me a seg fault. I cannot figure out which line. Here is the file (words.txt):
4
bravo
alpha
gamma
delta
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct node_t{
char *word;
struct node_t *left;
struct node_t *right;
}node_t;
node_t *insert(char *currWord, node_t *head){
node_t *curr_node = head, *new_node = malloc(sizeof(node_t));
new_node->right = NULL;
new_node->left = NULL;
new_node->word = currWord;
if (head = NULL)
return new_node;
while (1) {
if (strcmp(currWord, curr_node->word) > 0) {
if (curr_node->right == NULL) {
curr_node->right = new_node;
return head;
}else
curr_node = curr_node->right;
}else{
if (curr_node->left == NULL) {
curr_node->left = new_node;
return head;
}else
curr_node = curr_node->left;
}
}
}
int main(){
FILE *ifp;
ifp = fopen("words.txt", "r");
int numWords, i;
node_t *head = malloc(sizeof(node_t));
fscanf(ifp, "%d", &numWords);
for (i = 0; i < numWords; i++){
char *currWord = malloc(sizeof(char));
fscanf(ifp, "%s", currWord);
head = insert(currWord, head);
}
fclose(ifp);
return 0;
}