This is topic Linear Linked List, help and also why Pointers are EVIL in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=048099

Posted by Blayne Bradley (Member # 8565) on :
 
code:
class List
{
public:
List( /* in */ const char dataLine[] );
~List( );
void PrintList( ) const;
void ReversePrintList( ) const;
private:
struct Node
{
char item;
Node* next;
};

Node* front;


};

List::List( /* in */ const char dataLine[] )
{


int temp = strlen(dataLine);

for ( int counter = 0; counter < temp; counter++)
{
front = new Node;
front->item = dataLine[counter];
front->next = front;


cout << front->item << endl;
}

}

basicaly I am trying to make a linear linked list with pointers. In main I pass the constructor an array with the items say a,b,c, and d.

However I am dereferencing a null value somewhere and I am not quite sure where.

http://img102.imageshack.us/my.php?image=dereferenceid9.png

heres the what error looks like.

So ya can anyone help me.
 
Posted by Blayne Bradley (Member # 8565) on :
 
Okay, fixed it apparently i had a pointer pointing to nowhere but alas my linked list still isnt working right. for some reason each front->item is always equaling to a. and I am not sure how I am doing it wrong.
 
Posted by El JT de Spang (Member # 7742) on :
 
You've never gonna be worth much as a programmer if you run for help every time you run into an error message.

Learn to debug your own programs -- I can't stress how important a skill this is.
 
Posted by MrSquicky (Member # 1802) on :
 
When working with nodes and pointers, I recommend drawing out what you are expecting to happen so that you can identify where it goes wrong. Your problem here looks to be pretty basic and that should fix you right up.

Also, what JT said. If you ever want to get hired as a programmer, now is the time to develop some self-reliance in debugging.
 
Posted by Dagonee (Member # 5818) on :
 
I had little magnets with node boxes drawn on them that stuck to a white board, plus color-coded magnetic arrows. Worked great for stepping through iterations.

The magnets had dry-erasable surfaces, so I could put in values.

Make each box a node, draw arrows from the pointer field in the struct, use the colored arrows to represent pointer variables.
 
Posted by King of Men (Member # 6684) on :
 
Stepping through code in your head, and using cout statements, are both great friends for a programmer. You should note that a human brain actually makes a supremely excellent debugging interpreter for C++.
 
Posted by King of Men (Member # 6684) on :
 
And dude, did you even read your own code? Please do take a look at what you are doing with that assignment of front.
 
Posted by Bokonon (Member # 480) on :
 
Do you not have breakpoints in your environment? I also recommend a whiteboard/chalkboard/piece of paper. Then step through line by line your constructor, with boxes for a node object, and an arrow for the node pointer in the Node. also have a box labelled null. Try this with a dataLine with one value, and then try it with 2 values in dataLine. If you still don't see it, try with 3 values. If you _still_ don't see it, talk to your teacher('s assistant).

Also, for your own sanity, explicitly initialize your pointers...

-Bok
 
Posted by Chanie (Member # 9544) on :
 
Download gdb (or some other debugger). You'll find the problem right away.
 
Posted by The Pixiest (Member # 1863) on :
 
If you are having problems with such a simple program of your own creation, you will suffer greatly when you have to debug someone elses code and you have no clue what their rational is.

Further, pointers are everything. If you can't handle them/hate them and can not learn to love them, maybe you should look for another career.
 
Posted by Chanie (Member # 9544) on :
 
Ah, as I click on your error, I see that you are using an IDE. Did you try to use the debugger? When I TA'ed an intro class I wouldn't even talk to a student unless they could tell me what happened when they tried to run it in the debugger.
 
Posted by Ken (Member # 10082) on :
 
Master pointers and the building of simple data structures like a linked list and then higher level languages like ruby will be a real treat!

The first couple programming classes can be rough because you have no foundation but it's worth the trouble.
 
Posted by Will B (Member # 7931) on :
 
Let's be merciful. We can't expect beginning programmers to be as adept as experts.

Blayne, you weren't ready to compile yet, because you needed to walk thru your code as others here recommended: visually, going thru the steps of your function, drawing yourself a box for each variable you make, filling in its contents with the values you get (which will be arrows pointing to other things, if they're pointers), and see what happens. When you have done that and it seems to give you the right result, then you're ready to write it as real code and compile it. If you don't know what I mean, get a prof or TA to show you -- it is a skill you will use over and over again.

Your problem here is that you have, for each Node you create, the next pointer pointing to that same Node. That is, you're creating a bunch of circular lists of length 1.
 
Posted by Dagonee (Member # 5818) on :
 
BTW, being able to fluidly work with various forms of indirection is a very useful mental process to have in you arsenal. The uses in programming are obvious and way beyond pointers.

But when I went to law school, it was immediately useful.

Practice doing this and being able to keep straight in your head whether you are talking about a thing, or something that refers to that thing, and you will have developed a mode of thought that will serve you well in many situations.
 
Posted by Blayne Bradley (Member # 8565) on :
 
on the car ride home I figured out what the problem is for some reason its making a circular linked list by pointing to itself so each time it goes through the loop it simple replaces itself and then points to itself. Now I just need to figure out how to not make it circular.

I have couts and I have a debugger and it howed it pointing to itself for infinity but i hadnt realized at the time that I was looking at a 1 node circular linked list.


*Did not read Wills post before posting this*
 
Posted by Bokonon (Member # 480) on :
 
Don't just rely on your program. Independently verify your algorithm (especially school projects) using paper/whiteboard/your brain. Use one to validate the other. And don't make assumptions when doing it out by hand.

-Bok
 
Posted by Scott R (Member # 567) on :
 
Dude, follow your heart.

That's what I always do.
 
Posted by King of Men (Member # 6684) on :
 
quote:
for some reason its making a circular linked list by pointing to itself
Just like you told it to. Dude, it's staring you right in the face. Step through the code in your head like we've been telling you; it'll jump right out. By the way, do you have a sub for Poland or Greece tomorrow?
 
Posted by Blayne Bradley (Member # 8565) on :
 
front->next = front; THIS is whats causing it, Im just tryign to figure out now how to get it not to do that, the examples online dont help.
 
Posted by Chanie (Member # 9544) on :
 
If you were to say it in English, what would you want the next arrow of front pointing to?
 
Posted by MrSquicky (Member # 1802) on :
 
It's important to keep in mind that a linked list should generally also end (i.e. have its last node pointing to nothing).

---

Stop looking for examples on the web, take people's advice here, and figure it out on your own. This is something you are going to need to be able to visualize for yourself, and the only way to do that is to do it.

[ March 30, 2007, 04:01 PM: Message edited by: MrSquicky ]
 
Posted by King of Men (Member # 6684) on :
 
You need to think iteratively. Suppose you are making the second node of the chain; what needs to happen to the first one?
 
Posted by Bokonon (Member # 480) on :
 
quote:
Originally posted by MrSquicky:
It's important to keep in mind that a linked list should generally also end (i.e. have its last node pointing to nothing).

This ties in with my suggestion to explicitly initialize your struct values/pointers (for this exercise, until you understand it). If you don't, your assumptions can be wrong.

-Bok
 
Posted by Blayne Bradley (Member # 8565) on :
 
code:
List::List( /* in */ const char dataLine[] )
{
Node* current;

front = new Node;
front->item = dataLine[0];


int temp = strlen(dataLine);

for ( int counter = 1; counter < temp; counter++)
{

current = front;

Node* newPtr;
newPtr = new Node;

newPtr->item = dataLine[counter];

*while ( current->next != NULL )
{
// stuff
current = current->next;

}

current->next = newPtr;


cout << newPtr->item << endl;

}

}

k with KoM guiding me and helping me understand this I have rewritten my code but it says when I run it that there is an unhandled exception at some place in memory access vilation reaing location some place in memory.

At the * line, this is with the array [ 'a', 'b', 'c', 'd' ] being passed.
 
Posted by fugu13 (Member # 2859) on :
 
Are you intializing Node->next to NULL in the Node constructor? If not, there's no guarantee that an uninitialized pointer will have a particular value. Its just some (essentially random) memory location, which can vary depending on implementation and the state of your computer.

Also, your code still has logic problems. I believe it will work, but its doing things in an extremely circuitous fashion.
 
Posted by Nighthawk (Member # 4176) on :
 
It's confusing, and it's extremely inefficient to determine the end of the list each iteration. Best to keep track of what the last node is. I explain through code...

(Code omitted...)

[ March 31, 2007, 04:54 PM: Message edited by: Nighthawk ]
 
Posted by Will B (Member # 7931) on :
 
Nighthawk, I ask you to consider removing your post. Using your code for his assignment would be cheating, and I don't think that was Blayne's intent. We should make it easy for him to stay on the straight and narrow!

Blayne, how did it work out when you drew a diagram of what was happening, and walked thru the code?

The Wikipedia entry has some examples of this approach. http://en.wikipedia.org/wiki/Linked_list
 
Posted by Nighthawk (Member # 4176) on :
 
Sorry about that... I have a tendency of better describing things through code. The disadvantage of being self-taught as opposed to going through a curriculum is that sometimes it's hard explaining things using the same terms as those instructed.
 
Posted by Will B (Member # 7931) on :
 
No problemo.
 
Posted by Nighthawk (Member # 4176) on :
 
Let me try to explain then...

You have to keep two variables: where you started and where you are.

Knowing where you are allows you to not have to plow through the array from the beginning every time you have to add something to the end. Most linked list implementations in data structures, such as queues, maintain the concept of a "head" and a "tail". You add on to the "tail", and remove from the "head" (or vice-versa).

Also, it's a bad idea to initialize the variable with the first character in "dataLine": what if there is none and the string is zero length? For that matter, your last implementation is one off because you're using "strlen", but not taking in to consideration the character you already initialized with.
 
Posted by King of Men (Member # 6684) on :
 
You're right about the inefficiency, but I thought it valuable to learn how to iterate through a linked list to get to the end, as a thought exercise. And the non-initialised pointers were indeed what was causing the problem.

Edit: And he is indeed taking into account the character he's used, that's why the loop begins at 1. Though your point about empty strings is well taken.
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2