DSA-Ch4Linked-Lists
DSA-Ch4Linked-Lists
Chapter 4:
Linked List
Contents
4.1 Introduction
4.2 Representation of Linked List in memory
4.3 Traversing Linked List
4.4 Searching Linked List
4.5 Memory Allocation, Garbage Collection
4.6 Overflow & Underflow
4.7 malloc() & free()
4.8 Inserting in Linked List
4.9 Deletion in Linked List
4.1 Introduction
• “A linked list is a linear collection of data elements,
called node pointing to the next nodes by means of
pointers.”
• Each node is divided into two parts.
1. The first part contains the information of the
element.
2. The second part called the link field contains the
address of the next node in the list.
4.1 Introduction
• To see this more clearly lets look at an example:
4.1 Introduction
• The last node contains a null link
• The list may (or may not) have a header
• A node’s successor is the next node in the sequence
– The last node has no successor
• A node’s predecessor is the previous node in the
sequence
– The first node has no predecessor
• A list’s length is the number of elements in it
– A list may be empty (contain no elements)
4.1 Introduction
• The Head is a special pointer variable which contains
the address of the first node of the list.
• If there is no node available in the list then Head
contains NULL value that means, List is empty.
• The left part of the each node represents the
information part of the node
– which may contain an entire record of data (e.g. ID, name,
marks, age etc)
• The right part represents pointer/link to the next node
• The next pointer of the last node is null pointer signal the
end of the list.
Advantages
• List of data can be stored in arrays but linked
structures (pointers) provide several advantages.
• A linked list is appropriate when the number of
data elements to be represented in data
structure is unpredictable.
• It also appropriate when there are frequently
insertions & deletions occurred in the list.
• Linked lists are dynamic, so the length of a list can
increase or decrease as necessary.
Singly Linked Lists and Arrays
c a e d b
first
first
N
U
a b c d e L
L
9
10 B 8
11
12 E 0
Representation of Linked List in memory
INFO LINK
START
4 1 D 12
2
3
4 A 10
5
6
7
8 C 1
9
10 B 8
11
12 E 0
TYPES OF LINKED LISTS
1. Singly linked list (Linear linked list / One way list)
2. Doubly linked list (Two way list)
3. Circular linked list
4. Circular doubly linked list
1. Singly linked list
first
last
last
4-18
4. Circular doubly linked list
• A circular doubly linked list is one which has
both the successor pointer and predecessor
pointer in circular manner.
LL Implementation
Struct Node 200 100 300
200
{
int data; data 1 100 2 300 3 0
node* next;
}; next
// insert node n
n n n n
node* n;
node* t; t
t tt t t
node* h;
h
h
• Forward Traversing:
– traversing from the very first node towards the last node.
• Reverse Traversing:
– traversing from the very last node towards the first node.
Traversing Algorithm
Traversing a SLL (animation)
PTR
START
24
Searching:
• The process of finding a particular node of an
linked list is called Searching”.
• Two algorithms for searching:
– Unsorted linked list searching algorithm
– Sorted linked list searching algorithm
Sorted Linked List Searching Algorithm
20 16 12 5 3 1 x
1. delete pointer;
2. delete[] pointer;
• Back to Linked List
Insertion:
• This operation is used to insert a new node in the
linked list at the specified position.
A new node may be inserted :
• At the beginning of a linked list
• At the end of a linked list
• At the specified position in a linked list.
• If the list itself is empty, then the new node is
inserted as a first node.
Insertion at the beginning
Inserting at the beginning of the list
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the
single linked list...
•Step 1: Create a newNode with given value.
•Step 2: Check whether list is Empty (head == NULL)
•Step 3: If it is Empty then,
set newNode→next = NULL and head = newNode.
•Step 4: If it is Not Empty then,
set newNode→next = head and head = newNode.
Inserting at Given Position
Algorithm of Insertion at Given Position
Inserting At Specific location in the list (After a Node)
We can use the following steps to insert a new node after a node in the single
linked list...
•Step 1: Create a newNode with given value.
•Step 2: Check whether list is Empty (head == NULL)
•Step 3: If it is Empty then, set newNode →
next = NULL and head = newNode.
•Step 4: If it is Not Empty then, define a node pointer temp and initialize
with head.
•Step 5: Keep moving the temp to its next node until it reaches to the node after
which we want to insert the newNode (until temp1 → data is equal to location,
here location is the node value after which we want to insert the newNode).
•Step 6: Every time check whether temp is reached to last node or not. If it is
reached to last node then display 'Given node is not found in the list!!!
Insertion not possible!!!' and terminate the function. Otherwise move
the temp to next node.
•Step 7: Finally, Set 'newNode → next = temp → next' and 'temp →
next = newNode'
Insertion at Last of Linked List