Linked List: Data Structure & Algorithms
Linked List: Data Structure & Algorithms
Linked List: Data Structure & Algorithms
Chapter 4:
Linked List
Imran Ali Memon
IT Department
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 M 8
11
12 N 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-17
4. Circular doubly linked list
• A circular doubly linked list is one which has
both the successor pointer and predecessor
pointer in circular manner.
Operations on Linked List:
• There are several operations associated with linked
list i.e.
1. Traversing a Linked List
2. Searching a Linked List:
3. Insertion into a Linked List:
4. Inserting a new node in list:
5. Delete a node from list:
Traversing a Linked List
• It is a process of going through all the nodes of a
linked list from one end to the other end.
PTR
START
22
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 Given Position
Algorithm of Insertion at Given Position
Insertion at Last of Linked List