Asking for pointer explanation (in a linked list)

Im trying to understand pointers & linked lists, so far so good, but there is one thing i cant get my head around.
I created a Linked list of a struct, witch contains just an int x and *next ofcourse. I make 4 structs in the list, and now im trying to delete the first in the list.

(Number is the struct and Head is pointing to the start of the list.)

1
2
3
Number *Current;
Current=Head; //Current points to the same thing Head is pointing to.
Current=Head->Next; //Why does this line make Head pointing to the next in the list!? Is it a law that Current=Head & Head = Current now? o_O 


And another thing, tried to make the samething with something i find to be logical.

1
2
3
Number *Current;
Current=Head;
Head=Current->Next;


These lines delete the last in the list of my 4 structs, Im Conpuzzled.
Thanks in advance! :)
Last edited on
Why does this line make Head pointing to the next in the list!?
It doesn't. How do you come to this conclusion? Only Current is modified.
Line 2 is has no effect

These lines delete the last in the list of my 4 structs
It doesn't. It removes the head and replaces it with the next. Same as Head=Head->Next;
Im starting to think that my tutorial aint that good.. http://www.functionx.com/cpp/articles/linkedlist.htm
To delete the first of the list, i need head pointing to the 2nd element, so the first one is no longer in the list, am i right?
Could someone please maybe give me a good link on this topic?

Nevermind that, got my head around it, but i have a new question. The line below, they work just fine, but i cant understand why.. Could someone, please, explain? I even tried to write on paper, but still could not understand whats going on here :|

1
2
3
4
5
6
7
8
9
Number *Retrieve(int Position)
	{
		Number *Current=Head; 
		for(int i=Count()-1; i>Position && Current!=NULL; i--) 
		{
			Current=Current->Next; 
		}
		return Current;
	}
Last edited on
Topic archived. No new replies allowed.