链表问题,请大家帮忙解答,我想知道我的为什么错误?
换了家新单位,不能上网,刚去没有什么事情,就想写个链表吧。假如将头指针作为参数应该很简单。(呵呵,有机会再试试)。假如像我现在这样使用全局指针,得不到正确的结果啊。
p->id=4,
p->next is not null
p->id=4,
p->next is null
this is a test for test4
以上是结果,明显是不对。为什么呢原因在哪里,请指教谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
int id;
// char name[20];
struct node *next;
};
struct node *init_node(struct node ptr);
struct node *head;
void add_node(struct node ptr);
void show_allnode(void);
int main(void)
{
struct node ptr;
ptr.id = 1;
ptr.next = NULL;
head = init_node(ptr);
ptr.id = 2;
ptr.next = NULL;
add_node(ptr);
ptr.id = 3;
ptr.next = NULL;
add_node(ptr);
ptr.id = 4;
ptr.next = NULL;
add_node(ptr);
show_allnode();
printf(" this is a test for test4\n");
return 0;
}
void show_allnode(void)
{
struct node *p;
p = head;
if (p == NULL)
{
printf("head is null\n");
}
printf(" this is a test for show all node\n");
while(p != NULL)
{
printf("p->id=%d,\n", p->id);
if (p->next == NULL)
{
printf("p->next is null\n");
}
else
{
printf("p->next is not null\n");
}
p = p->next;
}
}
struct node *init_node(struct node ptr)
{
struct node *head1 = (struct node *)malloc(sizeof(struct node) * 1);
if (headi1 == NULL)
{
printf("malloc error\n");
}
head1 = &ptr;
head = head1;
return head;
}
void add_node(struct node ptr)
{
struct node *p;
p = head;
if (p == NULL)
{
printf("head is null\n");
}
while(p->next != NULL)
{
p = p->next;
}
struct node *head1 = (struct node *)malloc(sizeof(struct node) * 1);
if (head1 == NULL)
{
printf("malloc error\n");
}
head1->id = ptr.id;
p->next = head1;
head1->next = NULL;
}
p->id=4,
p->next is not null
p->id=4,
p->next is null
this is a test for test4
以上是结果,明显是不对。为什么呢原因在哪里,请指教谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node{
int id;
// char name[20];
struct node *next;
};
struct node *init_node(struct node ptr);
struct node *head;
void add_node(struct node ptr);
void show_allnode(void);
int main(void)
{
struct node ptr;
ptr.id = 1;
ptr.next = NULL;
head = init_node(ptr);
ptr.id = 2;
ptr.next = NULL;
add_node(ptr);
ptr.id = 3;
ptr.next = NULL;
add_node(ptr);
ptr.id = 4;
ptr.next = NULL;
add_node(ptr);
show_allnode();
printf(" this is a test for test4\n");
return 0;
}
void show_allnode(void)
{
struct node *p;
p = head;
if (p == NULL)
{
printf("head is null\n");
}
printf(" this is a test for show all node\n");
while(p != NULL)
{
printf("p->id=%d,\n", p->id);
if (p->next == NULL)
{
printf("p->next is null\n");
}
else
{
printf("p->next is not null\n");
}
p = p->next;
}
}
struct node *init_node(struct node ptr)
{
struct node *head1 = (struct node *)malloc(sizeof(struct node) * 1);
if (headi1 == NULL)
{
printf("malloc error\n");
}
head1 = &ptr;
head = head1;
return head;
}
void add_node(struct node ptr)
{
struct node *p;
p = head;
if (p == NULL)
{
printf("head is null\n");
}
while(p->next != NULL)
{
p = p->next;
}
struct node *head1 = (struct node *)malloc(sizeof(struct node) * 1);
if (head1 == NULL)
{
printf("malloc error\n");
}
head1->id = ptr.id;
p->next = head1;
head1->next = NULL;
}
...全文
相关推荐
你必须知道的495个C语言问题.pdf
通过阅读和理解《你必须知道的495个C语言问题》,开发者可以深入了解C语言的细节,提升编程技能,避免常见的陷阱和错误。
Josephus问题C++单循环链表实现
在实际编程中,我们还需要考虑边界条件和错误处理,例如当人数为1时,无需报数,直接返回1作为存活者。此外,对于不同的报数间隔M,算法的效率也会有所不同,因此优化算法以适应各种M值也是很重要的。
你必须知道的495个C语言问题(1).pdf
C语言问题FAQ本文档是C语言的问题FAQ,包含495个常见问题的解答。这些问题涵盖了C语言的基本概念、变量、数据类型、函数、数组、指针、结构体、联合体、枚举、文件输入输出、存储器管理等方面。
你必须知道的495个C语言问题
《你必须知道的495个C语言问题》是一本由Steve Summit编写的经典书籍,后由朱群英和孙云翻译成中文。
《你必须知道的495个C语言问题》
#### 1.6 我似乎不能成功定义一个链表在C语言中,结构体可以包含指向自身的指针,从而形成链表。示例中的错误可能是因为`NODEPTR`尚未定义就被使用。


