Trouble adding vertexes to linked list - c

There is something wrong with my add_vertex
In valgrind I am getting an "Uninitialised value was created by a heap allocation" error
at 0x4C27A2E: malloc (vg_replace_malloc.c:270)
by 0x400892: add_vertex (graph.c:56)
typedef struct {
char *dest_name;
int cost;
} dest_cost;
typedef struct Node {
char *name;
dest_cost *destcost;
int num_dest;
struct Node *next;
} Node;
typedef struct {
int size;
struct Node *node;
} Graph;
INITIALIZE GRAPH
void init_graph(Graph *graph) {
if (graph != NULL)
graph->size = 0;
}
ADD VERTEX
int add_vertex(Graph *graph, const char new_vertex[]) {
Node *next, *temp, *new_node;
int i;
if (has_vertex(*graph, new_vertex))
return 0;
new_node = malloc(sizeof(Node));
new_node->name = malloc(strlen(new_vertex) + 1);
strcpy(new_node->name, new_vertex);
new_node->num_dest = 0;
if (graph->size != 0) {
temp = graph->node;
if (strcmp(temp->name, new_vertex) > 0) {
new_node->next = temp;
graph->node = new_node;
graph->size++;
return 1;
}
else for (i = 1; i < graph->size; i++) {
next = temp->next;
if (strcmp(next->name, new_vertex) > 0) {
new_node->next = next;
temp->next = new_node;
graph->size++;
return 1;
}
}
temp->next = new_node;
graph->size++;
return 1;
}
graph->node = new_node;
graph->size++;
return 1;
}
ADDED: CLEAR GRAPH
void clear_graph(Graph *graph) {
int i;
Node *next, *n = graph->node;
if (graph != NULL) {
for (i = 0; i < graph->size; i++) {
next = n->next;
free(n);
n = next;
}
free(graph);
}
}
ADDED TEST
Graph graph;
const char *vertices_to_add[]= {"koala", "platypus", "snake", "salamander",
"gecko", "frog", "dog", "hedgehog"};
int i;
init_graph(&graph);
for (i= 0; i < sizeof(vertices_to_add) / sizeof(vertices_to_add[0]); i++)
add_vertex(&graph, vertices_to_add[i]);
clear_graph(&graph);

Related

How to declare and access a pointer to a member of a member struct in C?

