user379888
user379888

Reputation:

please can some one help me explain linked list?

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.

Upvotes: 0

Views: 424

Answers (4)

Erik Escobedo
Erik Escobedo

Reputation: 2803

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.

Upvotes: 2

NullUserException
NullUserException

Reputation: 85458

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:

http://img837.imageshack.us/img837/5613/ll1s.png

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.

Upvotes: 12

Starkey
Starkey

Reputation: 9781

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...

Upvotes: 1

ChrisF
ChrisF

Reputation: 137148

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.

Upvotes: 1

Related Questions