我一直试图制作一个双重链接列表(我觉得我太近了),但我似乎无法获得正确的输出。我的输出似乎给我一些非常随意的值。我觉得问题是由于我的操作员超载,但我似乎无法理解它,早晨的太阳接近XD。非常感谢你们的帮助!
同样为方便起见,预期输出为:(忽略行之间的空格)
Prince Massi
Joel David Conrad Prince Massi
Nic Lindo Ernest Joel David Conrad Prince Massi
Nic Lindo Ernest Joel David Conrad Prince Massi
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
Lindo Ernest Joel David Conrad Pince Massi Jesse
Lindo Ernest Joel David Conrad Pince Massi
Lindo Ernest Joel David Conrad Pince
#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;
}
答案 0 :(得分:0)
假设u
按顺序排列。
以下代码
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; // this line has the issue
}
执行以下过程(假设1 =头部,2 = v,3 =尾部)
让我们在v = 2和tail = 3之间添加“4”,即执行add(tail,data)。
u = 4; 4的下一个是尾巴 4的前一个是tail(3)的当前前一个,即2 现在我们修改原始列表的向后链接。然后3的前一个是4,但是然后4(现在是v-> prev)的下一个是4,这本身就是。
逐行浏览代码确实很有帮助。使用橡皮鸭:
答案 1 :(得分: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
,而且还有未初始化的成员{{ 1}}。
可以通过以下方式定义该功能
next
此外,函数add的最后一个语句是错误的
ostream& operator <<( ostream& out, const StringLinkedList &obj )
{
for ( StringNode *temp = obj.head->next; temp != obj.tail; temp = temp->next )
{
out << temp->elem << ' ';
}
return out;
}
您应该将其拆分为两个语句。例如
v->prev->next = v->prev = u;