Fifth Lecture
Fifth Lecture
Albaath university
Faculty of Mechanical and Electrical Engineering
Department of automatic control and computers
Department of Electronic and Communication
Fifth lecture
• In a circular Singly linked list, the last node of the list contains a
pointer to the first node of the list.
• We traverse a circular singly linked list until we reach the same node
where we started.
• The circular singly liked list has no beginning and no ending. There is
no null value present in the next part of any of the nodes.
• Circular linked list are mostly used in task maintenance in operating systems.
• There are many examples where circular linked list are being used in
computer science
• Circular data structures: Circular linked lists are suitable for representing data that
naturally loops or wraps around. For example, in a game, you might use a circular
linked list to manage a list of players where the last player points back to the first
player, forming a continuous loop.
• Iteration: Due to the circular nature, it is easier to iterate over all the elements in a
circular linked list compared to a traditional linked list. You can start at any node
and traverse the list until you reach the starting node again, without needing to
check for null values.
• Implementation of algorithms: Circular linked lists are used in various algorithms
and data structures. For instance, they are used in the implementation of circular
buffers or queues, where elements are continuously added and removed in a
circular manner.
• The last node of the list contains the address of the first node of the
list.
• Many operations
• Insertion.
• Deletion.
• Traversing.
• Searching.
• Insertion
• The insertion into a Circular Singly linked list can be performed at different
positions.
• Based on the position of the new node being inserted, the insertion is
categorized into the following categories.
• Insertion at the bigging of the linked list.
• Insertion at the end of the linked list.
• Algorithm
• Step 1: Start
• Step 2: If HEAD = NULL, Write UNDERFLOW, Goto Step 10.
• Step 3: If HEAD -> NEXT = HEAD, Set HEAD = NULL, Free HEAD, Goto
Step 10.
• Step 4: Set PTR = HEAD
• Step 5: Repeat Step 6 While PTR -> NEXT != HEAD
• Step 6: Set PTR = PTR -> NEXT
• Step 7: Set PTR -> NEXT = HEAD -> NEXT
• Step 8: Free HEAD
• Step 9: Set HEAD = PTR -> NEXT
• Step 10: End
• Algorithm
• Step 1: Start
• Step 2: If HEAD = NULL, Write UNDERFLOW, Goto Step 10.
• Step 3: If HEAD -> NEXT = HEAD, Set HEAD = NULL, Free HEAD, Goto
Step 10.
• Step 4: Set PTR = HEAD
• Step 5: Repeat Steps 6 And 7 While PTR -> NEXT != HEAD
• Step 6: Set PREPTR = PTR
• Step 7: Set PTR = PTR -> NEXT
• Step 8: Set PREPTR -> NEXT = HEAD
• Step 9: Free PTR
• Step 10: End
int main() {
void lastinsert(int item) LinkedList MyList;
{
// insert at the bagging of the list
struct Node *ptr = new Node; MyList.lastinsert(10);
struct Node *temp; MyList.lastinsert(20);
if(ptr == NULL) MyList.lastinsert(30);
{ cout << "the list after fill : ";
cout << "\nOVERFLOW\n"; MyList.PrintList();
}
else return 0;
{ }
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}} } };
Algorithms and data structures - First semester 2023-2024 31
Time complexity
• For the Traversal operation the worst case time complexity is O(n).
• For the Insertion operation in general the worst case time complexity
is O(n).
• For the Deletion operation the worst case time complexity is O(n).
• For the Search operation the worst case time complexity is O(n).