Lecture 09
Circular Linked List
Mr. Mubashir Ali
Lecturer (Dept. of Software Engineering)
[email protected]Lahore Garrison University, Lahore
1
Previous Lecture
1. Delete to Head in Doubly Linked List
2. Delete to Tail
3. Delete to Specific Position
4. Searching
5. Traversing
6. Lab-4
Mubashir Ali - Lecturer (Department of
2
Software Engineering)
Outline
1. Circular Linked List
2. Advantages of Circular Linked List
3. Applications of Circular Linked List
4. Operations on Circular Linked List
5. Add to Head
6. Add to Tail
7. Add to Specific Position
8. Lab 5.1
9. 2nd Submission
Mubashir Ali - Lecturer (Department of
3
Software Engineering)
Circular Linked List
Circular linked list is a linked list where all nodes
are connected to form a circle. There is no NULL
at the end. A circular linked list can be a singly
circular linked list or doubly circular linked list.
Mubashir Ali - Lecturer (Department of
4
Software Engineering)
Advantages of Circular Linked List
• Any node can be a starting point. We can traverse
the whole list by starting from any point. We just
need to stop when the first visited node is visited
again.
• Useful for implementation of queue.
Unlike this implementation, we don’t need to
maintain two pointers for front and rear if we use
circular linked list. We can maintain a pointer to the
last inserted node and front can always be obtained
as next of last.
Mubashir Ali - Lecturer (Department of
5
Software Engineering)
Applications of Circular Linked List
• The real life application where the circular linked list
is used is our Personal Computers, where multiple
applications are running. All the running applications
are kept in a circular linked list and the OS gives a
fixed time slot to all for running. The Operating
System keeps on iterating over the linked list until all
the applications are completed.
• Another example can be Multiplayer games. All the
Players are kept in a Circular Linked List and the
pointer keeps on moving forward as a player's
chance ends.
Mubashir Ali - Lecturer (Department of
6
Software Engineering)
Applications of Circular Linked List
• Circular Linked List can also be used to create Circular
Queue. In a Queue we have to keep two pointers,
FRONT and REAR in memory all the time, where as in
Circular Linked List, only one pointer is required.
• Circular Doubly Linked Lists are used for
implementation of advanced data structures
like Fibonacci Heap.
Mubashir Ali - Lecturer (Department of
7
Software Engineering)
Operations on Doubly Linked List
Operation Description
Insertion at Adding the node into the linked list at
beginning beginning.
Insertion at end Adding the node into the linked list to the
end.
Insertion after Adding the node into the linked list after the
specified node specified node.
Deletion at Removing the node from beginning of the list
beginning
Deletion at the Removing the node from end of the list.
end
Mubashir Ali - Lecturer (Department of
8
Software Engineering)
Operations on Doubly Linked List
Operation Description
Deletion of the Removing the node which is present just after
node having given the node containing the given data.
data
Searching Comparing each node data with the item to
be searched and return the location of the
item in the list if the item found else return
null.
Traversing Visiting each node of the list at least once in
order to perform some specific operation like
searching, sorting, display, etc. or as many
operations as you want.
Mubashir Ali - Lecturer (Department of
9
Software Engineering)
Add to Head
Insertion in an empty List:
Initially when the list is empty, last pointer will be NULL.
Mubashir Ali - Lecturer (Department of
10
Software Engineering)
Add to Head
Insertion at the beginning of the list:
To Insert a node at the beginning of the list, follow these
step:
1. Create a node, say T.
2. Make T -> next = last -> next.
3. last -> next = T.
Mubashir Ali - Lecturer (Department of
11
Software Engineering)
Add to Head
Mubashir Ali - Lecturer (Department of
12
Software Engineering)
Add to Tail
Insertion at the end of the list:
To Insert a node at the end of the list, follow these step:
1. Create a node, say T.
2. Make T -> next = last -> next;
3. last -> next = T.
4. last = T.
Mubashir Ali - Lecturer (Department of
13
Software Engineering)
Add to Tail
Mubashir Ali - Lecturer (Department of
14
Software Engineering)
Add to Specific Position
Insertion in between the nodes:
To Insert a node at the end of the list, follow these step:
1. Create a node, say T.
2. Search the node after which T need to be insert, say
that node be P.
3. Make T -> next = P -> next;
4. P -> next = T.
Suppose 12 need to be insert after node having value
10
Mubashir Ali - Lecturer (Department of
15
Software Engineering)
Add to Specific Position
Mubashir Ali - Lecturer (Department of
16
Software Engineering)
Implementation
Mubashir Ali - Lecturer (Department of
17
Software Engineering)
Implementation
Mubashir Ali - Lecturer (Department of
18
Software Engineering)
Implementation
Mubashir Ali - Lecturer (Department of
19
Software Engineering)
Implementation
Mubashir Ali - Lecturer (Department of
20
Software Engineering)
Implementation
Mubashir Ali - Lecturer (Department of
21
Software Engineering)
Implementation
Mubashir Ali - Lecturer (Department of
22
Software Engineering)
Implementation
Mubashir Ali - Lecturer (Department of
23
Software Engineering)
Implementation
Mubashir Ali - Lecturer (Department of
24
Software Engineering)
Implementation
Mubashir Ali - Lecturer (Department of
25
Software Engineering)
Lab-5.1
Write C++ program to perform dynamic
implementation of Circular Linked List(Singly
and Doubly). Write function for add data to
head, tail and specific position.
Mubashir Ali - Lecturer (Department of
26
Software Engineering)
2nd Submission
Submit “Title and Abstract & Introduction” of your
semester research project at TURNITIN within
deadline, decided in class.
Note: Submission with ambiguous topics or containing
plagiarism more than 15% will be rejected.
Mubashir Ali - Lecturer (Department of
27
Software Engineering)
Summary
Circular Linked List
Advantages of Circular Linked List
Applications of Circular Linked List
Operations on Circular Linked List
Add to Head
Add to Tail
Add to Specific Position
Lab 5.1
2nd Submission
Mubashir Ali - Lecturer (Department of
28
Software Engineering)
References
you will be able to find course resources at
http://www.mubashirali.com/data-structures-algorithms/
Topic 3.3 (Data Structures and Algorithms in C++ by Adam
Drozdek)
Mubashir Ali - Lecturer (Department of
29
Software Engineering)