【问题标题】:Code of display node in linked list doesn't work链表中显示节点的代码不起作用
【发布时间】:2021-06-29 11:08:50
【问题描述】:

我在链表中​​编写了插入代码。但是当我运行这段代码时,我的显示功能无法正常工作并打印其他内容。

**ERROR IN CODE**

在这里,我将 head 函数声明为全局变量并将其设置为 NULL。之后,当我返回主函数并通过它时。在一些代码之后,将一个节点分配给结构 p 并将第一个节点声明为头(现在我们的头不是 NULL,它包含节点中的一些值),然后我们调用显示函数。 现在当我们返回显示函数并运行它时,它显示我们的头部为NULL,而不是两者都为NULL。

下面是我的代码

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

struct node
{
    int data;
    struct node* next;
};
struct node* head = NULL, * tail = NULL;

void printList()
{
    struct node* ptr = head;
    if (head == NULL)
    {
        printf("Empty list\n");
    }
    printf("Nodes of the list are: \n");
    while (ptr != NULL)
    {
        printf("%d \t", ptr->data);
        ptr = ptr->next;
    }
    printf("\n");
}

struct node* insertion(struct node** head, struct node** tail, int ele, int pos)
{
    int i;
    struct node* new_node, * temp;
    new_node = (struct node*)malloc(sizeof(struct node));
    if (new_node == NULL)
    {
        printf(" Dynamic memory allocation failed. Insertion can not be done");
        return NULL;
    }
    else
    {
        new_node->data = ele;
        if (pos < 1)
        {
            printf(" Inappropriate position");
            return NULL;
        }
        else if (*head == NULL)
        {
            new_node->next = NULL;
            *head = *tail = new_node;
            return (new_node);
        }
        else if (pos == 1)
        {
            new_node->next = *head;
            *head = new_node;
            return(new_node);
        }
        else
        {
            temp = *head;
            i = 1;
            while (i < pos - 1 && temp != NULL)
            {
                temp = temp->next;
                i++;
            }
            if ((temp == NULL) || ((i == pos - 1) && (temp->next == NULL)))
            {
                new_node->next = NULL;
                (*tail)->next = new_node;
                (*tail) = new_node;
                return (new_node);
            }
            else
            {
                new_node->next = temp->next;
                temp->next = new_node;
                return (new_node);
            }
        }
    }

}


int main()
{
    struct node* head, * tail, * p;
    int n, i, ele, pos;

    do {
        printf(" Enter the number of elements initially in linked list:   ");
        scanf("%d", &n);
        if (n < 0)
        {
            printf(" Enter the number greater than zero \n");
        }
    } while (n < 0);
    printf(" Enter the elements of linked list \n");
    for (i = 0; i < n; i++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        printf("%d element : ", i + 1);
        scanf("%d", &p->data);
        if (head == NULL)
        {
            head = p;
            tail = p;
        }
        else
        {
            tail->next = p;
            tail = p;
        }
    }

    printList();

    printf(" Enter the value which you want to insert:  ");
    scanf("%d", &ele);

    printf(" Enter the position at which you want to insert:  ");
    scanf("%d", &pos);

    insertion(&head, &tail, ele, pos);
    return 0;
}

【问题讨论】:

  • 分享 实际 输出,而不是“类似...”,这不是真正的输出。
  • 有很多问题。是时候开始学习如何使用调试器了。这是投资的好时机,很快就会得到回报

标签: c data-structures linked-list singly-linked-list


【解决方案1】:
int main()
{
    struct node* head, * tail, * p;

这是你的问题。您在名为 headtail 的 main 中声明局部变量,它们在该范围内隐藏全局变量。当您稍后在 main 中分配给 head 和 tail 时,您正在更改局部变量的值。全局变量保持不变。

【讨论】:

  • 我删除了它,但它还没有显示任何内容
  • 你得到了什么输出,你期望什么输出?当我用struct node *p; 替换行时,它会提示输入列表的长度,每个元素的值,然后打印列表。 (它不会在最终插入 之后打印列表,但那是因为您不再调用 printList。)顺便说一句,您永远不会释放内存。
  • **OUTPUT ** -> 代表此注释中的换行 -> 输入链表中初始元素的数量:4 -> 输入链表的元素 -> 1 个元素:2 - > 2 个元素:4 -> 3 个元素:5 -> 4 个元素:7 -> 空列表 -> 列表的节点是: -> 输入要插入的值:等等。我有疑问地问为什么当我的头不为空时会打印空列表
  • 那我不知道该说什么了。如果在 main 的第一行将struct node* head, * tail, * p; 替换为struct node *p;,它应该打印Nodes of this list are: -&gt; 2 4 5 7。你忘记重新编译了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多