C程序遍历单链表
我编写了一个 C 程序来实现遍历单链表的概念。该程序首先通过请求用户输入来创建列表,然后显示/遍历创建的列表。当我运行此代码时,它成功创建链接列表,但在显示创建的列表时,它会产生无限循环。 `
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
} *head;
int main(){
struct node *newNode, *temp;
int n;
printf("Enter number of Nodes: ");
scanf("%d",&n);
if(n <= 1){
printf("Invalid Input Nodes should Nodes should be greater than 1");
exit(0);
}
else{
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL){
printf("No Memory Allocation");
exit(0);
}
else{
printf("Node Data 1: ");
scanf("%d",&head -> data);
head -> next = NULL;
temp = head;
}
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL){
printf("No Memory Allocation");
exit(0);
}
else{
for(int i = 2; i <= n; ++i){
printf("Node Data %d: ",i);
scanf("%d",&newNode -> data);
newNode -> next = NULL;
temp -> next = newNode;
temp = temp -> next;
}
}
//Traversal
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Empty list..");
}
else
{
while (ptr != NULL){
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
return 0;
}
`
I have written a C program to implement the concept of traversing a singly linked list. The program first creats the list by asking for the user input and then displays/ traverses through the created list. When i run this code, it sucessfully creates the linked list, but on displaying the created list, it produces an infinite loop.
`
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
} *head;
int main(){
struct node *newNode, *temp;
int n;
printf("Enter number of Nodes: ");
scanf("%d",&n);
if(n <= 1){
printf("Invalid Input Nodes should Nodes should be greater than 1");
exit(0);
}
else{
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL){
printf("No Memory Allocation");
exit(0);
}
else{
printf("Node Data 1: ");
scanf("%d",&head -> data);
head -> next = NULL;
temp = head;
}
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL){
printf("No Memory Allocation");
exit(0);
}
else{
for(int i = 2; i <= n; ++i){
printf("Node Data %d: ",i);
scanf("%d",&newNode -> data);
newNode -> next = NULL;
temp -> next = newNode;
temp = temp -> next;
}
}
//Traversal
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Empty list..");
}
else
{
while (ptr != NULL){
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}
return 0;
}
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此 for 循环中,
同一指针 newNode 被分配给数据成员 temp->next,这相当于表达式 head->next,因为在循环内指针 temp 未更改。
您需要在循环中分配新节点并重新分配指针 temp。
您还需要检查输入的变量
n
值。例如,如果n
设置为1,那么就会出现内存泄漏。如果输入的变量 n 值不等于 INT_MAX,则不会出现无限循环。
该程序可以如下所示
程序输出是
请注意,您需要在程序中附加将释放所有已分配内存的代码。
In this for loop
the same pointer newNode is assigned to the data member temp->next that is equivalent to the expression head->next because within the loop the pointer temp is not changed.
You need to allocate new nodes in the loop and reassign the pointer temp.
Also you need to check the entered value for the variable
n
. For example ifn
is set to 1 then there will be a memory leak.And there is neither infinite loop provided that the entered value for the variable n is not equal to
INT_MAX
.The program can look the following way
The program output is
Pay attention to that you need to append the program with the code that will free all the allocated memory.