Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在链表的第n个位置添加包含多位数的节点_C_Data Structures_Linked List_Dynamic Memory Allocation - Fatal编程技术网

C 在链表的第n个位置添加包含多位数的节点

C 在链表的第n个位置添加包含多位数的节点,c,data-structures,linked-list,dynamic-memory-allocation,C,Data Structures,Linked List,Dynamic Memory Allocation,我已经编写了一个在第n个位置插入节点的代码。 当用户在一个节点中输入1位数字时,它工作正常,但当用户输入等于或大于2位数字时,它将继续在无限循环中打印最后一个节点 我不知道出了什么问题。我的代码在下面 #include<stdio.h> #include<stdlib.h> struct st { int roll; char name[20]; struct st *next; }; void add_middle(struct st **p

我已经编写了一个在第n个位置插入节点的代码。 当用户在一个节点中输入1位数字时,它工作正常,但当用户输入等于或大于2位数字时,它将继续在无限循环中打印最后一个节点

我不知道出了什么问题。我的代码在下面

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

struct st
{
    int roll;
    char name[20];
    struct st *next;
};

void add_middle(struct st **ptr)
{
    struct st *temp,*temp1;
    temp=malloc(sizeof(struct st ));
    printf("netre ur name\n");
    scanf("%s",temp->name);
    printf("enter ur roll\n");
    scanf("%d",&(temp->roll));

    if((*ptr==NULL)||(temp->roll<(*ptr)->roll))
    {
        temp->next=*ptr;
        *ptr=temp;
    }
    else
    {
        temp1=*ptr;
        while(temp1)
        {
            if((temp1->next==NULL)||(temp1->next->roll>temp->roll))
            {
                temp1->next=temp;
                temp->next=temp1->next;
                break;
            }
            temp1=temp1->next;
        }

    }
}

void display(struct st *ptr)
{
    while(ptr)
    {
        printf("%s %d\n",ptr->name,ptr->roll);
        ptr=ptr->next;
    }
}

main()
{
    struct st *headptr=0;
    add_middle(&headptr);`
        add_middle(&headptr);
    add_middle(&headptr);
    display(headptr);
}

让我们看看插入新节点时会发生什么:

temp1->next = temp;
temp->next = temp1->next;
这将使以前的节点temp1指向新节点,这很好。然后,它将使新节点temp指向自身temp1->next==temp,这是错误的

要解决这个问题,您可以交换这两行。即:

if ((temp1->next==NULL) || (temp1->next->roll > temp->roll)) {
    temp->next = temp1->next;
    temp1->next = temp;
    break;
}
此外,如果使用更好的变量名,这可能会更清楚:

temp1变为previousNode 临时变为新节点
让我们看看插入新节点时会发生什么:

temp1->next = temp;
temp->next = temp1->next;
这将使以前的节点temp1指向新节点,这很好。然后,它将使新节点temp指向自身temp1->next==temp,这是错误的

要解决这个问题,您可以交换这两行。即:

if ((temp1->next==NULL) || (temp1->next->roll > temp->roll)) {
    temp->next = temp1->next;
    temp1->next = temp;
    break;
}
此外,如果使用更好的变量名,这可能会更清楚:

temp1变为previousNode 临时变为新节点
我假设您的代码中有一个错误:add_middle&headptr;`?请注意该行末尾的`尽管缺少输入验证,但如果您利用指针指向指针来实现它的最佳功能,您可以使这一点变得相当容易。无论如何,祝你好运。我希望代码从头部开始查看每个现有节点,如果没有节点,则将现有节点与新节点进行比较,否则只更新现有代码。下一步使用ptr更新新节点和新节点。下一步=null。如果新节点大于现有节点,则更新现有节点到新节点和新节点到先前现有节点的两个下一个指针。作为建议,temp和temp1这样的名称非常容易混淆。最好根据它们的实际内容来命名它们。我假设您的代码中有一个错误:add_middle&headptr;`?请注意该行末尾的`尽管缺少输入验证,但如果您利用指针指向指针来实现它的最佳功能,您可以使这一点变得相当容易。无论如何,祝你好运。我希望代码从头部开始查看每个现有节点,如果没有节点,则将现有节点与新节点进行比较,否则只更新现有代码。下一步使用ptr更新新节点和新节点。下一步=null。如果新节点大于现有节点,则更新现有节点到新节点和新节点到先前现有节点的两个下一个指针。作为建议,temp和temp1这样的名称非常容易混淆。最好以与其实际内容相关的方式命名它们。