So, I am relatively new to C and trying to implement a Queue using Linked Lists. Here is some code I wrote with help from the internet.
#include <stdio.h>
#include <stdlib.h>
#define pscan(prompt, x) printf(prompt); scanf("%d", &x)
#define nl() printf("\n");
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct LinkedList {
Node* head;
Node* tail;
int size;
int (*add) (struct LinkedList*, int, int);
int (*append) (struct LinkedList*, int);
int (*get) (struct LinkedList*, int);
int (*remove) (struct LinkedList*, int);
void (*display_list) (struct LinkedList*);
Node* (*createNode) (int);
} LinkedList;
int add (LinkedList* self, int data, int position);
int append (LinkedList* self, int data);
int get (LinkedList* self, int position);
int rmv (LinkedList* self, int position);
void display_list (LinkedList* self);
LinkedList createLinkedList ();
Node* createNode (int data);
int add(LinkedList* self, int data, int position)
{
if (position > self->size || position < 0)
{
printf("Index out of bounds\n");
return 0;
}
Node* newNode = self->createNode(data);
Node* head = self->head;
Node* tail = self->tail;
if (position == 0)
{
if (head == NULL) self->head = newNode;
else
{
if (tail == NULL) tail = head;
newNode->next = head;
self->head = newNode;
}
self->size++;
}
else if (position == self->size)
{
if (head == NULL) self->head = newNode;
else
{
if (tail == NULL) tail = head;
tail->next = newNode;
self->tail = newNode;
}
self->size++;
}
else
{
Node* prev = head;
for(int i = 0; i < position-1; i++)
{
prev = prev->next;
}
Node* node = prev->next;
prev->next = newNode;
newNode->next = node;
self->size++;
}
return 0;
}
int append(LinkedList* self, int data)
{
return self->add(self, data, self->size);
}
int get(LinkedList* self, int position)
{
if (self->size == 0)
{
printf("The list is empty.");
return 0;
}
else if (position >= self->size || position < 0)
{
printf("Index out of bound.");
return 0;
}
if (position == 0) return self->head->data;
else if (position+1 == self->size) return self->tail->data;
else
{
Node* node = self->head;
for(int i = 0; i < position; i++) node = node->next;
return node->data;
}
}
int rmv (LinkedList* self, int position)
{
int dt;
if (self->size == 0)
{
printf("The list is empty.");
return 0;
}
else if (position >= self->size || position < 0)
{
printf("Index out of bound");
return 0;
}
if (position == 0)
{
Node* head = self->head;
Node* next = head->next;
self->head = next;
dt = head->data;
free(head);
self->size--;
}
else if (position+1 == self->size)
{
Node* node = self->head;
Node* tail = self->tail;
for(int i = 0; i < self->size-2; i++) node = node->next;
node->next = NULL;
self->tail = node;
dt = tail->data;
free(tail);
self->size--;
}
else
{
Node* prev = self->head;
Node* next;
Node* node;
for(int i = 0; i < position-1; i++) prev = prev->next;
node = prev->next;
next = node->next;
prev->next = next;
dt = node->data;
free(node);
self->size--;
}
return dt;
}
void display_list(LinkedList* self)
{
if (self->size == 0) printf("This list is empty.\n\n");
else
{
Node* node = self->head;
printf("[");
for (int i = 0; i < self->size; i++)
{
if (i > 0) printf(", ");
printf("%d", node->data);
node = node->next;
}
printf("]\n\n");
}
}
Node* createNode (int data)
{
Node* node = (Node*) malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
LinkedList createLinkedList ()
{
LinkedList l;
l.head = NULL;
l.tail = NULL;
l.add = &add;
l.append = &append;
l.get = &get;
l.remove = &rmv;
l.display_list = &display_list;
l.createNode = &createNode;
l.size = 0;
return l;
}
typedef struct queue
{
LinkedList items;
int *size;
int (*enqueue) (struct queue*, int);
int (*dequeue) (struct queue*);
int (*peek) (struct queue*, int);
void (*display) (struct queue*);
} Queue;
Queue CreateQueue();
int enqueue(Queue* self, int item);
int dequeue(Queue* self);
int peek(Queue* self, int pos);
void display(Queue* self);
Queue CreateQueue()
{
Queue q;
q.items = createLinkedList();
q.size = &(q.items.size);
q.enqueue = &enqueue;
q.dequeue = &dequeue;
q.peek = &peek;
q.display = &display;
return q;
}
int enqueue(Queue* self, int item)
{
self->items.append(&(self->items), item);
return 1;
}
int dequeue(Queue* self)
{
return self->items.remove(&(self->items), 0);
}
int peek(Queue* self, int pos)
{
return self->items.get(&(self->items), pos);
}
void display(Queue* self)
{
printf("%d items in queue.\n", *(self->size));
self->items.display_list(&(self->items));
}
void main()
{
Queue q = CreateQueue();
q.enqueue(&q, 3);
q.enqueue(&q, 7);
q.enqueue(&q, 4);
q.display(&q);
int item = q.dequeue(&q);
printf("Dequeued: %d\n", item);
q.display(&q);
q.enqueue(&q, 14);
q.display(&q);
}
The part I'm having an issue with is making the Queue's size pointer point to the LinkedList's size integer and then accessing that value.
On compiling and running, I get this:
Output from the above code
Thanks in advance.
The problem is in createQueue:
Queue CreateQueue()
{
Queue q;
q.items = createLinkedList();
q.size = &(q.items.size);
q.enqueue = &enqueue;
q.dequeue = &dequeue;
q.peek = &peek;
q.display = &display;
return q;
}
You set q.size to point to q.items.size. This is a pointer to a local variable. You then return a copy of q, but the size member now points to a local that doesn't exist. Dereferencing a pointer to a variable whose lifetime has ended triggers undefined behavior.
There's no need for the size element in Queue. Just access the size element of the items member directly.

Can't find memory leak in linked lists function "16 bytes in 1 blocks are definitely lost in loss record 1 of 1"

I am learning C and wrote a few functions to work with linked lists in C. However, I have a memory leak (I guess because of the reassignment of the node* list in lines 28 and 29) and was looking for a way to solve it.
My code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int number;
struct node *next;
}
node;
node *declare(int value);
int count(node *list);
node *lead(node *list, int value);
void trail(node *list, int value);
node *append(node *list, int value, int index);
node *fetch_node(node *list, int index);
int fetch(node *list, int index);
int *list_to_array(node *list);
node *array_to_list(int array[], int range);
void free_list(node *list);
node *delete(node *list, int index);
void print(node *list);
int main(void)
{
node *list = declare(1);
list = lead(list, -1);
trail(list, 2);
trail(list, 44);
list = delete(list, 0);
append(list, 232, 1);
int *array = list_to_array(list);
node *list_new = array_to_list(array, 4);
printf("%d\n", fetch(list_new, 3));
print(list);
free_list(list);
free_list(list_new);
free(array);
}
node *declare(int value)
{
node *n = malloc(sizeof(node));
n->number = value;
n->next = NULL;
return n;
}
int count(node *list)
{
node *tmp = list;
int count = 1;
while (tmp->next != NULL)
{
tmp = tmp->next;
count++;
}
return count;
}
node *lead(node *list, int value)
{
node *n = declare(value);
node *tmp = list;
n->next = tmp;
return n;
}
void trail(node *list, int value)
{
node *n = declare(value);
node *tmp = list;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = n;
}
node *append(node *list, int value, int index)
{
if (index == 0)
{
return lead(list, value);
}
else
{
int range = count(list);
node *n = declare(value);
node *prev = fetch_node(list, index - 1);
node *follow = fetch_node(list, index);
prev->next = n;
n->next = follow;
return list;
}
}
node *fetch_node(node *list, int index)
{
node *tmp = list;
if (index < count(list))
{
for (int i = 0; i < index; i++)
{
tmp = tmp->next;
}
}
else
{
fprintf(stderr, "VALUE IN FETCH OUT OF RANGE, RETURNING POINTER TO LIST\n");
}
return tmp;
}
int fetch(node *list, int index)
{
node *fetched = fetch_node(list, index);
return fetched->number;
}
node *delete(node *list, int index)
{
if (index == 0)
{
node *follow = list->next;
list = list->next;
}
else
{
node *fetched = fetch_node(list, index);
node *prev, *follow;
if (index > 0)
{
prev = fetch_node(list, index - 1);
}
else
{
prev = list;
}
int range = count(list);
if (index != range - 1 && index < range)
{
follow = fetch_node(list, index + 1);
}
else
{
follow = NULL;
}
prev->next = follow;
}
return list;
}
int *list_to_array(node *list)
{
int range = count(list);
int *array = malloc(sizeof(int) * range);
for (int i = 0; i < range; i++)
{
array[i] = fetch(list, i);
}
return array;
}
node *array_to_list(int array[], int range)
{
node *list = declare(array[0]);
for (int i = 1; i < range; i++)
{
trail(list, array[i]);
}
return list;
}
void free_list(node *list)
{
int range = count(list);
node *tmp = list;
node *del;
for (int i = 0; i < range; i++)
{
del = tmp;
tmp = tmp->next;
free(del);
}
}
void print(node *list)
{
int range = count(list);
printf("[");
for (int i = 0; i < range; i++)
{
int value = fetch(list, i);
printf("%d", value);
if (i != range - 1)
{
printf(", ");
}
}
printf("]\n");
}
And this is Valgrind output:
==18184== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1
==18184== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18184== by 0x4007C4: declare (linked_lists.c:45)
==18184== by 0x400806: lead (linked_lists.c:65)
==18184== by 0x4006F3: main (linked_lists.c:29)
Any ideas?
Your delete function is creating memory leak because it doesn't free the deleted node.
Add code to free that.
node *delete(node *list, int index)
{
if (index == 0)
{
node *follow = list->next;
// list = list->next;
free(list); // free deleted node
list = follow;
}
else
{
node *fetched = fetch_node(list, index);
node *prev, *follow;
if (index > 0)
{
prev = fetch_node(list, index - 1);
}
else
{
prev = list;
}
int range = count(list);
if (index != range - 1 && index < range)
{
follow = fetch_node(list, index + 1);
}
else
{
follow = NULL;
}
free(prev->next); // free deleted node
prev->next = follow;
}
return list;
}

