Linked List
Linked List
A linked list, in simple terms, is a linear collection of data elements. These data
elements are called nodes. Linked list is a data structure which in turn can be used
to implement other data structures. Thus, it acts as a building block to implement
data structures such as stacks, queues, and their variations.
In Fig, we can see a linked list in which every node contains two parts, an integer
and apointer to the next node. The left part of the node which contains data may
include a simple datatype, an array, or a structure. The right part of the node
contains a pointer to the next node (oraddress of the next node in sequence). The
last node will have no next node connected to it, soit will store a special value
called NULL. In Fig, the NULL pointer is represented by X. Whileprogramming,
we usually define NULL as –1.Sincein a linked list, every node contains a pointer
to another node which is of the same type, it is also called a self-referential data
type.
Linked lists contain a pointer variable START that stores the address of the first
node in the list. We can traverse the entire list using START which contains the
address of the first node; the next part of the first node in turn stores the address of
its succeeding nodes.
Both arrays and linked lists are a linear collection of data elements. But unlike an
array, a linked list does not store its nodes in consecutive memory locations.
Another point of difference betweenan array and a linked list is that a linked list
does not allow random access of data. Nodes in alinked list can be accessed only in
a sequential manner. But like an array, insertions and deletions can be done at any
point in the list in a constant time. Another advantage of a linked list over an array
is that we can add any number of elements in the list. This is not possible in case of
an array. For example, if we declare an array as int marks[20], then the array can
store a maximum of 20 data elements only. There is no such restriction in caseof a
linked list.
The computer maintains a list of all free memory cells. This list of available space
is called the free pool. When we delete a particular node from an existing linked
list or delete the entire linked list, the space occupied by it must be given back to
the free pool so that the memory can be reused by some other program that needs
memory space.The operating system does this task ofadding the freed memory to
the free pool. Theoperating system will perform this operation whenever it finds
the CPU idle or whenever the programs are falling short of memory space. The
operating system scans through all the memory cells and marks those cells that are
being used by some program. Then it collects all the cells which are not being used
and adds their address to the free pool, so that these cells can be reused by other
programs. This process is called garbage collection.
A singly linked list is the simplest type of linked list in which every node contains
some data anda pointer to the next node of the same data type. By saying that the
node contains a pointer to thenext node, we mean that the node stores the address
of the next node in sequence. A singly linkedlist allows traversal of data only in
one way.
Traversing a Linked List
Traversing a linked list means accessing the nodes of the list in order to perform
some processing on them. Remember a linked list always contains a pointer
variable START which stores the address of the first node of the list. End of the list
is marked by storing NULL or –1 in the NEXT field of the last node. For
traversing the linked list, we also make use of another pointer variable PTR which
points to the node that is currently being accessed.
In this section, we will discuss how a node is deleted from an already existing
linked list. We will consider three cases and then see how deletion is done in each
case.
In a circular linked list, the last node contains a pointer to the first node of the list.
We can havea circular singly linked list as well as a circular doubly linked list.
While traversing a circularlinked list, we can begin at any node and traverse the list
in any direction, forward or backward,until we reach the same node where we
started. Thus, a circular linked list has no beginning andno ending. The only
downside of a circular linked list is the complexity of iteration. Note that there
areno NULL values in the NEXT part of any of the nodes of list.
Circular linked lists are widely used in operating systems for taskmaintenance.
When we are surfing the Internet, we can use the Backbutton and the Forward
button to move to the previous pages thatwe have already visited. How is this
done? The answer is simple.A circular linked list is used to maintain the sequence
of the Webpages visited. Traversing this circular linked list either in forward
orbackward direction helps to revisit the pages again using Back andForward
buttons. Actually, this is done using either the circular stackor the circular queue.
Inserting a New Node in a Circular Linked List
In this section, we will see how a new node is added into an already existing linked
list. We will take two cases and then see how insertion is done in each case.
Case 1: The new node is inserted at the beginning of the circular linked list.
Case 2: The new node is inserted at the end of the circular linked list.
In this section, we will discuss how a node is deleted from an already existing
circular linked list. We will take two cases and then see how deletion is done in
each case. Rests of the cases of deletion are same as that given for singly linked
lists.
In this section, we will discuss how a new node is added into an already
existing doubly linked list. We will take four cases and then see how
insertion is done in each case.
Case 1: The new node is inserted at the beginning.
Case 2: The new node is inserted at the end.
Case 3: The new node is inserted after a given node.
Case 4: The new node is inserted before a given node.
will take four cases and then see how deletion is done in each case.
A circular doubly linked list or a circular two-way linked list is a more complex
type of linked list which contains a pointer to the next as well as the previous node
in the sequence. The difference between a doubly linked and a circular doubly
linked list is same as that exists between a singly linked list and a circular linked
list. The circular doubly linked list does not contain NULL in theprevious field of
the first node and the next field of the last node. Rather, the next field of the last
node stores the address of the first node of the list, i.e., START. Similarly, the
previous field of the first field stores the address of the last node.
Since a circular doubly linked list contains three parts in its structure, it calls for
more spaceper node and more expensive basic operations. However, a circular
doubly linked list provides the ease to manipulate the elements of the list as it
maintains pointers to nodes in both the directions (forward and backward). The
main advantage of using a circular doubly linked list is that it makes search
operation twice as efficient.
A header linked list is a special type of linked list which contains a header node at
the beginning of the list. So, in a header linked list, START will not point to the
first node of the list but START will contain the address of the header node. The
following are the two variants of a header linked list:
Σ Grounded header linked list which stores NULL in the next field of the last node.
Σ Circular header linked list which stores the address of the header node in the next
field of the last node. Here, the header node will denote the end of the list.
As in other linked lists, if START = NULL, then this denotes an empty header
linked list. Let us see how a grounded header linked list is stored in the memory. In
a grounded header linked list, a node has two fields, DATA and NEXT. The
DATA field will store the information part and the NEXT field will store the
address of the node in sequence
Note that START stores the address of the header node. The shaded row denotes a
header node. The NEXT field of the header node stores the address of the first
node of the list. This node stores H. The corresponding NEXT field stores the
address of the next node, which is 3. So, we will look at address 3 to fetch the next
data item.
Hence, we see that the first node can be accessed by writing FIRST_NODE =
START -> NEXT and not by writing START = FIRST_ NODE. This is because
START points to the header node and the header node points to the first node of
the header linked list.