我无法为此创建重载编码。不确定从哪里开始或如何开始。我是 C++ 新手,即使在阅读了此内容之后,也无法理解链表和节点。这就是我到目前为止所拥有的。
#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
#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
我不确定我在哪里。我只是在尝试并从教授的一些例子中得到一些想法