C程序遍历单链链
C program to traverse a singly linked lisk
我写了一个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;
}
`
在这个for循环中
for(int i = 2; i <= n; ++i){
printf("Node Data %d: ",i);
scanf("%d",&newNode -> data);
newNode -> next = NULL;
temp -> next = newNode;
}
相同的指针 newNode 被分配给数据成员 temp->next,它等效于表达式 head->next,因为在循环中指针 temp 没有改变。
您需要在循环中分配新节点并重新分配指针temp。
您还需要检查变量 n
的输入值。例如,如果 n
设置为 1 则将发生内存泄漏。
只要输入的变量n的值不等于INT_MAX
.
,就不会出现死循环
程序可以这样看
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
struct node
{
int data;
struct node *next;
} *head = NULL;
unsigned int n = 0;
printf( "Enter number of Nodes: " );
scanf( "%u", &n );
if (n != 0)
{
head = malloc( sizeof( struct node ) );
if (!head)
{
puts( "No Memory Allocation" );
exit( 0 );
}
else
{
printf( "Node Data 1: " );
scanf( "%d", &head->data );
head->next = NULL;
unsigned int i = 0;
for (struct node *temp = head; ++i < n; temp = temp->next)
{
if (( temp->next = malloc( sizeof( struct node ) ) ) == NULL)
{
puts( "No Memory Allocation" );
break;
}
temp->next->next = NULL;
printf( "Node Data %u: ", i + 1 );
scanf( "%d", &temp->next->data );
}
for (const struct node *current = head; current != NULL; current = current->next)
{
printf( "%d -> ", current->data );
}
puts( "null" );
}
}
}
程序输出为
Enter number of Nodes: 5
Node Data 1: 1
Node Data 2: 2
Node Data 3: 3
Node Data 4: 4
Node Data 5: 5
1 -> 2 -> 3 -> 4 -> 5 -> null
请注意,您需要在程序中附加释放所有已分配内存的代码。
我写了一个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;
}
`
在这个for循环中
for(int i = 2; i <= n; ++i){
printf("Node Data %d: ",i);
scanf("%d",&newNode -> data);
newNode -> next = NULL;
temp -> next = newNode;
}
相同的指针 newNode 被分配给数据成员 temp->next,它等效于表达式 head->next,因为在循环中指针 temp 没有改变。
您需要在循环中分配新节点并重新分配指针temp。
您还需要检查变量 n
的输入值。例如,如果 n
设置为 1 则将发生内存泄漏。
只要输入的变量n的值不等于INT_MAX
.
程序可以这样看
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
struct node
{
int data;
struct node *next;
} *head = NULL;
unsigned int n = 0;
printf( "Enter number of Nodes: " );
scanf( "%u", &n );
if (n != 0)
{
head = malloc( sizeof( struct node ) );
if (!head)
{
puts( "No Memory Allocation" );
exit( 0 );
}
else
{
printf( "Node Data 1: " );
scanf( "%d", &head->data );
head->next = NULL;
unsigned int i = 0;
for (struct node *temp = head; ++i < n; temp = temp->next)
{
if (( temp->next = malloc( sizeof( struct node ) ) ) == NULL)
{
puts( "No Memory Allocation" );
break;
}
temp->next->next = NULL;
printf( "Node Data %u: ", i + 1 );
scanf( "%d", &temp->next->data );
}
for (const struct node *current = head; current != NULL; current = current->next)
{
printf( "%d -> ", current->data );
}
puts( "null" );
}
}
}
程序输出为
Enter number of Nodes: 5
Node Data 1: 1
Node Data 2: 2
Node Data 3: 3
Node Data 4: 4
Node Data 5: 5
1 -> 2 -> 3 -> 4 -> 5 -> null
请注意,您需要在程序中附加释放所有已分配内存的代码。