提供给函数的参数变为 NULL?
Supplied parameter to the function becomes NULL?
我写了一个递归函数来反转链表如下:
struct node{
int val;
struct node *next;
};
//Global pointer to structure
struct node *start=NULL,*head=NULL;
//*Function to input node*
void create(int data){
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(start == NULL){
temp->val=data;
temp->next=NULL;
start=temp;
head=temp;
}
else{
temp->val=data;
temp->next=NULL;
head->next=temp;
head=temp;
}
}
*Function to reverse the linked list*
void* rev(struct node *prev,struct node *cur){
if(cur!=NULL){
printf("Works");
rev(cur,cur->next);
cur->next=prev;
}
else{
start=prev;
}
}
而main中的相关代码是:
main(){
struct node *temp;
temp=start;
/*Code to insert values*/
rev(NULL,temp);
}
现在代码接受输入并完美地打印出来,但是在我调用 rev()
函数之后,相同的遍历函数什么也没有打印出来。
我在调试器上逐行 运行 代码 n 它给了我以下输出:
rev (prev=0x0, cur=0x0)
此外,由于 cur
不知何故为 NULL,因此 rev()
的 if
部分永远不会执行,只有 else
执行一次。
当我在我的 create()
函数中输入时,我会更新链表的第一个元素,甚至在 main 中打印语句证明它是这样的。
但是为什么函数 rev()
总是接收 NULL 的输入参数?
如果需要任何额外信息,请发表评论。
您的代码存在特定问题:您的 main()
函数缺少足够的代码来测试反转功能(例如,它不创建任何节点!);您的 create()
例程确实需要 head
和 tail
指针才能正常工作,而不是当前的 head
和 start
;您的反转函数维护 head/start 指针但不处理尾指针;您的 if
和 else
子句中有冗余代码,可以从条件语句中删除;你声明 rev()
为 void *
而不是简单的 void
.
我修改了下面的代码,解决了上述更改以及一些样式问题:
#include <stdlib.h>
#include <stdio.h>
struct node {
int value;
struct node *next;
};
// Global pointers to structure
struct node *head = NULL, *tail = NULL;
// Function to add node
void create(int data) {
struct node *temporary = malloc(sizeof(struct node));
temporary->value = data;
temporary->next = NULL;
if (head == NULL) {
head = temporary;
} else {
tail->next = temporary;
}
tail = temporary;
}
// Function to reverse the linked list
void reverse(struct node *previous, struct node *current) {
if (current != NULL) {
reverse(current, current->next);
current->next = previous;
} else {
head = previous;
}
if (previous != NULL) {
tail = previous;
}
}
void display(struct node *temporary) {
while (temporary != NULL) {
printf("%d ", temporary->value);
temporary = temporary->next;
}
printf("\n");
}
// And the related code in main is:
int main() {
/* Code to insert values */
for (int i = 1; i <= 10; i++) {
create(i);
}
display(head);
reverse(NULL, head);
display(head);
create(0);
display(head);
return 0;
}
输出
> ./a.out
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1 0
>
您应该添加一个例程来释放链表中的节点。
我写了一个递归函数来反转链表如下:
struct node{
int val;
struct node *next;
};
//Global pointer to structure
struct node *start=NULL,*head=NULL;
//*Function to input node*
void create(int data){
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(start == NULL){
temp->val=data;
temp->next=NULL;
start=temp;
head=temp;
}
else{
temp->val=data;
temp->next=NULL;
head->next=temp;
head=temp;
}
}
*Function to reverse the linked list*
void* rev(struct node *prev,struct node *cur){
if(cur!=NULL){
printf("Works");
rev(cur,cur->next);
cur->next=prev;
}
else{
start=prev;
}
}
而main中的相关代码是:
main(){
struct node *temp;
temp=start;
/*Code to insert values*/
rev(NULL,temp);
}
现在代码接受输入并完美地打印出来,但是在我调用 rev()
函数之后,相同的遍历函数什么也没有打印出来。
我在调试器上逐行 运行 代码 n 它给了我以下输出:
rev (prev=0x0, cur=0x0)
此外,由于 cur
不知何故为 NULL,因此 rev()
的 if
部分永远不会执行,只有 else
执行一次。
当我在我的 create()
函数中输入时,我会更新链表的第一个元素,甚至在 main 中打印语句证明它是这样的。
但是为什么函数 rev()
总是接收 NULL 的输入参数?
如果需要任何额外信息,请发表评论。
您的代码存在特定问题:您的 main()
函数缺少足够的代码来测试反转功能(例如,它不创建任何节点!);您的 create()
例程确实需要 head
和 tail
指针才能正常工作,而不是当前的 head
和 start
;您的反转函数维护 head/start 指针但不处理尾指针;您的 if
和 else
子句中有冗余代码,可以从条件语句中删除;你声明 rev()
为 void *
而不是简单的 void
.
我修改了下面的代码,解决了上述更改以及一些样式问题:
#include <stdlib.h>
#include <stdio.h>
struct node {
int value;
struct node *next;
};
// Global pointers to structure
struct node *head = NULL, *tail = NULL;
// Function to add node
void create(int data) {
struct node *temporary = malloc(sizeof(struct node));
temporary->value = data;
temporary->next = NULL;
if (head == NULL) {
head = temporary;
} else {
tail->next = temporary;
}
tail = temporary;
}
// Function to reverse the linked list
void reverse(struct node *previous, struct node *current) {
if (current != NULL) {
reverse(current, current->next);
current->next = previous;
} else {
head = previous;
}
if (previous != NULL) {
tail = previous;
}
}
void display(struct node *temporary) {
while (temporary != NULL) {
printf("%d ", temporary->value);
temporary = temporary->next;
}
printf("\n");
}
// And the related code in main is:
int main() {
/* Code to insert values */
for (int i = 1; i <= 10; i++) {
create(i);
}
display(head);
reverse(NULL, head);
display(head);
create(0);
display(head);
return 0;
}
输出
> ./a.out
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1 0
>
您应该添加一个例程来释放链表中的节点。