【问题标题】:Linked List C: add node to beginning, unexpected '0' value链表 C:将节点添加到开头,意外的“0”值
【发布时间】:2017-03-26 21:58:00
【问题描述】:

我正在尝试将一个新节点添加到链表的末尾,我可以添加一些内容,但是当我将其打印出来时,该节点的值为“0”。我认为这可能会发生,因为我可能忽略了在某处初始化变量,或者忘记分配内存所述变量。但我无法让它工作。

这是我的源代码:

我的链表/结构:

#include<stdio.h>

typedef char DATA;

struct Node {
    DATA d;
    struct Node *next;
};

我的 printList 函数:

void printList(struct Node **head) {
    struct Node *newNode = malloc(sizeof(struct Node));
    struct Node *temp;

    temp = *head;
    printf("Linked list:");

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

我的 insertNodeAtEnd 到结束函数:

// inset data at end

void insertNodeAtEnd(struct Node *head) {
    struct Node *newNode = malloc(sizeof(struct Node));
    struct Node *currentNode, *temp;

    temp = newNode;
    currentNode = newNode;

    printf("Enter a Node to insert at the end of the list \n");
    scanf("%s", &newNode->d);

    newNode->next = NULL;
    if (head == NULL) {
        head = newNode;
        currentNode = newNode;
    } else {
        temp = head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }

}

还有我的 main():

int main() {
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    struct Node *head = newNode;
    struct Node *temp = newNode;

    head->d = 1;
    int i = 0;
    printf("Enter 3 numbers");

    for (i = 0; i < 3; i++) {
        struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
        scanf("%d", &temp->d);
        temp->next = newNode;
        temp = temp->next;
    }

    insertNodeAtEnd(head);
    printList(&head);

    return 0;

对不起,任何混乱的代码,我在这方面还是相当新的

【问题讨论】:

  • for (temp= *head; temp != NULL; temp = temp-&gt;next){ printf( "%d\n", temp-&gt;d); } :: for() 循环是你的朋友。
  • void insertNodeAtEnd(struct Node *head)this是使用指针指向的地方;不在 print() 函数中。
  • %d%sscanf 不匹配 char(又名 DATA)。如果要输入数字,应将 typedef char DATA; 更改为 typedef int DATA;。然后进行匹配的输入输出。
  • 修复this

标签: c linked-list


【解决方案1】:

嗯,你有几个错误。首先,你需要添加

temp->next = NULL;

insertNodeAtEnd(head); 行之前。您的代码可能在没有这一行的情况下工作的原因可能是因为您的编译器默认将指针初始化为 NULL。例如,在 GCC 中,如果没有该行,您的程序就会崩溃。第二个问题是您将 DATA 类型定义为 char,但将其读取为 int。如果处理器使用 big-engian 地址,它可能会导致您的应用程序崩溃。你应该把它改成

typedef int DATA;

还有变化

scanf("%s", &newNode->d);

scanf("%d", &newNode->d);

之后,改变

while(temp->next!=NULL)

while(temp!=NULL)

因为否则你会丢失最后一个元素。然后,您需要稍微重新排序一个循环。这是包含所有修复的完整工作代码:

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

typedef int DATA;

struct Node
{
DATA d;
struct Node *next;
};

void printList(struct Node **head)
{
struct Node *newNode;
struct Node *temp;
temp = *head;
printf("Linked list:");
while(temp!=NULL)
{
    printf( " \n %d ", temp->d);
    temp = temp->next;
}
printf("\n");
}

// inset data at end

void insertNodeAtEnd(struct Node **headPointer)
{
    struct Node *head = *headPointer;
    struct Node *newNode = malloc(sizeof(struct Node));
    struct Node *currentNode, *temp;
    temp = newNode;
    currentNode = newNode;
    printf("Enter a Node to insert at the end of the list \n");
    scanf("%d", &newNode->d);
    newNode->next = NULL;
    if(head == NULL)
    {
        head = newNode;
        currentNode = newNode;
    }

else
{
    temp = head;
        while(temp->next!= NULL)
        {
            temp = temp->next;

        }
    temp->next = newNode;
}
  *headPointer = head;
}

int main()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *head = newNode;
struct Node *temp = newNode;
head->d = 1;
int i = 0;
printf("Enter 3 numbers: ");

for(i = 0; i < 3; i++)
{
  if(i){
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    temp->next = newNode;
    temp = temp->next;
    scanf("%d", &temp->d);
  }else{
    scanf("%d", &temp->d);
  }

}
temp->next = NULL;
insertNodeAtEnd(&head);
printList(&head);
return 0;
}

更新

我又添加了两个修复程序。正如@BLUEPIXY 指出的那样,还有一些OP 的错误。我已经发现了它们,但我没有修复它,因为它们对于导致 OP 问题的原因并不重要。但是,无论如何,错误如下:

首先,如果列表为空,函数insertNodeAtEnd 将不会更新指向列表的指针,因为您传递的是指向列表头部的指针,而不是指针的指针 指向头部。可以通过在函数参数类型中添加** 来修复它。

其次,打印列表时不需要分配内存。您显然只是将代码复制到每个函数,甚至是不需要插入节点的函数(如printList)。

以上脚本是包含这两个修复的更新脚本。

【讨论】:

  • 1) insertNodeAtEnd head = newNode; 。这无法更新列表。
  • 2) printList struct Node *newNode = malloc(sizeof(struct Node)); 发生内存泄漏。
  • @JoeKur。如果我的回答对您有帮助,请考虑接受。
  • @BLUEPIXY。我已经发现了这一点,但我没有做太多细节,因为这对 OP 的问题来说并不重要。但是,我还是修好了。
猜你喜欢
  • 2021-06-28
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多