Disadvantages of Array: 1) The Size of The Arrays Is Fixed: So We Must Know The Upper Limit
Disadvantages of Array: 1) The Size of The Arrays Is Fixed: So We Must Know The Upper Limit
Arrays can be used to store linear data of similar types, but arrays
have following limitations.
1) The size of the arrays is fixed: So we must know the upper limit
on the number of elements in advance. Also, generally, the
allocated memory is equal to the upper limit irrespective of the
usage.
2) Inserting a new element in an array of elements is expensive,
because room has to be created for the new elements and to create
room existing elements have to shifted.
Introduction
• A linked list is a linear collection of data elements called nodes in
which linear representation is given by links from one node to the
next node.
1 2 3 4 5 6 7 X
• In the above linked list, every node contains two parts - one
integer and the other a pointer to the next node.
• The left part of the node which contains data may include a simple
data type, an array or a structure.
• The right part of the node contains a pointer to the next node (or
address of the next node in sequence).
• The last node will have no next node connected to it, so it will
store a special value called NULL.
Traversing Linked Lists
• We can traverse the entire linked list using a single pointer
variable called START.
• The START node contains the address of the first node; the next
part of the first node in turn stores the address of its succeeding
node.
• Using this technique the individual nodes of the list will form a
chain of nodes.
• If START = NULL, this means that the linked list is empty and
contains no nodes.
• In C, we can implement a linked list using the following code:
struct node
{
int data;
struct node *next;
};
START Pointer
START
NEXT
1 DATA
1
H 4
2
START pointing to
the first element of 3
memory 5
AVAIL 6
9 7 L 8
L 10
8
9
10 O -1
Singly Linked Lists
• A singly linked list is the simplest type of linked list in which
every node contains some data and a pointer to the next node
of the same data type.
START
1 2 3 4 5 6 7 X
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
Inserting a Node at the Beginning
1 7 3 4 2 6 5 X
START
9 1 7 3 4 2 6 5 X
START
START, PTR
1 7 3 4 2 6 5 9 X
START
ALGORITHM TO INSERT A NEW NODE AFTER A NODE THAT HAS VALUE NUM
1 7 3 4 2 6 5 X
START
7 3 4 2 6 5 X
START
Deleting the Last Node
ALGORITHM TO DELETE THE LAST NODE OF THE LINKED LIST
1 7 3 4 2 6 5 X
1 7 3 4 2 6 X 5 X
PREPTR PTR
START
Deleting the Node After a Given Node
ALGORITHM TO DELETE THE NODE AFTER A GIVEN NODE FROM THE LINKED LIST
1 7 3 4 2 6 5 X
1 7 3 4 2 6 5 X
START
1 7 3 4 6 5 X
Circular Linked List
• In a circular linked list, the last node contains a pointer to the first
node of the list. We can have a circular singly listed list as well as
circular doubly linked list. While traversing a circular linked 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 had
started. Thus, a circular linked list has no beginning and no
ending.
START
1 2 3 4 5 6 7
Circular Linked List
Algorithm to insert a new node in the beginning of circular the linked list
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5
PTR
START
9 1 7 3 4 2 6 5
START
Circular Linked List
Algorithm to insert a new node at the end of the circular linked list
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5 9
START PTR
Circular Linked List
Algorithm to insert a new node after a node that has value NUM
Algorithm to delete the first node from the circular linked list
1 7 3 4 2 6 5
START, PTR
1 7 3 4 2 6 5
PTR
START
7 3 4 2 6 5
START
Circular Linked List
1 7 3 4 2 6 5
1 7 3 4 2 6 5
PREPTR
PTR
START
1 7 3 4 2 6
START
Circular Linked List
Algorithm to delete the node after a given node from the circular linked list
1 7 3 4 2 6 5
1 7 3 4 2 6 5
1 7 3 4 6 5
START
Doubly Linked List
A doubly linked list or a two way linked list is a more complex
type of linked list which contains a pointer to the next as well
as previous node in the sequence. Therefore, it consists of
three parts and not just two. The three parts are data, a
pointer to the next node and a pointer to the previous node
START
X 1 1 2 3 4 X
Doubly Linked List
• In C language, the structure of a doubly linked list is given as,
struct node
{ struct node *prev;
int data;
struct node *next;
};
• The prev field of the first node and the next field of the last
node will contain NULL. The prev field is used to store the
address of the preceding node. This would enable to traverse
the list in the backward direction as well.
Doubly Linked List
Algorithm to insert a new node in the beginning of the doubly linked list
X 1 7 3 4 2 X
START
2 X
X 9 1 7 3 4
START
Doubly Linked List
Algorithm to insert a new node at the end of the doubly linked list
X 1 7 3 4 2 X
START, PTR
X 1 7 3 4 2 9 X
PTR
Doubly Linked List
Algorithm to insert a new node after a node that has value NUM
X 1 7 3 4 2 X
START, PTR
X 1 7 3 4 2 X
START PTR
X 1 7 3 9 4 2 X
START
Doubly Linked List
Algorithm to delete the first node from the doubly linked list
X 1 7 3 4 2 X
START, PTR
7 3 4 2 X
Doubly Linked List
Algorithm to delete the last node of the doubly linked list
X 1 3 5 7 8 9 X
1
START, PTR
X 1 3 5 7 8 9 X
1
START PTR
X 1 3 5 7 8 X
START
Doubly Linked List
Algorithm to delete the node after a given node from the doubly linked list
X 1 3 4 7 8 9 X
1
START, PTR
X 1 3 4 7 8 9 X
1
START
PTR
X 1 3 4 8 9 X
START
Circular Doubly Linked List
• 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 previous node in the sequence.
START
1 1 2 3 4
Circular Doubly Linked List
1 7 3 4 2
START
9 1 7 3 4 2
START
Circular Doubly Linked List
Algorithm to insert a new node at the end of the circular doubly linked
list
1 7 3 4 2
START
1 7 3 4 2 9
START
Circular Doubly Linked List
Algorithm to insert a new node after a node that has value NUM
1 7 3 4 2
START, PTR
1 7 3 4 2
PTR
START 9
1 7 3 9 4 2
START
Circular Doubly Linked List
Algorithm to delete the first node from the circular doubly linked list
1 3 5 7 8 9
1
START, PTR
3 5 7 8 9
1
START
Circular Doubly Linked List
Algorithm to delete the last node of the circular doubly linked list
1 3 5 7 8 9
1
START PTR
X 1 3 5 7 8
START
Circular Doubly Linked List
Algorithm to delete the node after a given node from the circular doubly
linked list
1 3 5 7 8 9
1
START
1 3 5 7 8 9
1
START PTR
1 3 4 8 9
START
Header Linked List
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.
There are basically two variants of a header linked list-
Header Node
1 2 3 4 5 6 X
START
Header Node
1 2 3 4 5 6
START
Header Linked List