如何在c中删除链表中的头?
该程序应删除单链表中的N节点。如果我把N=1或N=2放进去,就可以了,程序可以运行了。但当N=0时,输出将打印带有随机值的无限节点(删除节点0后)。我想这个节目看不到新的头像。谢谢你的帮助如何在c中删除链表中的头?,c,pointers,struct,linked-list,singly-linked-list,C,Pointers,Struct,Linked List,Singly Linked List,该程序应删除单链表中的N节点。如果我把N=1或N=2放进去,就可以了,程序可以运行了。但当N=0时,输出将打印带有随机值的无限节点(删除节点0后)。我想这个节目看不到新的头像。谢谢你的帮助 #include <stdio.h> #include <stdlib.h> #define N 0 struct node { int data; struct node *next; }; void printlist(struct node *head){
#include <stdio.h>
#include <stdlib.h>
#define N 0
struct node {
int data;
struct node *next;
};
void printlist(struct node *head){
struct node *current=head;
int i=0;
while (current!=NULL){
printf("node number %d \t : %d\n", i, current->data);
current=current->next;
i++;
}
}
int deletenode(struct node *head,int n){
struct node *current=head;
struct node *previous=head;
int i=0;
while(current!= NULL){
if (n==i && i!=0){
previous->next=current->next;
free(current);
return 1;
}
else if (n==i && i==0){
head=head->next;
free(current);
return 1;
}
i++;
previous=current;
current=current->next;
return 0;
}
printf("error\n");
exit(EXIT_FAILURE);
}
void main(){
struct node *n1=malloc(sizeof(struct node));
struct node *n2=malloc(sizeof(struct node));
struct node *n3=malloc(sizeof(struct node));
struct node *head=n1;
n1->data=5;
n1->next=n2;
n2->data=10;
n2->next=n3;
n3->data=15;
n3->next=NULL;
printf("\n\nbefore\n");
printlist(head);
deletenode(head,N);
printf("\n\nafter\n");
printlist(head);
}
#包括
#包括
#定义n0
结构节点{
int数据;
结构节点*下一步;
};
无效打印列表(结构节点*头){
结构节点*当前=头部;
int i=0;
while(当前!=NULL){
printf(“节点号%d\t:%d\n”,i,当前->数据);
当前=当前->下一步;
i++;
}
}
int deletenode(结构节点*头,int n){
结构节点*当前=头部;
结构节点*previous=头部;
int i=0;
while(当前!=NULL){
如果(n==i&&i!=0){
上一个->下一个=当前->下一个;
自由(电流);
返回1;
}
else如果(n==i&&i==0){
头部=头部->下一步;
自由(电流);
返回1;
}
i++;
先前=当前;
当前=当前->下一步;
返回0;
}
printf(“错误\n”);
退出(退出失败);
}
void main(){
结构节点*n1=malloc(sizeof(结构节点));
结构节点*n2=malloc(sizeof(结构节点));
结构节点*n3=malloc(sizeof(结构节点));
结构节点*head=n1;
n1->数据=5;
n1->next=n2;
n2->数据=10;
n2->next=n3;
n3->数据=15;
n3->next=NULL;
printf(“\n\n前\n”);
印刷品清单(标题);
删除节点(头,N);
printf(“\n\n之后”);
印刷品清单(标题);
}
我使用
current
作为临时指针,因为在第二个节点上的head移位之后,我需要一个指向旧head的指针,并使用free。C总是通过值传递,因此更改参数对调用方没有影响
void foo(int i) {
i = 1234; // No effect on caller.
}
void foo(int *p) {
p = NULL; // No effect on caller.
}
如果要修改变量(例如调用方的头
),则需要向其传递指针。(仍然可以更改指针引用的对象。)
现在,您可以简单地用(*head)
替换代码中head
的每个实例,以说明新的调用约定,但这将浪费简化的机会。通过将指针指向一个结构节点*
,我们不需要以不同的方式处理头
(一个结构节点*
)和上一个节点->下一个
(一个结构节点*
)
int delete_node_n(struct node **ptr, unsigned n) {
// Make `ptr` point to the pointer we want to modify.
// This will either be the `head` variable
// or the `next` field of some node.
while (1) {
if (!*ptr)
return 0;
if (!n)
break;
ptr = &( (*ptr)->next );
--n;
}
struct node *to_free = *ptr;
*ptr = (*ptr)->next;
free(to_free);
return 1;
}
伙计,我会努力的。你能帮我理解为什么在这种情况下我需要一个指向指针的指针吗?因为C总是通过值传递。更改参数对调用方没有影响。Added to answer.deletenode应返回指向新标头的指针
int delete_node_n(struct node **ptr, unsigned n) {
// Make `ptr` point to the pointer we want to modify.
// This will either be the `head` variable
// or the `next` field of some node.
while (1) {
if (!*ptr)
return 0;
if (!n)
break;
ptr = &( (*ptr)->next );
--n;
}
struct node *to_free = *ptr;
*ptr = (*ptr)->next;
free(to_free);
return 1;
}