小编Moh*_*zen的帖子

实现双向链表,其中数据存储在堆上?

我试图实现一个双向链表,其中节点存储在堆栈上,但堆上的节点数据.您可以使用push()将包含数据的新节点添加到列表中,并使用pop()删除最后一个元素.

我有push()方法的问题.

#include <iostream>

using namespace std;

struct Data {
    string name;
    int age;
};

struct Node {
    struct Node *next;
    struct Node *previous;   
    struct Data *d;         
};

struct Node *first;
struct Node *last;

void init() {
    first = last = NULL;
}


void push(Data d) { 
    Node *temp;

    if(first == NULL){
        first = new Node();
        first->d = malloc(sizeof(struct Data *));
        first->next = NULL;
        first->previous = NULL;
        last = first;
    } else if(last->next == NULL) {
        last->next = new Node(); …
Run Code Online (Sandbox Code Playgroud)

c++ malloc

2
推荐指数
1
解决办法
359
查看次数

标签 统计

c++ ×1

malloc ×1