Bucket Sort Algo

How can i change code here to work with float values in the array, when I'm trying to compile the code, I got an error
so what I need here is my code can work with float values not just int, If i added an array with int values it works fine but with float values it gives me an error
How can i change code here to work with float values in the array, when I'm trying to compile the code, I got an error
so what I need here is my code can work with float values not just int, If i added an array with int values it works fine but with float values it gives me an error
#include <stdio.h>
#include <stdlib.h>
#define NARRAY 100 // Array size
#define NBUCKET 100 // Number of buckets
#define INTERVAL 100 // Each bucket capacity
struct Node {
int data;
struct Node *next;
};
void BucketSort(int arr[]);
struct Node *InsertionSort(struct Node *list);
void print(int arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(int value);
// Sorting function
void BucketSort(int arr[]) {
int i, j;
struct Node **buckets;
// Create buckets and allocate memory size
buckets = (struct Node **)malloc(sizeof(struct Node *) * NBUCKET);
// Initialize empty buckets
for (i = 0; i < NBUCKET; ++i) {
buckets[i] = NULL;
}
// Fill the buckets with respective elements
for (i = 0; i < NARRAY; ++i) {
struct Node *current;
int pos = getBucketIndex(arr[i]);
current = (struct Node *)malloc(sizeof(struct Node));
current->data = arr[i];
current->next = buckets[pos];
buckets[pos] = current;
}
// Print the buckets along with their elements
for (i = 0; i < NBUCKET; i++) {
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}
// Sort the elements of each bucket
for (i = 0; i < NBUCKET; ++i) {
buckets[i] = InsertionSort(buckets[i]);
}
printf("-------------\n");
printf("Bucktets after sorting\n");
for (i = 0; i < NBUCKET; i++) {
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}
// Put sorted elements on arr
for (j = 0, i = 0; i < NBUCKET; ++i) {
struct Node *node;
node = buckets[i];
while (node) {
arr[j++] = node->data;
node = node->next;
}
}
return;
}
// Function to sort the elements of each bucket
struct Node *InsertionSort(struct Node *list) {
struct Node *k, *nodeList;
if (list == 0 || list->next == 0) {
return list;
}
nodeList = list;
k = list->next;
nodeList->next = 0;
while (k != 0) {
struct Node *ptr;
if (nodeList->data > k->data) {
struct Node *tmp;
tmp = k;
k = k->next;
tmp->next = nodeList;
nodeList = tmp;
continue;
}
for (ptr = nodeList; ptr->next != 0; ptr = ptr->next) {
if (ptr->next->data > k->data)
break;
}
if (ptr->next != 0) {
struct Node *tmp;
tmp = k;
k = k->next;
tmp->next = ptr->next;
ptr->next = tmp;
continue;
} else {
ptr->next = k;
k = k->next;
ptr->next->next = 0;
continue;
}
}
return nodeList;
}
int getBucketIndex(int value) {
return value / INTERVAL;
}
void print(int ar[]) {
int i;
for (i = 0; i < NARRAY; ++i) {
printf("%d ", ar[i]);
}
printf("\n");
}
// Print buckets
void printBuckets(struct Node *list) {
struct Node *cur = list;
while (cur) {
printf("%d ", cur->data);
cur = cur->next;
}
}
// Driver code
int main(void) {
int array[NARRAY] = {0.50, 100.00, 99.97, 51.20, 53.90, 28.10, 25.50, 66.40, 65.70, 0.00};
printf("Initial array: ");
print(array);
printf("-------------\n");
BucketSort(array);
printf("-------------\n");
printf("Sorted array: ");
print(array);
return 0;
}
Use this code. I've made my own datatype iORf. Use typedef int iORf and typedef float iORf for int and float respectively. You have to switch manually for this.
#include <stdio.h>
#include <stdlib.h>
#define NARRAY 100 // Array size
#define NBUCKET 100 // Number of buckets
#define INTERVAL 100 // Each bucket capacity
typedef int iORf; //float or int, currently int
struct Node
{
iORf data;
struct Node *next;
};
void BucketSort(iORf arr[]);
struct Node *InsertionSort(struct Node *list);
void print(iORf arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(iORf value);
// Sorting function
void BucketSort(iORf arr[])
{
int i, j;
struct Node **buckets;
// Create buckets and allocate memory size
buckets = (struct Node **)malloc(sizeof(struct Node *) * NBUCKET);
// Initialize empty buckets
for (i = 0; i < NBUCKET; ++i)
{
buckets[i] = NULL;
}
// Fill the buckets with respective elements
for (i = 0; i < NARRAY; ++i)
{
struct Node *current;
int pos = getBucketIndex(arr[i]);
current = (struct Node *)malloc(sizeof(struct Node));
current->data = arr[i];
current->next = buckets[pos];
buckets[pos] = current;
}
// Print the buckets along with their elements
for (i = 0; i < NBUCKET; i++)
{
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}
// Sort the elements of each bucket
for (i = 0; i < NBUCKET; ++i)
{
buckets[i] = InsertionSort(buckets[i]);
}
printf("-------------\n");
printf("Bucktets after sorting\n");
for (i = 0; i < NBUCKET; i++)
{
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}
// Put sorted elements on arr
for (j = 0, i = 0; i < NBUCKET; ++i)
{
struct Node *node;
node = buckets[i];
while (node)
{
arr[j++] = node->data;
node = node->next;
}
}
return;
}
// Function to sort the elements of each bucket
struct Node *InsertionSort(struct Node *list)
{
struct Node *k, *nodeList;
if (list == 0 || list->next == 0)
{
return list;
}
nodeList = list;
k = list->next;
nodeList->next = 0;
while (k != 0)
{
struct Node *ptr;
if (nodeList->data > k->data)
{
struct Node *tmp;
tmp = k;
k = k->next;
tmp->next = nodeList;
nodeList = tmp;
continue;
}
for (ptr = nodeList; ptr->next != 0; ptr = ptr->next)
{
if (ptr->next->data > k->data)
break;
}
if (ptr->next != 0)
{
struct Node *tmp;
tmp = k;
k = k->next;
tmp->next = ptr->next;
ptr->next = tmp;
continue;
}
else
{
ptr->next = k;
k = k->next;
ptr->next->next = 0;
continue;
}
}
return nodeList;
}
int getBucketIndex(iORf value)
{
return (int)value / INTERVAL;
}
void print(iORf ar[])
{
int i;
int flag = 0;
iORf dummy = 1.5;
if (dummy > 1)
flag++;
for (i = 0; i < NARRAY; ++i)
{
if (flag > 0)
printf("%f ", ar[i]);
else
printf("%d ", ar[i]);
}
printf("\n");
}
// Print buckets
void printBuckets(struct Node *list)
{
struct Node *cur = list;
while (cur)
{
printf("%d ", cur->data);
cur = cur->next;
}
}
// Driver code
int main(void)
{
iORf array[NARRAY] = {0.5, 100.00, 99.97, 51.20, 53.90, 28.10, 25.50, 66.40, 65.70, 0.00};
printf("Initial array: ");
print(array);
printf("-------------\n");
BucketSort(array);
printf("-------------\n");
printf("Sorted array: ");
print(array);
return 0;
}

Binary tree implementation C

The 't' pointer in the insert() function is set to NULL every time I call the insert function. Is it something to do with messing up of pointers?
int main() {
int num, i;
tree *t;
t = NULL;
for(i = 0; i < 5; i++) {
scanf("%d", &num);
insert(t, num);
}
//inorder(t);
return 0;
}
The insert function is as follows:
void insert(tree *t, int num) {
int flag;
tree *p, *q;
tree *temp = (tree *)malloc(sizeof(tree));
temp->data = num;
temp->left = NULL;
temp->right = NULL;
if(t == NULL) {
t = temp;
printf("Hello");
return;
}
printf("%d", t->data);
p = t;
while(p) {
q = p;
if(p->data <= num) {
p = p->right;
flag = 1;
}
else {
p = p->left;
flag = 0;
}
if(flag == 1)
q->right = temp;
else
q->left = temp;
}
}
My tree structure is as follows :
typedef struct tree {
int data;
struct tree *left, *right;
}tree;

Segmentation fault error in BST

I keep getting a segmentation fault error when I call the find_word function in my main.
when a word is added I want to return 1 and when it finds that word, I want it to return 1.
So I'm also not sure if my insert method is correct either.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct node {
char *word;
struct node *left;
struct node *right;
};
static struct node *root;
int init(void)
{
struct node *new_node = malloc (sizeof(struct node));
if(new_node==NULL){
return 0;
}
else{
root = new_node;
new_node->left = NULL;
new_node->right = NULL;
return 1;
}
}
static int insert(struct node *newnode, char *word)
{
struct node *temp = NULL;
if(!(newnode))
{
temp = (struct node *)malloc(sizeof(struct node));
temp->left =NULL;
temp->right = NULL;
temp->word = word;
newnode = temp;
return 0;
}
if(word < (newnode)->word)
{
insert((newnode)->left, word);
}
else if(word > (newnode)->word)
{
insert((newnode)->right, word);
}
return 1;
}
int add_word(char *word)
{
return insert(root,word);
}
static int find(char *word, struct node *newnode){
if(newnode==NULL){
return 0;
}
else if(strcmp(word,newnode->word)>0){
find(word,newnode->left);
}
else if(strcmp(newnode->word,word)<0){
find(word,newnode->right);
}
else{
return 1;
}
return 0;
}
int find_word(char *word)
{
return find(word,root);
}
int main(int argc,char *argv[])
{
int k;
char l[5];
k = init();
printf("init: %d\n",k);
strcpy(l,"x");
k = add_word(l);
printf("add_word(%s): %d\n",l,k);
strcpy(l,"x");
k = find_word(l);
printf("find_word(%s): %d\n",l,k);
return 0;
}
to fix like this
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct node {
char *word;
struct node *left;
struct node *right;
};
static struct node *root = NULL;
static int insert(struct node **newnode, char *word){
struct node *temp = NULL;
int cmp;
if(!*newnode){
temp = (struct node *)malloc(sizeof(struct node));
temp->left =NULL;
temp->right = NULL;
temp->word = strdup(word);
*newnode = temp;
return 0;
}
if((cmp=strcmp(word, (*newnode)->word)) < 0)
return insert(&(*newnode)->left, word);
if(cmp > 0)
return insert(&(*newnode)->right, word);
return 1;
}
int add_word(char *word){
return insert(&root, word);
}
static int find(char *word, struct node *newnode){
int cmp;
if(newnode==NULL)
return 0;
if((cmp=strcmp(word, newnode->word)) == 0)
return 1;
if(cmp < 0)
return find(word, newnode->left);
return find(word, newnode->right);
}
int find_word(char *word){
return find(word, root);
}
int main(int argc,char *argv[]){
int k;
char *w;
k = add_word(w="x");
printf("add_word(%s): %d\n", w, k);
k = find_word(w);
printf("find_word(%s): %d\n", w, k);
return 0;
}
If newnode->word is NULL, you should insert the word at the current node, to handle the empty root node.
static int insert(struct node *newnode, char *word)
{
struct node *temp = NULL;
if(!(newnode))
{
temp = (struct node *)malloc(sizeof(struct node));
temp->left =NULL;
temp->right = NULL;
temp->word = malloc(strlen(word)+1);
strcpy(temp->word, word);
newnode = temp;
return 0;
}
if (newnode->word == NULL) {
newnode->word = malloc(strlen(word)+1);
strcpy(newnode->word, word);
return 1;
}
if(strcmp(word,(newnode)->word) < 0)
{
insert((newnode)->left, word);
}
else if(strcmp(word,(newnode)->word) > 0)
{
insert((newnode)->right, word);
}
return 1;
}
In your find function, you call strcmp twice. You swap the order of the arguments, but you also change > 0 to < 0. These cancel each other out, so both are testing the same thing. You need to change one or the other, but not both. You should also check for newnode->word == NULL.
static int find(char *word, struct node *newnode){
if(newnode==NULL || newnode->word == NULL){
return 0;
}
else if(strcmp(word,newnode->word)>0){
find(word,newnode->left);
}
else if(strcmp(word,newnode->word)<0){
find(word,newnode->right);
}
else{
return 1;
}
return 0;
}

Resources