0

i am new to programming and C. I am trying to create an ordered linked list. For some reason which i cannot figure out, it never enters the first if block in the insert_in_order function even though my linked list is empty when i call the insert_in_order function. Would anyone have any idea what i am doing wrong?

Here is my code:

#include <stdio.h>
#include <stdlib.h>

struct node {
    int value; 
    struct node* next;
    
    };
    typedef struct node node_t;
    
    
void printlist(node_t *head){
    node_t *temporary = head;
    
    while(temporary != NULL){
        printf("%d - ", temporary->value);
        temporary = temporary->next;
        }
    printf("\n");
    
    }

node_t *create_new_node(int value){
    node_t *result = malloc(sizeof(node_t));
    result->value = value;
    result->next = NULL;
    //printf("result.value = %d\n", result->value);
    return result;
    }

void insert_in_order (node_t *head, node_t *node_to_insert){
    node_t *current_node = head;
    node_t *prior_node = head;

    //linked list is empty
    if (head == NULL){ //never enters if block for some reason
        head->next = node_to_insert;
        node_to_insert->next = NULL;
        break;
        //printf("inside the if stmt"); 
        } 
        
    if(node_to_insert->value <= current_node->value){
        head->next = node_to_insert;
        node_to_insert->next = current_node;
        break;
        }   
       
    current_node = current_node->next;
        
    while (current_node->next != NULL){
        
        if(node_to_insert->value <= current_node->value){
            node_to_insert->next = current_node;
            prior_node->next = node_to_insert;
            break;
            }
        
        else if (node_to_insert > current_node){
            current_node = current_node->next;
            prior_node = prior_node->next;
            }
        
        }
    //node to insert is the largest in the linked list
    current_node->next = node_to_insert;
    node_to_insert->next = NULL;
    
    }

int main(){
    node_t *head;
    node_t *node1;
    node_t *node2;
    
    head = NULL;
    node1 = create_new_node(22);
    node2 = create_new_node(33);
    printf("node1's value equals %d\n", node1->value);
    printf("node2's value equals %d\n", node2->value);

    insert_in_order(head, node1);
    

    printlist(head);
}

1 Answers1

1

First of all this code does not compile - these breaks are invalid

if (head == NULL){ //never enters if block for some reason
    head->next = node_to_insert;
    node_to_insert->next = NULL;
    break; <<<<====
    //printf("inside the if stmt"); 
} 

and

if (node_to_insert->value <= current_node->value) {
    head->next = node_to_insert;
    node_to_insert->next = current_node;
    break; <<<=====
}

Seems like you meant return when you said break, now compiles with those replaced by return

Now this goes wrong

//linked list is empty
if (head == NULL) { //never enters if block for some reason
    head->next = node_to_insert;

You just tested to see if head is NULL and if it is you try to use it, thats never going to work

You mean this

//linked list is empty
if (head == NULL) { //never enters if block for some reason
    head = node_to_insert;
    node_to_insert->next = NULL;
    return;
}

code now runs to completion, although there may be other errors

node1's value equals 22
node2's value equals 33
pm100
  • 48,078
  • 23
  • 82
  • 145