You are on page 1of 4

Linked list

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,

struct list n1, n2;

The next pointer of structure n1 may be set to point to the n2 structure by

/* assign address of first element in n2 to the pointer next of the n1 structure */


n1.next = &n2;

which creates a link between the two structures.

/* LLIST.C Program to illustrate linked lists */


#include <stdio.h>
struct list {
int value;
struct list *next;
};

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);
}

Traversing a linked list

/* Program to illustrate traversing a list */


#include <stdio.h>
struct list {
int value;
struct list *next;
};

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.

Types of linked list:

There are different kinds of linked lists they are


Linear singly linked list,
Circular singly linked list,
Two way or doubly linked list,
Circular doubly linked list.

Linear singly linked list


The simplest kind of linked list is a singly-linked list , which has one link per node. This
link points to the next node in the list, or to a null value or empty list if it is the final node. A
singly linked list’s node is divided into two parts. The first part holds or points to information
about the node, and second part holds the address of next node. A singly linked list travels
one way.

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.

Doubly circular linked list


A circular list is formed by making use of pointers which would otherwise be null: The last
element of the list is made the predecessor of the first element; the first element, the
successor of the last. The upshot is that we no longer need both a head and tail pointer to keep
track of the list. Even if only a single pointer is used, both the first and the last list elements
can be found in constant time.

You might also like