链表程序运行不正常
linked list program is not functioning properly
给定的程序没有显示链表的所有元素。
我在识别错误时遇到问题。
起初我用一个空值初始化了head,然后创建了一个临时变量并为它分配了一个整数值和指向下一个节点的指针。
然后我创建了另一个名为 temp1 的节点并将其与头部链接。
它只会在 "i" 等于 1 时链接。
然后将 temp1 等同于下一个节点并执行相同的操作。
//链表
//插入节点。
#include <stdio.h>
struct node
{
int n;
struct node *next;
};
struct node *head;
int main ()
{
int i, s, x, y;
i = 0;
struct node *temp;
struct node *temp1;
struct node *cur;
head = NULL;
scanf ("%d", &s); //No. of nodes.
while (i < s)
{
scanf ("%d", &x);
if (head == NULL)
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
head = temp;
}
else
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
temp1 = temp;
if (i == 1)
{
head->next = temp1;
}
temp1 = temp1->next; //Assigning the next node.i.e. NULL value
}
i = i + 1;
}
cur = head;
while (cur != NULL)
{
printf ("%d", cur->n);
cur = cur->next;
}
return 0;
}
检查以下更改部分
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
head = temp;
temp1 = head;
}
else
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
temp1->next = temp;
temp1 = temp1->next; //Assigning the next node.i.e. NULL value
}
而不是依赖
if (i == 1) {
head->next = temp1;
}
我在创建头部时将 head
分配给 temp1
,这应该是第一次发生。
您的 else
部分也存在一些链接问题。
您丢失了前两个节点之外的节点,因为您从未将它们 link 添加到列表中。为变量使用有意义的名称:将 temp1
重命名为 tail
并在开始时将其初始化为 NULL
。那么循环体就变成了:
if (scanf(" %d", &x) != 1) {
// FIXME: handle error
}
temp = malloc(sizeof(*temp));
temp->n = x;
temp->next = NULL;
if (tail == NULL) {
head = temp;
} else {
tail->next = temp;
}
tail = temp;
++i;
(未测试。)
理由:您想将新节点添加到列表的末尾(尾部)。最简单的方法是在一个适当命名的变量中跟踪尾部,并且简单地 link 每个节点到 tail->next
而不是像检查节点计数等复杂的逻辑。唯一的特殊情况是空列表,即 head
和 tail
都是 NULL
,区别只是一行,所以不要复制整个代码块来设置新节点。
对于初学者,您必须包括 header <stdlib.h>
。
这个语句有问题
temp1 = temp;
如果 i
不等于 1
那么在这条语句之后
temp1 = temp1->next;
temp1
等于 NULL
.
因此所有其他节点都不会添加到列表中,因为存在循环
temp1 = temp;
//...
temp1 = temp1->next;
按以下方式更改循环
while (i < s)
{
scanf ("%d", &x);
if (head == NULL)
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
head = temp;
temp1 = head;
}
else
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
temp1->next = temp;
temp1 = temp;
}
i++;
}
注意你应该在使用它们的块范围内声明变量。否则程序将无法读取。
您使用的方法可以称为Java方法。
在 C 中,程序看起来更简单。例如
#include <stdio.h>
#include <stdlib.h>
struct node
{
int n;
struct node *next;
};
struct node *head;
int main( void )
{
struct node **temp = &head;
size_t n = 0;
scanf( "%zu", &n );
for ( size_t i = 0; i < n; i++ )
{
*temp = (struct node *) malloc ( sizeof ( struct node ) );
int value = 0;
scanf ( "%d", &value);
( *temp )->n = value;
( *temp )->next = NULL;
temp = &( *temp )->next;
}
for ( struct node *cur = head; cur != NULL; cur = cur->next )
{
printf ("%d -> ", cur->n );
}
puts( "NULL" );
return 0;
}
它的输出可能看起来像
1 -> 2 -> 3 -> NULL
给定的程序没有显示链表的所有元素。 我在识别错误时遇到问题。
起初我用一个空值初始化了head,然后创建了一个临时变量并为它分配了一个整数值和指向下一个节点的指针。 然后我创建了另一个名为 temp1 的节点并将其与头部链接。 它只会在 "i" 等于 1 时链接。
然后将 temp1 等同于下一个节点并执行相同的操作。
//链表 //插入节点。
#include <stdio.h>
struct node
{
int n;
struct node *next;
};
struct node *head;
int main ()
{
int i, s, x, y;
i = 0;
struct node *temp;
struct node *temp1;
struct node *cur;
head = NULL;
scanf ("%d", &s); //No. of nodes.
while (i < s)
{
scanf ("%d", &x);
if (head == NULL)
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
head = temp;
}
else
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
temp1 = temp;
if (i == 1)
{
head->next = temp1;
}
temp1 = temp1->next; //Assigning the next node.i.e. NULL value
}
i = i + 1;
}
cur = head;
while (cur != NULL)
{
printf ("%d", cur->n);
cur = cur->next;
}
return 0;
}
检查以下更改部分
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
head = temp;
temp1 = head;
}
else
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
temp1->next = temp;
temp1 = temp1->next; //Assigning the next node.i.e. NULL value
}
而不是依赖
if (i == 1) {
head->next = temp1;
}
我在创建头部时将 head
分配给 temp1
,这应该是第一次发生。
您的 else
部分也存在一些链接问题。
您丢失了前两个节点之外的节点,因为您从未将它们 link 添加到列表中。为变量使用有意义的名称:将 temp1
重命名为 tail
并在开始时将其初始化为 NULL
。那么循环体就变成了:
if (scanf(" %d", &x) != 1) {
// FIXME: handle error
}
temp = malloc(sizeof(*temp));
temp->n = x;
temp->next = NULL;
if (tail == NULL) {
head = temp;
} else {
tail->next = temp;
}
tail = temp;
++i;
(未测试。)
理由:您想将新节点添加到列表的末尾(尾部)。最简单的方法是在一个适当命名的变量中跟踪尾部,并且简单地 link 每个节点到 tail->next
而不是像检查节点计数等复杂的逻辑。唯一的特殊情况是空列表,即 head
和 tail
都是 NULL
,区别只是一行,所以不要复制整个代码块来设置新节点。
对于初学者,您必须包括 header <stdlib.h>
。
这个语句有问题
temp1 = temp;
如果 i
不等于 1
那么在这条语句之后
temp1 = temp1->next;
temp1
等于 NULL
.
因此所有其他节点都不会添加到列表中,因为存在循环
temp1 = temp;
//...
temp1 = temp1->next;
按以下方式更改循环
while (i < s)
{
scanf ("%d", &x);
if (head == NULL)
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
head = temp;
temp1 = head;
}
else
{
temp = (struct node *) malloc (sizeof (struct node));
temp->n = x;
temp->next = NULL;
temp1->next = temp;
temp1 = temp;
}
i++;
}
注意你应该在使用它们的块范围内声明变量。否则程序将无法读取。
您使用的方法可以称为Java方法。
在 C 中,程序看起来更简单。例如
#include <stdio.h>
#include <stdlib.h>
struct node
{
int n;
struct node *next;
};
struct node *head;
int main( void )
{
struct node **temp = &head;
size_t n = 0;
scanf( "%zu", &n );
for ( size_t i = 0; i < n; i++ )
{
*temp = (struct node *) malloc ( sizeof ( struct node ) );
int value = 0;
scanf ( "%d", &value);
( *temp )->n = value;
( *temp )->next = NULL;
temp = &( *temp )->next;
}
for ( struct node *cur = head; cur != NULL; cur = cur->next )
{
printf ("%d -> ", cur->n );
}
puts( "NULL" );
return 0;
}
它的输出可能看起来像
1 -> 2 -> 3 -> NULL