C 使用递归的链表-意外输出
我在互联网上发现了使用递归反转列表的方法,并将其应用于代码块中,但输出仅反转打印来自主函数的最后两个插入调用。它跳过前三个Insert调用。为什么?我确实在这里搜索过这个问题,但由于我是一个初学者,所以我没能理解它们。好心帮忙C 使用递归的链表-意外输出,c,recursion,linked-list,C,Recursion,Linked List,我在互联网上发现了使用递归反转列表的方法,并将其应用于代码块中,但输出仅反转打印来自主函数的最后两个插入调用。它跳过前三个Insert调用。为什么?我确实在这里搜索过这个问题,但由于我是一个初学者,所以我没能理解它们。好心帮忙 #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node * head; struct Nod
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node * head;
struct Node* Insert (struct Node* head, int data)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return;
}
struct Node* temp2 = head;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
void reversePrint(struct Node* head)
{
if(head == NULL)
{
printf("\n");
return;
}
reversePrint(head->next);
printf(" %d ", head->data);
return;
}
int main()
{
struct Node* head = NULL;
head = Insert(head,2);
head = Insert(head,7);
head = Insert(head,3);
head = Insert(head,1);
head = Insert(head,4);
reversePrint(head);
return 0;
}
#包括
#包括
结构体类型
{
int数据;
结构节点*下一步;
};
结构节点*头部;
结构节点*插入(结构节点*头,整数数据)
{
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=数据;
temp->next=NULL;
if(head==NULL)
{
压头=温度;
返回;
}
结构节点*temp2=头部;
while(temp2->next!=NULL)
{
temp2=temp2->next;
}
temp2->next=temp;
}
void reversePrint(结构节点*头)
{
if(head==NULL)
{
printf(“\n”);
返回;
}
反向打印(头->下一步);
printf(“%d”,头部->数据);
返回;
}
int main()
{
结构节点*head=NULL;
头部=插入件(头部,2);
头部=插入件(头部,7);
头=插入件(头,3);
头=插入件(头,1);
头部=插入件(头部,4);
反向打印(头);
返回0;
}
O/p:41注:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node * head;
void Insert (int data)
{
struct Node* temp = malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return;
}
struct Node* temp2 = head;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
void reversePrint(struct Node* head)
{
if(head == NULL)
{
printf("\n");
return;
}
reversePrint(head->next);
printf(" %d ", head->data);
return;
}
int main()
{
Insert(2);
Insert(7);
Insert(3);
Insert(1);
Insert(4);
reversePrint(head);
return 0;
}
- 不要强制转换malloc的返回值
- 你声明了两个
,弄糊涂了*头
- 当您将head声明为全局时,无需将指针传递给函数并返回指针。这不是一个好主意,但我遵循了你的代码
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node * head;
void Insert (int data)
{
struct Node* temp = malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return;
}
struct Node* temp2 = head;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
void reversePrint(struct Node* head)
{
if(head == NULL)
{
printf("\n");
return;
}
reversePrint(head->next);
printf(" %d ", head->data);
return;
}
int main()
{
Insert(2);
Insert(7);
Insert(3);
Insert(1);
Insert(4);
reversePrint(head);
return 0;
}
#包括
#包括
结构体类型
{
int数据;
结构节点*下一步;
};
结构节点*头部;
无效插入(整型数据)
{
结构节点*temp=malloc(sizeof(结构节点));
温度->数据=数据;
temp->next=NULL;
if(head==NULL)
{
压头=温度;
返回;
}
结构节点*temp2=头部;
while(temp2->next!=NULL)
{
temp2=temp2->next;
}
temp2->next=temp;
}
void reversePrint(结构节点*头)
{
if(head==NULL)
{
printf(“\n”);
返回;
}
反向打印(头->下一步);
printf(“%d”,头部->数据);
返回;
}
int main()
{
插入(2);
插入(7);
插入(3);
插入(1);
插入(4);
反向打印(头);
返回0;
}
输出:
41372
Insert
实际上并没有返回值。(您需要返回头;
)@BLUEPIXY非常感谢:)@Vector阅读以下内容: