简体   繁体   中英

Overloading << operator in linked list

I am having trouble creating the overloading coding for this. Not really sure where to start or how to even start. I am new to c++ and having trouble understanding linked lists and nodes, even after doing reading on this. this is what I have so far.

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

here is the screenshot 在此处输入图片说明

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

I'm not really sure where I am at with this. I'm just trying things and getting some ideas from some of the examples from my professor

You basically just << the elements you want printed inside your overload. For instance, assuming you have a LList::front() member function returning the first element, you could print that like this:

ostream &operator <<( ostream &out, const LList& llist ) {
  return out << llist.front();
}

Obviously you would want to print the whole list, not just the first element (and check if the list is empty) but that is done the same way. This assumes that there is an overload for the elements that are stored by your LList , if not, you have to provide that as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM