C++ 超载<&书信电报;链表中的运算符
我在为此创建重载编码时遇到问题。不确定从哪里开始,甚至不知道如何开始。我是新的C++,即使在阅读了这个链接之后,也无法理解链接列表和节点。这就是我目前所拥有的C++ 超载<&书信电报;链表中的运算符,c++,C++,我在为此创建重载编码时遇到问题。不确定从哪里开始,甚至不知道如何开始。我是新的C++,即使在阅读了这个链接之后,也无法理解链接列表和节点。这就是我目前所拥有的 #include "LList.h" #include <iostream> using namespace std; std::ostream& operator<<(ostream& out, const LList& llist); int main( ) { LList a;
#include "LList.h"
#include <iostream>
using namespace std;
std::ostream& operator<<(ostream& out, const LList& llist);
int main( )
{
LList a;
a.push_back( "30" );
a.push_front( "20" );
a.push_back( "40" );
a.push_front( "10" );
a.push_back( "50" );
cout << "list a:\n" << a << '\n';
return 0;
}
ostream &operator <<( ostream &out, const LList& llist )
{
LList :: //not sure what to really put from here
return out;
}
#包括“LList.h”
#包括
使用名称空间std;
std::ostream&operator您基本上只需编写一句话的规范即可。你想做什么?接下来的两个句子应该考虑两个基本情况。列表为空或有一个或多个项目。描述每种情况下发生的情况。继续编写越来越详细的规范。那么,编写符合您的规范的代码会容易得多。老实说,我甚至不确定应该发生什么。我正在处理非常不清楚的指令,只是一个输出应该是什么样子的屏幕截图like@JohnTinio告诉我们一些有趣的事情。见鬼,如果你真的无法描述的话,就把那张可怜的截图贴出来吧。(但请记住,我们不是代码编写服务)我甚至不确定是否应该在这里使用scope操作符。我是根据截图编码的,没有明确的方向do@JohnTinio:实现运算符时唯一合理的做法是,我理解这一部分。我知道我将不得不加入一个循环,但我不确定在这个过程中会发生什么conditions@JohnTinio:啊。好吧,你一定有办法穿过你的圈子。标准库()使用迭代器,这样您就可以使用惯用的begin()
/end()
对。但是,您的列表可能会有不同的工作方式。基本上,您必须从\u head
指针开始,并打印每个元素(通过for循环中的条件是否会像这样…for(p=llist.\u head;p!=0;p=p->next)
#ifndef LList_h
#define LList_h
#include <iostream>
#include "node.h"
class LList
{
public:
LList(void); //constructor
LList(const LList &); //copy constructor
~LList(); //destructor
LList *next; //points to next node
void push_back(const string &str);
void push_front(const string &str);
friend ostream& operator<<(ostream& out, const LList& llist);
LList &operator=(const LList &);
private:
Node *_head;
Node *_tail;
LList *front; //points to front of the list
};
inline LList::LList(void)
{
cerr << "default constructor";
}
inline void LList::push_back(const string &str)
{
Node *p = new Node(str);
if (_tail == 0)
{
_head = _tail = p;
}
else
{
_tail ->next(p);
_tail = p;
}
if (_head == 0)
{
_head = _tail = p;
}
else
{
_head ->next(p);
_head = p;
}
}
inline void LList::push_front(const string &str)
{
Node *p = new Node(str);
if (_tail == 0)
{
_head = _tail = p;
}
else
{
_tail ->next(p);
_tail = p;
}
if (_head == 0)
{
_head = _tail = p;
}
else
{
_head ->next(p);
_head = p;
}
}
inline LList::~LList( )
{
Node *p = new Node (str);
if ( _head == 0)
{
_head = p;
}
else
{
Node *q;
//&Node::next;
for (q = _head; q->next(); q = q -> next)
{
//loop until we have
//q pointing to the last node
}
q->next ( p); //last node points to p
} //_uead still points to the first node
}
#endif
ostream &operator <<( ostream &out, const LList& llist ) {
return out << llist.front();
}