C 我正在尝试通过递归反向打印链表,但它不起作用
我试图通过递归以相反的顺序打印链表。 这是代码:C 我正在尝试通过递归反向打印链表,但它不起作用,c,recursion,printing,linked-list,C,Recursion,Printing,Linked List,我试图通过递归以相反的顺序打印链表。 这是代码: #include <stdio.h> #include <stdlib.h> typedef struct list { int value; struct list* next; } list_s; static int g_size=sizeof(list_s); void printRe(list_s *node) { if (node = NULL) return;
#include <stdio.h>
#include <stdlib.h>
typedef struct list {
int value;
struct list* next;
} list_s;
static int g_size=sizeof(list_s);
void printRe(list_s *node) {
if (node = NULL)
return;
printRe(node->next); // this is where error happens
printf("%d", node->value);
}
int main() {
list_s *head;
head = (list_s*)malloc(g_size);
int n;
scanf("%d",&n);
int i = 0;
int x;
scanf("%d", &x);
head->value = x;
head->next = NULL;
list_s *current;
current = head;
for (i; i < n - 1; ++i) {
current->next = (list_s*)malloc(g_size);
current = current->next;
scanf("%d", &x);
current->value = x;
current->next = NULL;
};
printRe(head);
return 0;
}
如您所见,当我尝试打印节点->下一步时出错。为什么会发生错误?我是否以错误的方式将列表传递给函数
谢谢大家! 如果printRe函数中的条件如下 ifnode==NULL而不是ifnode=NULL 我希望它能帮助你。 谢谢node=NULL应该是node==NULL,在if-test.gcc中-Wall大叫警告:建议在用作真值[-wparenthess]ifnode=NULL的赋值周围加括号