打印链表时出现意外输出
Unexpected output while printing a linked list
我在 lubuntu 16.04 的代码块 13.12 上得到了这段代码
程序是运行但是问题是第一次插入是重复的也就是
假设我首先将整数“4”插入链表。但我得到的输出为:
4 ,4 ,
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* head = NULL;
void Insert(int c)
{
if(head == NULL) {
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
head = temp;
}
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
struct node* temp1 = head;
while (temp1->next != NULL) {
temp1 = temp1->next;
}
temp1 -> next = temp;
}
void print() {
struct node* temp = head;
printf("list is: \n");
while (temp != NULL) {
printf( "%d ,",temp->data);
temp = temp->next;
}
}
int main () {
printf("How Many Numbers?\n");
int a ,b ,c;
scanf("%d" , &b);
for(a = 0;a<b;a++) {
printf("Enter the numbers \n");
scanf("%d",&c);
Insert(c);
print();
}
return 0;
}
那是因为Insert
函数"falls through"在初始化第一项后,再次插入。在代码块末尾添加return
。
void Insert(int c)
{
if(head == NULL) {
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
head = temp;
return; //<-- add this line
}
...
the problem is that the first insertion is duplicated
原因:
- 你已经正确地提到了
if(head==NULL)
来检查插入的节点是否是第一个节点但是之后你没有提到else 限制编译器。
- 所以编译器编译了 if 块和它后面的代码
- 因此在
head
之后创建了另一个具有相同值 c
的节点
- 这就是当您插入
c=4
时,您得到 4,4,
作为输出的原因
解决方案
尝试将您的 insert()
与 else 条件一起使用
void Insert(int c)
{
if(head == NULL)
{
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
head = temp;
}
else
{
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
struct node* temp1 = head;
while (temp1->next!= NULL)
temp1 = temp1->next;
temp1 -> next = temp;
}
}
建议 : 你在insert()
函数中提到过两次
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
只需分配一次temp
,然后使用if-else
条件将其插入到适当的位置。这也减少了代码行数。这样做:
void Insert(int c)
{
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
if(head == NULL)
{
head = temp;
}
else
{
struct node* temp1 = head;
while (temp1->next!= NULL)
temp1 = temp1->next;
temp1 -> next = temp;
}
}
我在 lubuntu 16.04 的代码块 13.12 上得到了这段代码
程序是运行但是问题是第一次插入是重复的也就是
假设我首先将整数“4”插入链表。但我得到的输出为:
4 ,4 ,
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* head = NULL;
void Insert(int c)
{
if(head == NULL) {
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
head = temp;
}
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
struct node* temp1 = head;
while (temp1->next != NULL) {
temp1 = temp1->next;
}
temp1 -> next = temp;
}
void print() {
struct node* temp = head;
printf("list is: \n");
while (temp != NULL) {
printf( "%d ,",temp->data);
temp = temp->next;
}
}
int main () {
printf("How Many Numbers?\n");
int a ,b ,c;
scanf("%d" , &b);
for(a = 0;a<b;a++) {
printf("Enter the numbers \n");
scanf("%d",&c);
Insert(c);
print();
}
return 0;
}
那是因为Insert
函数"falls through"在初始化第一项后,再次插入。在代码块末尾添加return
。
void Insert(int c)
{
if(head == NULL) {
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
head = temp;
return; //<-- add this line
}
...
the problem is that the first insertion is duplicated
原因:
- 你已经正确地提到了
if(head==NULL)
来检查插入的节点是否是第一个节点但是之后你没有提到else 限制编译器。 - 所以编译器编译了 if 块和它后面的代码
- 因此在
head
之后创建了另一个具有相同值c
的节点
- 这就是当您插入
c=4
时,您得到4,4,
作为输出的原因
解决方案
尝试将您的 insert()
与 else 条件一起使用
void Insert(int c)
{
if(head == NULL)
{
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
head = temp;
}
else
{
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
struct node* temp1 = head;
while (temp1->next!= NULL)
temp1 = temp1->next;
temp1 -> next = temp;
}
}
建议 : 你在insert()
函数中提到过两次
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
只需分配一次temp
,然后使用if-else
条件将其插入到适当的位置。这也减少了代码行数。这样做:
void Insert(int c)
{
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
if(head == NULL)
{
head = temp;
}
else
{
struct node* temp1 = head;
while (temp1->next!= NULL)
temp1 = temp1->next;
temp1 -> next = temp;
}
}