在一个简单的程序中,在链表的开头输入元素并在c程序中显示它时出错



这是我的程序代码,用于在链表的开头输入元素并打印链表,它运行得很好(我得到了正确的输出(-

#include <stdio.h>
#include <stdlib.h>
// this is insertion of a node in begining of a linked list
typedef struct Node
{
int data;
struct Node *next;
} n;

void Insert(struct Node** head,int x)
{
struct Node *temp = (n *)malloc(sizeof(struct Node));
temp->data = x;
temp->next = *head;
*head = temp;
}
void Print(struct Node* head)
{
struct Node *temp = head;
printf("The linked list is:");
while (temp != NULL)
{
printf("%d,", temp->data);
temp = temp->next;
}
printf("n");
}
int main()
{
struct Node *head;
head = NULL;
int n, i, x;
printf("Enter  how many numbers you want?n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the number in the linkedlistn");
scanf("%d", &x);
Insert(&head,x);
Print(head);
}
return 0;
}

对于上下文,输出为

Enter  how many numbers you want?
6
Enter the number in the linkedlist
1
The linked list is:1,
Enter the number in the linkedlist
2
The linked list is:2,1,
Enter the number in the linkedlist
3
The linked list is:3,2,1,
Enter the number in the linkedlist
4
The linked list is:4,3,2,1,
Enter the number in the linkedlist
5
The linked list is:5,4,3,2,1,
Enter the number in the linkedlist
6
The linked list is:6,5,4,3,2,1,

现在只是想看看我的指针和指向结构的指针的概念是否清晰我修改了代码中的Print((函数,而程序的其余部分保持相同的

#include <stdio.h>
#include <stdlib.h>
// this is insertion of a node in begining of a linked list
typedef struct Node
{
int data;
struct Node *next;
} n;

void Insert(struct Node** head,int x)
{
struct Node *temp = (n *)malloc(sizeof(struct Node));
temp->data = x;
temp->next = *head;
*head = temp;
}
void Print(struct Node* head)
{
struct Node *temp = head;
printf("The linked list is:");
//    while (temp != NULL)
//    {
//        printf("%d,", temp->data);
//        temp = temp->next;
//    }
//    printf("n");
do{
printf("%d,", temp->data);
temp = temp->next;
}while((temp->next)!= NULL);
printf("n");
}
int main()
{
struct Node *head;
head = NULL;
int n, i, x;
printf("Enter  how many numbers you want?n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the number in the linkedlistn");
scanf("%d", &x);
Insert(&head,x);
Print(head);
}
Print(head);

return 0;
}

现在,当我进行试运行时,我得到了正确的结果,我得到的输出是错误的,对于上下文来说,输出是

Enter  how many numbers you want?
5
Enter the number in the linkedlist
1
The linked list is:1,
--------------------------------
Process exited after 5.156 seconds with return value 3221225477
Press any key to continue . . .

虽然我只在Print((函数中进行了更改,但为什么编译器不接受linkedlist中的其余元素作为输入?假设Print((函数中有错误

这样的功能实现

void Print(struct Node* head)
{
struct Node *temp = head;
printf("The linked list is:");
//    while (temp != NULL)
//    {
//        printf("%d,", temp->data);
//        temp = temp->next;
//    }
//    printf("n");
do{
printf("%d,", temp->data);
temp = temp->next;
}while((temp->next)!= NULL);
printf("n");
}

不正确。

首先,它不检查所传递的指针是否等于NULL

其次是边做边循环中的条件

}while((temp->next)!= NULL);

如果在循环中的该语句之后,可以调用未定义的行为

temp = temp->next;

指针CCD_ 2再次等于CCD_。因为在这种情况下,空指针用于访问内存。或者在其他情况下,当列表包含多个节点时,最后一个节点将不会输出。

最新更新