Linked List
Linked List
Introduction
inked list is a linear data structure that includes a series of connected nodes.
It is a dynamic data structure.
For every data item in a linked list, there is an associated pointer that gives the
memory location of the next data item in the linked list.
The data items in the linked list are not in consecutive memory locations. They may
be anywhere, but the accessing of these data items is easier as each data item
contains the address of the next data item.
Linked list can be defined as the nodes that are randomly stored in the memory.
A node in the linked list contains two parts, i.e., first is the data part and second is
the address part.
The last node of the list contains a pointer to the null.
After array, linked list is the second most used data structure. In a linked list, every
link contains a connection to another link.
Linked List is a Series of Nodes, where each node Consist of two Parts viz Data Part &
Pointer Part, the Pointer Part stores the address of the next node
Example
In above figure the Linked List consists of three nodes. First Node1 has two-part one
data part which consists of the 5 as data and the second part which contain the
address of the next node. The last node3 has the address part null showing that no
more data in the list.
Linked List Vs Arrays
Linked lists and arrays are similar since they both store linear collections of data.
Array is the most common data structure used to store collections of elements. Arrays
are convenient to declare and provide the easy syntax to access any element by its index
number. Once the array is set up, access to any element is convenient and fast.
Linked list can be represented as the connection of nodes in which each node points to the
next node of the list. The representation of the linked list is shown below -
Types of Linked list
Basically, linked list is classified into the following types:
1. Single Linked List.
2. Double Linked List.
3. Circular Linked List
Singly-linked list - Singly linked list can be defined as the collection of an ordered set of
elements. A node in the singly linked list consists of two parts: data part and link part. Data
part of the node stores actual information that is to be represented by the node, while the
link part of the node stores the address of its immediate successor. These types of lists are
often referred to as linear linked list.
Doubly linked list - Doubly linked list is a complex type of linked list in which a node contains
a pointer to the previous as well as the next node in the sequence. Therefore, in a doubly-
linked list, a node consists of three parts: node data, pointer to the next node in sequence
(next pointer), and pointer to the previous node (previous pointer).
This helps to traverse in forward direction and backward direction.
Circular linked list - In a circular singly linked list, the last node of the list contains a pointer
to the first node of the list. We can have circular singly linked list as well as circular doubly
linked list.
Applications of Linked list
The applications of the Linked list are given as follows -
Polynomials can be represented in linked list as well as can perform the operations
on the polynomial.
A linked list can be used to represent the sparse matrix.
The various operations like student's details, employee's details, or product details
can be implemented using the linked list as the linked list uses the structure data
type that can hold different data types.
Using linked list, we can implement stack, queue, tree, and graph data structures.
A linked list can be used to implement dynamic memory allocation. The dynamic
memory allocation is the memory allocation done at the run-time.
Operations performed on Linked list
The basic operations that are supported by a list are mentioned as follows –
NEW= AVAIL
AVAIL= LINK[AVAIL]
2. Fill Data into “Data Field“
INFO[NEW]=ITEM
3. Fill it’s “Link Field” as NULL or the address pointed by the START
(1) Algorithm to delete a node from the beginning of the Linked List
1. If START=NULL, then write “Underflow”
EXIT
2. LOC=START
3. START=LINK[START]
4. LINK[LOC]=AVAIL (add to avail list)
AVAIL=LOC
5. EXIT
else
LINK[SAVE]=LINK[LOC] (deletes node N)
AVAIL=LOC
3. EXIT
DOUBLY LINKED LIST OR TWO-WAY LIST
Doubly linked list is a complex type of linked list in which a node contains a pointer to
the previous as well as the next node in the sequence.
In a singly linked list, we could traverse only in one direction, because each node
contains address of the next node and it doesn't have any record of its previous nodes.
However, doubly linked list overcome this limitation of singly linked list.
In doubly linked list each node of the list contains the address of its previous node and
the next node. This helps to traverse in forward direction and backward direction.
In a doubly linked list, a node consists of three parts: node data, pointer to the next node
in sequence (next pointer) , pointer to the previous node (previous pointer).
Many applications require searching forward and backward thru nodes of a list. For
example, searching for a name in a telephone directory would need forward and
backward scanning thru a region of the whole list
A sample node in a doubly linked list is shown in the figure.
Two pointer variables can also use in the doubly linked list, namely FIRST –which points to
the first node in the list and LAST-which points to the last node in the list is also used
2. BACK[FORW[LOC]= BACK[LOC]
INSERTION OPERATION
1. Insertion in the middle of the doubly linked list
Algorithm to Insert a given ITEM between node A and B in LOCA and LOCB
1. FORW[LOCA]=NEW
2. FORW[NEW]=LOCB
3. BACK[LOCB]=NEW
4. BACK[NEW]=LOCA
Advantages:-
Circular lists are frequency used instead of ordinary linked list because in circular list all
nodes contain a valid address.
The important feature of circular list is as follows.
o In a circular list every node is accessible from a given node.
o Certain operations like concatenation and splitting becomes more efficient in
circular list.
Disadvantages:
1. IF START = NULL;
Write UNDERFLOW;
EXIT
2. PTR = START
3. Repeat Step 4 while LINK[PTR]! = START
4. PTR = LINK[PTR]
[END OF LOOP]
5. LINK[PTR] = LINK [START]
6. START = LINK [PTR]
7. EXIT