I''m new to c++ and I am having problems with linked lists. I feel as if I have a descent amount of the code correct, but my output as not the same as how it should be. I know I am not iterating through my list, but I can't really seem to figure out how to correct this. Here is the screenshot of the output its suppose to produce.
this is my code so far.
my LList.h
Code:
#ifndef LList_h
#define LList_h
#include <iostream>
#include <cstddef>
#include "node.h"
class LList
{
public:
LList(void); //constructor
LList(const LList &); //copy constructor
~LList(); //destructor
void displayList();
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 &l);
private:
Node *_head;
Node *_tail;
LList *front; //points to front of the list
string _str;
};
inline LList::LList(void)
{
cerr << "head = tail = 0 at 0024f8d0\n";
_head = 0;
_tail = 0;
front = 0;
}
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;
}
}
ostream &operator <<( ostream &out, const LList & llist )
{
for( LList *p = LList._head; p != 0; p = p -> next )
out << p;
return out;
}
LList & LList::operator=(const LList &l)
{
_head = 0;
_tail = 0;
return *this;
}
inline LList::~LList()
{
//delete _head;
//delete _tail;
}
/*inline LList::~LList( )
{
Node *p = new Node (_str);
if ( _head == 0)
{
_head = p;
}
else
{
Node *q;
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
} //_head still points to the first node
} */
#endif
and this is my main.cpp file
Code:
#include "LList.h"
#include <iostream>
using namespace std;
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;
}
my errors and changes would only be made to my LList.h file. But what is happening is I only get the output to say list a: but my list is not there. any ideas?