gpt4 book ai didi

c - 在 C 中保存结构地址的变量的数据类型应该是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 02:21:19 25 4
gpt4 key购买 nike

<分区>

我正在尝试从链表中删除一个节点。 “间接”变量的数据类型应该是什么?

我正在尝试使用指针从链表中删除一个节点。我无法确定函数 remove_list_entry 中变量“indirect”的类型。 remove_list_entry 期望一个节点作为输入被移除。

数据类型应该是什么?

struct Node
{
int data;
struct Node* next;
};

void remove_list_entry(struct Node* entry)
{
indirect=&head;

while ((*indirect) != entry)
indirect = &(*indirect)->next;

*indirect = entry->next;
}

编辑:- 所以下面是这个问题的解决方案

#include <stdlib.h> 

struct Node
{
int data;
struct Node* next;
};

void remove_list_entry(struct Node* entry, struct Node* head)
{
struct Node** indirect=&head;

while ((*indirect) != entry)
indirect = &(*indirect)->next;

*indirect = entry->next;
}

void print_list(struct Node* ptr)
{
while(ptr!=NULL)
{
printf("%d--> ",ptr->data);
ptr=ptr->next;
}
}
int main()
{
struct Node* head = NULL;

struct Node* first = NULL;
struct Node* second = NULL;
struct Node* third = NULL;

first = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));

first->data = 1;
first->next = second;

second->data = 2;
second->next = third;

third->data = 3;
third->next = NULL;

head = first;
printf("Initial Linked List\n");
print_list(head);

printf("\n\nNode to remove %d",second->data);
remove_list_entry(second,head);

printf("\n\nAfter deletion\n");
print_list(head);

return 0;
}

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