繁体   English   中英

无法插入 C 中的有序链表

[英]unable to insert into ordered linked list in C

我是编程新手和 C。我正在尝试创建一个有序链表。 由于某种我无法弄清楚的原因,它永远不会进入 insert_in_order function 中的第一个 if 块,即使当我调用 insert_in_order function 时我的链表是空的。有人知道我做错了什么吗?

这是我的代码:

#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);
}

首先,这段代码无法编译——这些中断是无效的

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; <<<=====
}

当你说break时,你的意思似乎是return ,现在用return替换的那些编译

现在出了问题

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

您刚刚测试过 head 是否为 NULL,如果您尝试使用它,那将永远无法工作

你是这个意思

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

代码现在运行完成,尽管可能还有其他错误

node1's value equals 22
node2's value equals 33

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM