首页 > 解决方案 > 似乎永远无法在 c 中的链表中获得打印功能

问题描述

无法打印链接列表,它陷入了无限循环,无法理解我哪里出错了。

#include<stdio.h>
#include<stdlib.h>

typedef struct node{

    int data;
    struct node *next;

}node;

node *head = NULL;

void print(node *head){

    node *temp = head;

    while(temp!=NULL);
    {
        printf("%d => ",temp->data);
        temp = temp->next;
    }

    printf("NULL");

}

node *clist(int n){

    node *temp = NULL;
    node *p = NULL;
    int i;

    for(i=0;i<n;i++)
    {
        temp = (node*)malloc(sizeof(node));

        printf("Enter the elements of the list.\n");
        scanf("%d",&temp->data);
    }

    if(head!=NULL)
    {
        while(p->next!=NULL)
        p=p->next; //shifting p here node by node

        p->next = temp; //last node which was just created
    }

    else
    {
        head = temp;
    }

    return head;
}

node *binsert(int x){

    node *temp = NULL;
    node *p = NULL;

    temp = (node*)malloc(sizeof(node));

    if(head!=NULL)
    {
        temp->next = head;
        temp = head;
    }

    else
    {
        p = head = temp;
    }

    return head;
}

int main ()
{
    int a, s, i, n,f;

    printf("Choose an option : \n1.Create a list.\n2.Exit.\n");
    scanf("%d",&s);

    switch(s)
    {
        case 1: 
            printf("Very Well! Input the number of nodes\n");
            scanf("%d",&n);
            head = clist(n);
            printf("Link List created successfully.\n");
            print(head);
            break;

        default:
            exit (0);           
    }

    printf("Choose the operation you want to perform on the linked list:\n1.Add an element to the beginning.\n2.Add an element to the end.\n3.Add an element at a a particular position.\n");
    scanf("%d",&a);
    switch(a)
    {
        case 1:
            printf("Enter the element you want to insert at the beginning.\n");
            scanf("%d",&f);

            binsert(f);
            printf("Inserted Successfully.\n");
            print(head);

            break;

        case 2:
            printf("Error E162B");
    }

    return 0;

}

我尝试将头部更改为全局变量。代码重写了 7 次。请帮忙。

标签: cdata-structureslinked-list

解决方案


第一的:

for(i=0;i<n;i++)
{
    temp = (node*)malloc(sizeof(node));

    printf("Enter the elements of the list.\n");
    scanf("%d",&temp->data);
}

此循环重复多次,以便用户可以输入列表的元素。但它永远不会将任何节点添加到链表中。循环的每次迭代都会更改temp为指向新分配的节点,但不会将节点添加到链表中。

第二:

while(temp!=NULL);

这是一个无限循环。如果tempis not NULL,它在循环中的任何地方都没有改变,所以它永远不会变成NULL. 你不想要那个分号。

第三:

node *head = NULL;

void print(node *head){

   node *temp = head;

不要对自己这样做。您有一个名为的全局变量head和一个名为 的参数head。当你这样做时,它指的node *temp = head;是多么明显。如果它们的范围重叠,不要给两个变量同名。 head

第四:

if(head!=NULL)
{
    temp->next = head;
    temp = head; // **** here
}

else
{
    p = head = temp;
}

return head;

有什么意义temp = head;?的值temp不能在任何地方访问。所以这不可能。

最后:

我认为你没有问正确的问题。你要求我们解释你哪里出错了,我已经做到了。但我认为这不是你真正需要的。也许您需要帮助开发算法来解决问题?也许问一个新问题来描述你认为算法应该是什么(只用文字,不需要使用代码)并寻求帮助修复算法。

而且,坦率地说,错误的数量表明您正在尝试一项远远超出您知识范围的任务。这是令人沮丧的,不是学习编程的好方法。您应该认真考虑首先尝试更简单的任务。

您可能已将其剥离以保持问题简单,但您的真实代码应该包含许多额外的检查和日志记录以帮助您理解它。输入函数时记录。记录函数做出的每个决定。记录什么函数返回。这将帮助您确定程序的实际操作与您认为它应该做的有偏差。或者,如果您愿意,可以学习使用平台的调试工具。这样,您将不会进行随机更改并希望它们使一切正常,而是会知道代码首先出错的地方,并且能够一次修复一件事,确信您没有破坏正在工作的东西。


推荐阅读