2. Powerpoint Templates
Page 2
c
What are Linked Lists
• A linked list is a linear
data structure.
• Nodes make up linked
lists.
• Nodes are structures
made up of data and a
pointer to another node.
• Usually the pointer is
called next.
4. Powerpoint Templates
Page 4
c
Singly Linked List
• Each node has only one link part
• Each link part contains the address of the
next node in the list
• Link part of the last node contains NULL
value which signifies the end of the node
5. Powerpoint Templates
Page 5
c
Here is a singly-linked list (SLL):
a b c d
myList
• Each node contains a value(data) and a
pointer to the next node in the list
• MyList is the header pointer which
points at the first node in the list
6. Powerpoint Templates
Page 6
d
Basic Operations on a list
• Creating a List
• Inserting an element in a list
• Deleting an element from a list
• Searching a list
• Reversing a list
7. Powerpoint Templates
Page 7
Inserting the node in a SLL
There are 3 cases here:-
Insertion at the beginning
Insertion at the end
Insertion after a particular node
8. Powerpoint Templates
Page 8
d
Insertion at the beginning
There are two steps to be followed:-
a) Make the next pointer of the node point towards
the first node of the list
b) Make the start pointer point towards this new
node
If the list is empty simply make the start pointer
point towards the new node;
11. Powerpoint Templates
Page 11
a
Inserting after an element
Here we again need to do 2 steps :-
Make the next pointer of the node to be inserted
point to the next node of the node after which
you want to insert the node
Make the next pointer of the node after which
the node is to be inserted, point to the node to
be inserted
13. Powerpoint Templates
Page 13
a
Deleting a node in SLL
Here also we have three cases:-
Deleting the first node
Deleting the last node
Deleting the intermediate node
14. Powerpoint Templates
Page 14
a
Deleting the first node
Here we apply 2 steps:-
Making the start pointer point towards the 2nd node
Deleting the first node using delete keyword
threetwoone
start
15. Powerpoint Templates
Page 15
a
Deleting the last node
Here we apply 2 steps:-
Making the second last node’s next pointer point
to NULL
Deleting the last node via delete keyword
node3node2node1
start
16. Powerpoint Templates
Page 16
a
Deleting a particular node
Here we make the next pointer of the node
previous to the node being deleted ,point to the
successor node of the node to be deleted and
then delete the node using delete keyword
node1 node2 node3
To be deleted
17. Powerpoint Templates
Page 17
d
Linked lists vs. Doubly linked lists
• Linked lists
– Simple implementation
– Require less memory
• Doubly linked lists
– More complex implementation
– Require more memory
– Accessing elements is easier, since accessing
from the “head” (i.e., leftmost element) costs as
much as accessing from the “tail” (i.e., rightmost
element)
20. Powerpoint Templates
Page 20
A node in a doubly linked list
stores two references:
- a next link, which points to the
next node in the list,
- a prev link, which points to the
previous node in the list.
21. Powerpoint Templates
Page 21
Such lists allow for :
a great variety of quick update
operations, including insertion and
removal at both ends, and in the
middle.
22. Powerpoint Templates
Page 22
Doubly linked lists
• A doubly linked list is a list that can be
traversed both from left to right and from right
to left
• It requires to store two head pointers:
– The first one points to the first element in the list
– The second one points to the last element in the
list
first
last
23. Powerpoint Templates
Page 23
Node definition
• Each node contains:
– The data
– A pointer to the previous element (left-side)
– A pointer to the next element (right-side)
previousPtr data nextPtr
24. Powerpoint Templates
Page 24
Structure of DLL
struct node
{
int data;
node*next;
node*previous; //holds the address of previous node
};
.Data .nextprevious.
inf
25. Powerpoint Templates
Page 25
NODE
A B
C
A doubly linked list contain three fields: an integer
value, the link to the next node, and the link to
the previous node.
previous data next
NULL 11 786
786200 400
200 656 400 786 777 NULL
27. Powerpoint Templates
Page 27
Doubly Linked Lists
Figure 3.14: A doubly linked list with sentinels, header and trailer,
marking the ends of the list. An empty list would have these sentinels
pointing to each other. We do not show the null prev pointer for the
header nor do we show the null next pointer for the trailer.
28. Powerpoint Templates
Page 28
Operation ID-Array Complexity Singly-linked list Complexity
Insert at beginning O(n) O(1)
Insert at end O(1) O(1) if the list has tail reference
O(n) if the list has no tail reference
Insert at middle O(n) O(n)
Delete at beginning O(n) O(1)
Delete at end O(1) O(n)
Delete at middle O(n):
O(1) access followed by O(n)
shift
O(n):
O(n) search, followed by O(1) delete
Search O(n) linear search
O(log n) Binary search
O(n)
Indexing: What is
the element at a
given position k?
O(1) O(n)
COMPLEXITY OF VARIOUS
OPERATIONS IN ARRAYS AND SLL
29. Deletion
Figure 3.15: Removing the node at the end of a a doubly linked list with
header and trailer sentinels: (a) before deleting at the tail; (b) deleting at
the tail; (c) after the deletion.