链接列表不打印

时间:2017-03-24 03:56:03

标签: c linked-list

我正在尝试使用C创建一个简单的链表,我想我能够自己构建链表,但是当我尝试打印它时,它会打印出最后一个节点的值而不是所有的值。列表。

#include <stdio.h>
#include <alloca.h>

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

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

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++)
    {
        scanf("%d", &temp->d);
        temp->next = newNode;
        temp = temp->next;
    }

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

    return 0;
}

非常感谢任何帮助/提示。

2 个答案:

答案 0 :(得分:1)

使用以下代码替换您的代码: -

#include<stdio.h>
#include <alloca.h>

typedef int DATA;

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

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

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;

    }

    printList(head);
    return 0;
}

在这里你需要在输入时声明循环中的newNode。因为,当前值被覆盖,旧的值被丢失,只打印了最后一个节点的值。

同时在打印时,请检查temp->next!=Null而不是temp!=NULL

答案 1 :(得分:1)

您的代码中存在多个问题:

  • 您没有正确创建列表。您只为一个节点分配内存,并且不正确地使用指针,导致链表指向同一个内存
  • 您根本没有调用printList函数
  • 目前尚不清楚为什么要在printList中再次分配内存,而应该打印已创建的列表
  • 此外,不需要为printList传递双指针,因为您没有修改列表。

虽然您应该自己理解并进行更改,但我提供了一个以下修改过的程序,希望能帮助您理解代码中的错误。

请参阅以下修改后的代码版本

#include<stdio.h>
#include <alloca.h>

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


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

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

struct Node *createNode()
{
    struct Node *newNode;

    newNode = malloc(sizeof(struct Node));
    if (NULL == newNode)
        return NULL;

    memset(newNode, 0, sizeof(struct Node));
    return newNode;
}

int main()
{
    struct Node *newNode = NULL;
    struct Node *headNode = NULL;
    struct Node *temp = NULL;
    int i = 0;
    int data = 0;

    printf("Enter 3 numbers\n");

    for( i = 0; i< 3; i++)
    {
        scanf("%d", &data);
        newNode = createNode();
        if (NULL == newNode)
            break;

        newNode->d = data;

        if (headNode == NULL)
        {
            headNode = newNode;
            temp = newNode;
        } 
        else
        {
            temp->next = newNode;
            temp = temp->next;
        }
    }
    printList(headNode);    
    return 0;
}
相关问题