C程序遍历单链表

发布于 2025-01-13 19:37:06 字数 1649 浏览 1 评论 0原文

我编写了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

束缚m 2025-01-20 19:37:06

在此 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

请注意,您需要在程序中附加将释放所有已分配内存的代码。

In this for loop

    for(int i = 2; i <= n; ++i){
        printf("Node Data %d: ",i);
        scanf("%d",&newNode -> data);
        newNode -> next = NULL;
        temp -> next = newNode;
        
        }

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 if n 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

#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" );
        }
    }
}

The program output is

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

Pay attention to that you need to append the program with the code that will free all the allocated memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文