If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Linked List Problems

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.

Re: Linked List Problems

Originally Posted by needhelp101

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.

Re: Linked List Problems

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.