简体   繁体   English

我正在尝试按升序将数字添加到链接列表中。 我该如何解决才能正常工作? 这是我的代码:

[英]I'm trying to add numbers into a linked list in ascending order. what can I fix so it'll work? Here's my code:

This code is supposed to put numbers into a linked list in ascending order. 此代码应按升序将数字放入链表中。 This is one function of my program that takes keyboard input(int x) from the user of the program. 这是我程序的一项功能,该功能从程序用户处获取键盘输入(int x)。

node* insert(node *head, int x)
{
    node *newPtr;
    node *prePtr;
    node *currentPtr;

    newPtr = malloc(sizeof(node));

    if(newPtr != NULL){
            newPtr->value = x;
            newPtr->next = NULL;

            prePtr = NULL;
            currentPtr = head;

            while(currentPtr != NULL && x > currentPtr->value){
                    prePtr = currentPtr;
                    currentPtr = currentPtr->next;
            }

            if(prePtr == NULL){
                    newPtr->next = head;
                    head = newPtr;
            }
            else{
                    prePtr->next = newPtr;
                    newPtr->next = currentPtr;
            }
    }
}
    int main(void)//calling input function in main
    {
        node *head = NULL;
        int x=0;
        while(x!=-1){
                printf("?: ");
                scanf("%d", &x);
                head=insert(head,x);
                print(head);
                printf("\n");
        }
        return 0;
    }

//It seems to only put a few numbers in, then it resets

The main() function asks for a numerical input, and sends that input into the insert function which is supposed to put the numbers in ascending order. main()函数要求输入数字,然后将该输入发送到insert函数中,该函数应该将数字按升序排列。 Sample output: 样本输出:

$ gcc prelab2.c
$ ./a.out
?: 4
4 -> NULL
?: 3
3 -> 4 -> NULL
?: 9
3 -> 4 -> 9 -> NULL
?: 7
3 -> 4 -> 7 -> 9 -> NULL
?: 2
2 -> 3 -> 4 -> 7 -> 9 -> NULL
?: -1

There is only a small mistake. 只有一个小错误。 In main() method 在main()方法中

head=insert(head,x);

your Insert method return nothing (NULL). 您的Insert方法不返回任何内容(NULL)。 so your head is never changed it is always NULL; 因此,您的头永不改变,始终为NULL;

return head;

Just return head in insert method and it will work fine. 只需将头返回insert方法,它将正常工作。

Try the following changes it should work fine. 尝试以下更改,它应该可以正常工作。

node* insert(node *head, int x)
{
node *newPtr;
node *prePtr;
node *currentPtr;

newPtr = malloc(sizeof(node));

if(newPtr != NULL)
{
        newPtr->value = x;
        newPtr->next = NULL;
        // check if the linked list is empty add the node directly and return 
        if(head == NULL)
        {
            head = newptr;
            return head;

        }
        else 
        {

        currentPtr = head;

        while(x > currentPtr->value && currentPtr->next != NULL)
        {         
                currentPtr = currentPtr->next;
        }
             // make the new node point where current next is pointing
               newPtr->next = currentPtr->next;
             // make the current point to new node
               currentPtr->next = newPtr; 
        }
return head;
}
else
     return NULL; // signifying malloc failed

}

EDIT: corrected the code missed out a condition check while copying 编辑:更正了代码,在复制时错过了条件检查

暂无
暂无

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

相关问题 以升序将元素插入单链列表中。 - To insert the elements in a singly linked list in ascending order. 我正在尝试在 VS 代码上运行 C,这就是终端上的内容。 我怎样才能解决这个问题? - I'm trying to run C on VS code and this is what's on terminal. How can I fix this? 我将如何修改这个单链表排序算法,使其按升序正确排序? - How would I modify this singly linked list sorting algorithm so that it sorts by ascending order properly? 我正在制作一个必须交换顺序的链表 - I'm making a linked list that has to swap it's order 我正在尝试使用if语句以升序方式排列三个3号,但这不适用于第三个数字。 该怎么办? - I am trying to arrange three 3 numbers in ascending manner using if statement, but this doesn't work for the third number. What can be done about it? 这里有什么问题? 我没有连接我的字符串? - What's wrong here? I'm not getting my string concatenated? 如何按升序对双向链表进行排序? - How do I sort a doubly-linked list in ascending order? 如何在我的链表实现中修复此内存泄漏? - How can I fix this memoryleak in my implementaion of linked list? 如何修复我的代码以清除链接结构中的节点 - How can I fix my code for clearing nodes in a linked structure “ For循环”以升序排列数字。 [ERROR]指针和整数比较 - 'For loop' to arrange Numbers in ascending order. [ERROR] pointer and integer comparision
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM