0

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 screenshotenter image description here

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

beginnerprogrammer
  • 183
  • 2
  • 5
  • 22
  • 2
    Start by writing a one sentence specification. What do you want to happen? – Eric Lippert Feb 14 '13 at 01:28
  • Then the next two sentences of the specification should consider the two basic cases. Either the list is empty or it has one or more items. Describe what happens in each case. – Eric Lippert Feb 14 '13 at 01:32
  • Keep writing a more and more detailed specification. Then writing the code to match your spec will be much easier. – Eric Lippert Feb 14 '13 at 01:33
  • To be honest, I'm not even really sure what is supposed to happen. I am working off of very unclear instructions and just a screenshot of what the output is supposed to look like – beginnerprogrammer Feb 14 '13 at 01:41
  • @JohnTinio Tell us something interesting. Hell, just poste that wretched screenshot if you really cannot describe it. (But remember, we're not a code writing service) – sehe Feb 14 '13 at 01:44
  • yes, I understand this is not a code writing service, but I'm just trying to understand this and figure out how all this works – beginnerprogrammer Feb 14 '13 at 01:54

1 Answers1

1

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.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • I'm not even sure if I should be using the scope operator here. I am coding based off of a screenshot, and have no clear cut directions on what to do – beginnerprogrammer Feb 14 '13 at 01:42
  • @JohnTinio: The only reasonable thing to do when implementing `operator<<` for a container (like your `LList`) is to delegate the work to other overloads of `operator<<` for the respective elements. If you want to dump a list, this is the way to go. However, note that my example code above is just to give you a feeling how that overloading works, you still have to throw in a loop to print every element ;) – bitmask Feb 14 '13 at 01:45
  • yes, I understood that part. I knew I would have to put a loop in, but I am not sure of what would be going in as the conditions – beginnerprogrammer Feb 14 '13 at 01:59
  • @JohnTinio: Ah. Well, you must have some means to walk through your loop. The standard library ([`std::list`](http://en.cppreference.com/w/cpp/container/list)) uses iterators such that you can use the idiomatic `begin()`/`end()` pair. However, your list might work differently. You basically have to start with your `_head` pointer and print each element (by `<<`ing it into the stream reference) until `_tail`. The screenshot also suggests a line break after each line, which should be written as `std::endl`, not `"\n"`. – bitmask Feb 14 '13 at 02:12
  • would the condition in the for loop look something like this... for( p = llist._head; p != 0; p = p -> next ) – beginnerprogrammer Feb 14 '13 at 02:23
  • @JohnTinio: Looks good to me. However, it doesn't seem really clean for your `LList` to expose its `_head` member. Depends on how much emphasis your assignment puts on good data structure design. – bitmask Feb 14 '13 at 02:27
  • @BenjaminLindley: Portability. – bitmask Feb 14 '13 at 02:27
  • but the thing is _head I have in the class in the private part of the class as...... Node *_head. And I receive an error saying value of type "Node" cannot be assigned to an entity type of "LList" – beginnerprogrammer Feb 14 '13 at 02:30
  • To see what is going on, should I place my LList.h file in as well? – beginnerprogrammer Feb 14 '13 at 02:40
  • In what sense is `std::endl` more portable than `'\n'`? – Benjamin Lindley Feb 14 '13 at 02:42
  • @JohnTinio: Sounds like a separate question to me. – bitmask Feb 14 '13 at 02:42
  • @BenjaminLindley: The standard says so. – bitmask Feb 14 '13 at 02:43
  • I can't seem to really figure this out. Just trying to understand this, and getting frustrated at the same time – beginnerprogrammer Feb 14 '13 at 02:46
  • @BenjaminLindley: Sorry, [it would appear I was wrong](http://en.cppreference.com/w/cpp/io/manip/endl). See also http://stackoverflow.com/a/213977/430766 – bitmask Feb 14 '13 at 02:48