Professional Documents
Culture Documents
Definition: A list implemented by each item having a link to the next item
A linked list is called so because each of items in the list is a part of a structure, which is
linked to the structure containing the next item. This type of list is called a linked list since it
can be considered as a list whose order is given by links from one item to the next.
Structure
Linked list is one of the fundamental data structures, and can be used to implement other data
structures. In a linked list there are different numbers of nodes. Each node is consists of two
fields. The first field holds the value or data and the second field holds the reference to the
next node or null if the linked list is empty.
A structure which contains a data element and a pointer to the next node is created by,
struct list {
int value;
struct list *next;
};
This defines a new data structure called list (actually the definition of a node), which contains
two members. The first is an integer called value. The second is called next, which is a
pointer to another list structure (or node). Suppose that we declare two structures to be of the
same type as list, eg,
main()
{
struct list n1, n2, n3;
int i;
n1.value = 100;
n2.value = 200;
n3.value = 300;
n1.next = &n2;
n2.next = &n3;
i = n1.next->value;
printf("%d\n", n2.next->value);
}
main()
{
struct list n1, n2, n3, n4;
struct list *list_pointer = &n1;
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n3.next = &n4;
n4.value = 400;
n4.next = 0;
while( list_pointer != 0 ) {
printf("%d\n", list_pointer->value);
list_pointer = list_pointer->next;
}
}
Advantages of Linked List:
A linked list is a dynamic data structure and therefore the size of the linked list can grow or
shrink in size during execution of the program. A linked list does not require any extra space
therefore it does not waste extra memory. It provides flexibility in rearranging the items
efficiently.
The limitation of linked list is that it consumes extra space when compared to an array since
each node must also contain the address of the next item in the list to search for a single item
in a linked list is cumbersome and time consuming.
Circular list
Definition: A variant of a linked list in which the nominal tail is linked to the head. The
entire list may be accessed starting at any item and following links until one comes to the
starting item again.
Doubly linked list
Definition: A variant of a linked list in which each item has a link to the previous item as
well as the next. This allows easily accessing list items backward as well as forward and
deleting any item in constant time.