gpt4 book ai didi

c - 在双向链表中的元素之后插入

转载 作者:行者123 更新时间:2023-11-30 15:22:40 25 4
gpt4 key购买 nike

我正在链接列表中的元素后面插入元素,但我的代码未运行。

typedef struct Node
{
int info;
struct Node *next;
struct Node *prev;
}node;

node *head;


// w-the element to be inserted & z-the position after which it has to inserted

void insertpos(int w,int z)
{
int i;
node *ptr=head;
node *ptr1;
for(i=1;i<=z-1;i++)
{
ptr=ptr->next;
}
ptr1=(node*)malloc(sizeof(node));
ptr1->info=w;
ptr->next=ptr1;
((ptr->next)->next)->prev=ptr1;
}

最佳答案

   ptr->next=ptr1;
((ptr->next)->next)->prev=ptr1;

您正在更改新创建的指针 ptr1 旁边的 ptr。 ptr1 的 next 在这里显然是 null 。你必须让它指向 ptr 的下一个节点

ptr1->next = ptr->next

然后你必须让ptr指向ptr1

ptr->next = ptr1

它会起作用,请发布您在控制台中看到的错误。

关于c - 在双向链表中的元素之后插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29142567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com