tags:

views:

107

answers:

4

I have tried a lot to learn linked list.But all my efforts were wasted.Please can some one help me understand linked list by providing his/her own code?Thanks in advance.

+1  A: 

A linked list is a series of object each pointing to the next one in the list. The last element in the list has NULL as it's next pointer.

You keep track of the head of the list in your program so you can traverse the list from the start.

You might want to keep track of the current position in the list, but that will depend on your application.

A doubly linked list has a previous element pointer too enabling you to traverse the list in both directions.

This:

typedef struct tagProp
{
   rtPropertyKey   key;
   rtProperty      property;
   struct tagProp *next;
} TProperty;

defines a property key/value lookup list.

ChrisF
+12  A: 

A linked list is simply a list of elements (usually called nodes) where each node has a reference (or pointers, in C) to the next node:

You keep track of the list by having a pointer to the first node (the "head"), and by having the last node point to null

alt text

Linked lists where each element points to both the next and previous nodes are called doubly-linked lists.

alt text

By following these references, you can traverse the list and get any node.

A common advantage of linked lists over arrays is that you can insert and remove the elements in O(1) (constant) time. The disadvantage is that you have O(N) random-access.

See Wikipedia for more.

NullUserException
Did you make those diagrams yourself? They look good - What tool did you use if you made them yourself?
mathepic
Oh, nitpick: they don't point to NULL, they have a pointer of value NULL. (+1 by the way)
mathepic
@math I used http://yuml.me
NullUserException
Oh, another thing you should add here - Although removing is O(1), you have to use the O(n) traversing to get to the element you need to remove.
mathepic
+1  A: 

A linked list is implemented as a list of "nodes". The nodes are linked together using pointers. The following code describes one node. The pointer to the next node is called next. In my example, each node contains an integer value as its data.


struct node {
   int val;
   struct node * next;
};

The fun is how to actually create a list. You have to use malloc to create new nodes. When you malloc the new node, you have to tie into the list using the next pointer.

We can help you more if you specifically tell us what your issues are...

Starkey
+2  A: 

Have you play some of these rally games? The organizer left this hints all around the city and you must get one hint and then solve the riddle for getting the position of the next hint. Now, imagine every hint comes with a little prize.

Well, linked list are like that: every element has "content" on it AND the memory address (the hint) for getting the next item. The next item, of course, has another prize and another hint.

Erik Escobedo