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
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?
I''m new to c++ and I am having problems with linked lists.
1) Always plan on paper how a linked list works. Did you do that first? If you did that, there is little reason for the code not to work if you first write how a linked list works on paper by drawing boxes with links showing insertion, deletion, etc. and then write the code according to that plan.
2) Did you debug your code using the debugger? I don't see any indication of that (you wrote code, you ran the code, but no mention of stepping through the code and debugging it yourself). If you debugged your code, where does your linked list implementation fail to not follow your plan from step 1) above?
3) Why did you comment out the destructor? The linked list class is faulty if it doesn't clean up the memory.
I ran my code, and it runs fine. But when I run my code, my output doesn't look like the screenshot output. My output just displays head = tail = 0 = '#', list a:
I commented out the destructor because as soon as I ran my code, the program crashed.
In your screen shot it shows the actual contents of head and tail and the actual memory address. On your system the memory address to show will almost certainly be different from that shown on the screen shot!
I commented out the destructor because as soon as I ran my code, the program crashed.
and doesn't that tell you something??? so why does your program crash? It's no good just commenting out some code because it causes problems - you MUST understand why this causes the problem by using the debugger and then fix it!!!
How does the code above set one linked list equal to another linked list? It is a bogus assignment operator, and bogus assignment operators should not be coded, since it affects the overall running of any code using LList.
You need to code a proper assignment statement, one that actually makes one linked list equal to the other linked list.
Code:
inline LList::~LList( )
{
node *p = new Node (_str);
Why are you allocating a new node in the destructor? You're supposed to point to the head node, and just traverse each node removing each one.
Bookmarks