1. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 1
1
Insert - General Case
In general case, new node is always inserted
between two nodes, which are already in the
list. Head and tail links are not updated in
this case.
We need to know two nodes "Previous" and
"Next", between which we want to insert the
new node.
This also can be done in two steps:
• Update link of the "previous" node, to point to the new
node.
• Update link of the new node, to point to the "next" node.
2. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 2
2
3. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 3
3
Singly-linked List - Deletion
There are four cases, which can occur while
removing the node.
We have the same four situations, but the
order of algorithm actions is opposite.
Notice, that removal algorithm includes the
disposal of the deleted node - unnecessary in
languages with automatic garbage collection
(Java).
4. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 4
4
List has only one node
When list has only
one node, that the
head points to the
same node as the
tail, the removal is
quite simple.
Algorithm disposes
the node, pointed
by head (or tail)
and sets both head
and tail to NULL.
5. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 5
5
Remove First
In this case, first node (current head node) is
removed from the list.
It can be done in two steps:
• Update head link to point to the node, next to the
head.
• Dispose removed node.
6. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 6
6
7. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 7
7
Remove Last
In this case, last node (current tail node) is
removed from the list. This operation is a bit
more tricky, than removing the first node,
because algorithm should find a node, which
is previous to the tail first.
It can be done in three steps:
• Update tail link to point to the node, before the
tail. In order to find it, list should be traversed
first, beginning from the head.
• Set next link of the new tail to NULL.
• Dispose removed node.
8. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 8
8
9. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 9
9
Remove - General Case
In general case, node to be removed is
always located between two list nodes. Head
and tail links are not updated in this case.
We need to know two nodes "Previous" and
"Next", of the node which we want to delete.
Such a removal can be done in two steps:
• Update next link of the previous node, to point to
the next node, relative to the removed node.
• Dispose removed node.
10. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 10
10
11. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 11
11
Advantages of Using Linked Lists
Need to know where the first node is
• the rest of the nodes can be accessed
No need to move the elements in the list
for insertion and deletion operations
No memory waste
12. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 12
12
Cursor Implementation
Problems with linked list implementation:
Same language do not support pointers!
• Then how can you use linked lists ?
new and free operations are slow
• Actually not constant time
SOLUTION: Implement linked list on an array -
called CURSOR
13. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 13
13
Cursor Implementation - Diagram
14. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 14
14
Cursor Implementation
If L = 5, then L represents list (A, B, E)
If M = 3, then M represents list (C, D, F)
15. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 15
15
Arrays - Pros and Cons
Pros
• Directly supported by C
• Provides random access
Cons
• Size determined at compile time
• Inserting and deleting elements is
time consuming
16. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 16
16
Linked Lists - Pros and Cons
Pros
• Size determined during runtime
• Inserting and deleting elements is
quick
Cons
• No random access
• User must provide programming
support
17. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 17
17
Application of Lists
Lists can be used
To store the records sequentially
For creation of stacks and queues
For polynomial handling
To maintain the sequence of operations
for do / undo in software
To keep track of the history of web sites
visited
18. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 18
18
Why Doubly Linked List ?
given only the pointer location, we cannot access its
predecessor in the list.
Another task that is difficult to perform on a linear
linked list is traversing the list in reverse.
Doubly linked list A linked list in which each node is
linked to both its successor and its predecessor
In such a case, where we need to access the node
that precedes a given node, a doubly linked list is
useful.
19. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 19
19
Doubly Linked List
In a doubly linked list, the nodes are linked
in both directions. Each node of a doubly
linked list contains three parts:
• Info: the data stored in the node
• Next: the pointer to the following node
• Back: the pointer to the preceding node
20. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 20
20
Operations on Doubly Linked Lists
The algorithms for the insertion and deletion
operations on a doubly linked list are
somewhat more complicated than the
corresponding operations on a singly linked
list.
The reason is clear: There are more pointers
to keep track of in a doubly linked list.
21. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 21
21
Inserting Item
As an example, consider the Inserting an
item.
To link the new node, after a given node, in
a singly linked list, we need to change two
pointers:
• newNode->next and
• location->next.
The same operation on a doubly linked list
requires four pointer changes.
22. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 22
22
Singly Linked List Insertion
23. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 23
23
Doubly Linked List Insertion
24. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 24
24
The Order is Important
25. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 25
25
Doubly Linked List - Deletion
One useful feature of a doubly linked list is
its elimination of the need for a pointer to a
node's predecessor to delete the node.
Through the back member, we can alter the
next member of the preceding node to make
it jump over the unwanted node.
Then we make the back pointer of the
succeeding node point to the preceding node.
26. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 26
26
Doubly Linked List - Deletion
27. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 27
27
Special Cases of Deletion
We do, however, have to be careful about
the end cases:
• If location->back is NULL, we are deleting the
first node
• if location->next is NULL, we are deleting the last
node.
• If both location->back and location->next are
NULL, we are deleting the only node.
28. April 16, 2025
April 16, 2025 Anna University, Chennai - 600 025
Anna University, Chennai - 600 025 28
28
Interaction