我试图实现一个双向链表,其中节点存储在堆栈上,但堆上的节点数据.您可以使用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)