我一直在尝试制作一个双向链接列表(我感觉自己很亲近),但是我似乎无法获得正确的输出。我的输出似乎向我抛出了一些非常随机的值。我觉得问题出在我的操作员超负荷,但是我似乎无法理解它,早晨的太阳接近XD。非常感谢大家的帮助!

另外,为方便起见,预期输出为:(忽略行之间的空格)

马西王子

乔尔·戴维·康拉德·马西亲王

Nic Lindo Ernest乔尔·戴维·康拉德·马西王子

Nic Lindo Ernest乔尔·戴维·康拉德·马西王子

Lindo Ernest乔尔·戴维·康拉德·马西王子

Lindo Ernest乔尔·大卫·康拉德·皮斯·马西·杰西·谢恩·理查德

Lindo Ernest乔尔·大卫·康拉德·皮斯·马西·杰西·谢恩

Lindo Ernest乔尔·大卫·康拉德·皮斯·马西·杰西

Lindo Ernest乔尔·大卫·康拉德·皮斯·马西

Lindo Ernest乔尔·戴维·康拉德·皮斯

#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

class StringNode{
    public:
        string elem;
        StringNode* next;
        StringNode* prev;
        friend class StringLinkedList;
};

class StringLinkedList{
    public:
        StringLinkedList();
        ~StringLinkedList();
        bool isEmpty() const;
        const string& front() const;
        const string& back() const;
        void addFront(const string& e);
        void addBack(const string& e);
        void removeFront();
        void removeBack();
        friend ostream& operator<<(ostream& out, const StringLinkedList& obj);
    private:
        StringNode* head;
        StringNode* tail;
    protected:
        void add(StringNode* v, const string& e);
        void remove(StringNode* v);
};

StringLinkedList::StringLinkedList(){
    head = new StringNode;
    tail = new StringNode;
    head->next = tail;
    tail->prev = head;
}

StringLinkedList::~StringLinkedList(){
    while(!isEmpty()){
        removeFront();
    }
    delete head;
    delete tail;
}

bool StringLinkedList::isEmpty() const{
    return (head->next == tail);
}

const string& StringLinkedList::front() const{
    return head->next->elem;
}

const string& StringLinkedList::back() const{
    return tail->prev->elem;
}

void StringLinkedList::add(StringNode* v, const string& e){
    StringNode* u = new StringNode; u->elem = e;
    u->next = v;
    u->prev = v->prev;
    v->prev->next = v->prev = u;
}

void StringLinkedList::addFront(const string& e){
    add(head->next, e);
}

void StringLinkedList::addBack(const string& e){
    add(tail, e);
}

void StringLinkedList::remove(StringNode* v){
    StringNode* u = v->prev;
    StringNode* w = v->next;
    u->next = w;
    w->prev = u;
    delete v;
}

void StringLinkedList::removeFront(){
    remove(head->next);
}

void StringLinkedList::removeBack(){
    remove(tail->prev);
}

ostream& operator <<( ostream& out, const StringLinkedList &obj )
{
    for ( StringNode *temp = obj.head->next; temp != obj.tail; temp = temp->next )
    {
        out << temp->elem << ' ';
    }
    return out;
}

int main(void){
    StringLinkedList* myList = new StringLinkedList();
    myList->addFront("Massi");
    myList->addFront("Prince");
    cout<< *myList << endl;
    myList->addFront("Conrad");
    myList->addFront("David");
    myList->addFront("Joel");
    cout<< *myList << endl;
    myList->addFront("Ernest");
    myList->addFront("Lindo");
    myList->addFront("Nic");
    cout<< *myList << endl;
    myList->addFront("Sasha");
    myList->removeFront();
    cout<< *myList << endl;
    myList->removeFront();
    cout<< *myList << endl;
    myList ->addBack("Jesse");
    myList ->addBack("Shane");
    myList ->addBack("Richard");
    cout << *myList << endl;
    myList -> removeBack ();
    cout << *myList << endl;
    myList -> removeBack ();
    cout << *myList << endl;
    myList -> removeBack ();
    cout << *myList << endl;
    myList -> removeBack ();
    cout << *myList << endl;
    return 0;
}

最佳答案

这个运算符

ostream& operator<<(ostream& out, const StringLinkedList& obj){
    StringNode* temp = obj.head;
    while(temp != NULL){
        out << temp->elem << ' ';
        temp = temp->next;
    }
    return out;
}


这是错误的,因为它试图输出不存储字符串的elem的数据成员head和也不存储字符串的tail数据成员,而且具有未初始化的成员next

该功能可以通过以下方式定义

ostream& operator <<( ostream& out, const StringLinkedList &obj )
{
    for ( StringNode *temp = obj.head->next; temp != obj.tail; temp = temp->next )
    {
        out << temp->elem << ' ';
    }

    return out;
}


另外函数add的最后一条语句是错误的

v->prev->next = v->prev = u;


您应该将其分为两个语句。例如

void StringLinkedList::add( StringNode *v, const string &e )
{
    StringNode *u = new StringNode; u->elem = e;
    u->next = v;
    u->prev = v->prev;
    u->prev->next = u;
    v->prev = u;
}

09-20 22:19