gpt4 book ai didi

c++ - 双向链表的输出

转载 作者:太空宇宙 更新时间:2023-11-04 13:29:34 24 4
gpt4 key购买 nike

我一直在尝试制作一个双向链表(我觉得我已经很接近了),但我似乎无法获得正确的输出。我的输出似乎向我抛出一些非常随机的值。我觉得问题是由于我的运算符(operator)重载造成的,但我似乎无法理解它并且早晨的太阳接近 XD。非常感谢大家的帮助!

同样为了方便起见,预期的输出是:(忽略行与行之间的空格)

马西王子

乔尔大卫康拉德王子马西

Nic Lindo Ernest Joel David Conrad Prince Massi

Nic Lindo Ernest Joel David Conrad Prince Massi

林多·欧内斯特·乔尔·大卫·康拉德·马西王子

Lindo Ernest Joel David Conrad Pince Massi Jesse Shane Richard

Lindo Ernest Joel David Conrad Pince Massi Jesse Shane

林多·欧内斯特·乔尔·大卫·康拉德·平斯·马西·杰西

林多·欧内斯特·乔尔·大卫·康拉德·平斯·马西

林多·欧内斯特·乔尔·大卫·康拉德·平斯

#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;
}

是错误的,因为它试图输出 head 的数据成员 elem 不存储字符串,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;
}

关于c++ - 双向链表的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32058994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com