c++ - 添加到简单链接列表的前面

标签 c++ c algorithm data-structures linked-list

我正在尝试实现一个链接列表,并从 addToFront 函数开始。 在这里,我只是将数字 5 添加到列表的前面。我知道如果列表为空,则列表指针应该为 Null,但是,情况似乎并非如此。

已编辑的文件: 我已经编辑了这些文件(感谢taskinoor的回答),现在提供了

的输出
0 5

而不是

5

我有头文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

typedef struct List {
    struct list * next;
    int value;
    int size;
}list;

void addToFront(int num, list **l);

void printList(list * l);

int getSize(list * l);

void initialize(list * l);

void freeList(list *l);

一个c文件“main.c”

#include "Header.h"

int main() {

    list l;

    initialize(&l);

    addToFront(5, &l);
    printList(&l);


    _getch();
    freeList(&l);
    return 0;
}

void printList(list * l) {
    list *current = l;
    while (current != NULL) {
        printf("%d ", current->value);
        current = current->next;
    }
}

void freeList(list *l) {
    list *current = l;
    while (current != NULL) {
        list *tmp = current;
        current = current->next;
        free(tmp);
    }
}

还有一个接口(interface)c文件(不完整)

#include "Header.h"

int getSize(list * l) {
    return l->size;
}

void initialize(list * l) {
    l->next = NULL;
    l->value = 0;
    l->size = 0;
}

// need to pass **l to update it
void addToFront(int num, list **l) {
    // allocate memory for new node
    list *tmp = (list *)malloc(sizeof(list));

    tmp->value = num;

    // new node should point to whatever head is currently pointing
    // even if head is NULL at beginning
    tmp->next = *l;

    // finally l needs to point to new node
    // thus new node becomes the first node
    *l = tmp;
}

但是,当调用 addToFront 函数时,if 语句永远不会执行。这没有意义,如果列表为空,列表指针不应该为空吗?

接下来,我尝试在初始化函数中手动设置l == NULL,但这也没有执行任何操作。另外,我的打印函数无限循环,我认为这是 malloc 的问题。任何帮助将不胜感激。

最佳答案

条件if (l == NULL)addToFront中不成立,因为l在这里不为NULL。您在 main 的开头调用了 l = malloc(sizeof(list));,这使得 l 不为 NULL。不需要以这种方式进行malloc和初始化l。我想 l 你的意思是指向列表的头指针。这在开始时应该为 NULL(即不要在 main 处调用 malloc 并将返回的地址分配给 l),并且节点的内存应该是在 addToFront 中分配如下:

// need to pass **l to update it
void addToFront(int num, list **l) {
    // allocate memory for new node
    list *tmp = (list *) malloc(sizeof(list));

    tmp->value = num;

    // new node should point to whatever head is currently pointing
    // even if head is NULL at beginning
    tmp->next = *l;

    // finally l needs to point to new node
    // thus new node becomes the first node
    *l = tmp;
}

main 中删除 malloc

int main() {
    list *l;
    addToFront(5, &l);  // pass address of l
    printList(l);

    // other stuffs
}

打印将是这样的:

void printList(list * l) {
    list *current = l;
    while (current != NULL) {
        printf("%d ", current->value);
        current = current->next;
    }
}

最后,仅释放 l 是不够的。您需要遍历整个列表并释放其中的每个节点。

void freeList(list *l) {
    list *current = l;
    while (current != NULL) {
        list *tmp = current;
        current = current->next;
        free(tmp);
    }
}

关于c++ - 添加到简单链接列表的前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34373358/

相关文章:

c++ - 如何使用 Visual Studio 2008 确定 C++ 代码的复杂性(如 CPU 周期)

代码不显示返回值

c++ - 依赖图的设计模式

arrays - 查找数组的最大元素是另一个元素的约数

algorithm - 他们如何在字符串中搜索

c++ - iostream::write() 中需要 sizeof() 吗?

c++ - 使用 COLLADA DOM 将 COLLADA 文档输出为字符串

c++ - 计算 unordered_map 中重新散列的次数

c - 打印浮点变量

c - 在我的 TCP 连接中,客户端发送 Hello,但服务器收到 Hellob