我使用单链表编写了此代码。现在,我想将其更改为双向链表。我尝试了几种不同的方法,但是一切都搞砸了。
我的代码中没有多少行没用,但基本上是一个单链表。将其更改为双向链表的正确方法是什么?

#pragma once

#include "Exhibit.h"

struct Node
{
    Exhibit exhibit;
    Node *next;
};

class Museum
{
private:
    Node *p;
    Node *d;
    Exhibit *Ex;
    int n;
    int nmax;
public:
    Museum();
    ~Museum();

    void Start() { d = p; }
    bool HasNext() { if (d == NULL) return false;  return true; }
    void Next() { d = d->next; }
    Exhibit GetExhibit() const { return d->exhibit; }
    int GetN() { return n; }

    void Sort();
    void AddExhibit(Exhibit e);
    void RemoveExpensive(int p); //Removes elements more expensive than int p
};

Museum::Museum()
{
    p = NULL;
    d = NULL;

}

Museum::~Museum()
{
    Node * element;
    while (p)
    {
        element = p;
        p = p->next;
        delete element;
    }
    p = NULL;
    d = NULL;

}

void Museum::AddExhibit(Exhibit e)
{
    Node *element = new Node;

    element->exhibit = e;
    element->next = p;
    p = element;
}

int CountExhibits(string CDfv)
{
    string line;
    int count = 0;
    ifstream fd(CDfv.c_str());
    while (!fd.eof())
    {
        getline(fd, line);
        count++;
    }
    fd.close();
    return count;
}

void Museum::Sort()
{
    // traverse the entire list
    for (Node *list = p; list->next != NULL; list = list->next)
    {
        // compare to the list ahead
        for (Node *pass = list->next; pass != NULL; pass = pass->next)
        {
            // compare and swap
            if (list->exhibit.Date() > pass->exhibit.Date())
            {
                // swap
                Exhibit temp = list->exhibit;
                list->exhibit = pass->exhibit;
                pass->exhibit = temp;
            }
        }
    }
}

void Museum::RemoveExpensive(int pr)
{
    Node *pPre = NULL, *pDel = NULL;


    pPre = p;
    pDel = p->next;

    /* traverse the list and check the value of each node */
    while (pDel != NULL) {
        if (pDel->exhibit.Price() > pr) {
            /* Update the list */
            pPre->next = pDel->next;
            /* If it is the last node, update the tail */
            if (pDel == d) {
                d = pPre;
            }
            delete pDel; /* Here only remove the first node with the given value */
            pDel = pPre;
             /* break and return */
        }
        pPre = pDel;
        pDel = pDel->next;
    }


    /* Check whether it is the head node?
    if it is, delete and update the head node */
    if (p->exhibit.Price() > pr) {
        /* point to the node to be deleted */
        pDel = p;
        /* update */
        p = pDel->next;
        delete pDel;

    }
}

编辑:我新的AddExhibit()方法:
void Museum::AddExhibit(Exhibit e)
{
    Node *element = new Node;

    element->exhibit = e;

    if (p == NULL)
        p = d = element;
    else
    {
        p->prev = element;
        element->next = p;
        p = element;

    }
    //element->next = p;
    //p = element;
}

最佳答案

我使用此AddExhibit()方法解决了我的问题:

void Museum::AddExhibit(Exhibit e)
{
    Node *element = new Node;

    element->exhibit = e;

    element->next = p;
    element->prev = NULL;
    if (p == NULL)
        d = element;
    else
        p->prev = element;
    p = element;
}

我不知道它是否完全正确,但对于我的情况来说效果很好。

09-30 23:11