0% found this document useful (0 votes)
34 views

Fifth Lecture

Circular Singly Linked List definition • Memory Representation of circular linked list • Operations on Circular Singly linked list • Time complexity

Uploaded by

Rami Rami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Fifth Lecture

Circular Singly Linked List definition • Memory Representation of circular linked list • Operations on Circular Singly linked list • Time complexity

Uploaded by

Rami Rami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Syrian Arab Republic

Albaath university
Faculty of Mechanical and Electrical Engineering
Department of automatic control and computers
Department of Electronic and Communication

Algorithms and data structures

Fifth lecture

By: Dr. Hayyan Hasan

Algorithms and data structures - First semester 2023-2024 1


Outline

• Circular Singly Linked List definition


• Memory Representation of circular linked list
• Operations on Circular Singly linked list
• Time complexity.

Algorithms and data structures - First semester 2023-2024 2


Circular Singly Linked List definition

• 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.

Algorithms and data structures - First semester 2023-2024 3


Circular Singly Linked List definition Cont.

• 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.

Algorithms and data structures - First semester 2023-2024 4


Memory Representation of circular linked list

• The last node of the list contains the address of the first node of the
list.

Algorithms and data structures - First semester 2023-2024 5


Operations on Circular Singly linked list

• Many operations
• Insertion.
• Deletion.
• Traversing.
• Searching.

Algorithms and data structures - First semester 2023-2024 6


Operations on Circular Singly linked list Cont.

• 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.

Algorithms and data structures - First semester 2023-2024 7


Operations on Circular Singly linked list Cont.

• Insertion into circular singly linked list at beginning


• There are two scenario in which a node can be inserted in circular singly linked list at beginning.
Either the node will be inserted in an empty list or the node is to be inserted in an already filled list.
• Algorithm
• Step 1: Start
• Step 2: Create NEW_NODE = PTR
• Step 3: If PTR = NULL, Write OVERFLOW, Goto Step 12
• Step 4: Set PTR-> DATA = ITEM
• Step 5: If HEAD = NULL, Set PTR -> NEXT = HEAD , Set HEAD = PTR, Goto Step 12
• Step 6: Set TEMP = HEAD
• Step 7: Repeat Step 8 While TEMP -> NEXT != HEAD
• Step 8: Set TEMP = TEMP -> NEXT
• Step 9: Set TEMP - > NEXT = PTR
• Step 10: Set PTR -> NEXT = HEAD
• Step 11: Set HEAD = PTR
• Step 12: End

Algorithms and data structures - First semester 2023-2024 8


Operations on Circular Singly linked list Cont.

• Insertion into circular singly linked list at beginning

Algorithms and data structures - First semester 2023-2024 9


Operations on Circular Singly linked list Cont.
#include <iostream>
void beg_insert(int item)
using namespace std;
{
//node structure
struct Node *ptr = new Node;
struct Node
struct Node *temp;
{
if(ptr == NULL)
int data;
{
struct Node *next;
cout << "\nOVERFLOW";
};
}
class LinkedList {
else
private:
{
Node* head;
ptr -> data = item;
public:
LinkedList(){
head = NULL;
if(head == NULL)
}
{
//display the content of the list
head = ptr;
void PrintList() {
ptr -> next = head;
struct Node *ptr;
}
ptr=head;
else
if(head == NULL)
{
{
temp = head;
cout << "\nnothing to print";
while(temp->next != head)
} else {
temp = temp->next;
while(ptr -> next != head) {
ptr->next = head;
cout << ptr -> data << " ";
temp -> next = ptr;
ptr = ptr -> next; }
head = ptr;
cout << ptr -> data << " ";
}}} };
cout<<endl; }}
Algorithms and data structures - First semester 2023-2024 10
Operations on Circular Singly linked list Cont.

// test the code


int main() {
LinkedList MyList;
// insert at the bagging of the list
MyList.beg_insert(10);
cout << "the list after fill : ";
MyList.PrintList();
MyList.beg_insert(20);
cout << "the list after fill : ";
MyList.PrintList();
MyList.beg_insert(30);
cout << "the list after fill : ";
MyList.PrintList();
return 0;
}

Algorithms and data structures - First semester 2023-2024 11


Operations on Circular Singly linked list Cont.

• Insertion into circular singly linked list at the end


• There are two scenario in which a node can be inserted in circular singly linked list at the end. Either
the node will be inserted in an empty list or the node is to be inserted in an already filled list.
• Algorithm
• Step 1: Start
• Step 2: Create NEW_NODE = PTR
• Step 3: If PTR = NULL, Write OVERFLOW, Goto Step 11
• Step 4: Set PTR-> DATA = ITEM
• Step 5: If HEAD = NULL, Set PTR -> NEXT = HEAD , Set HEAD = PTR, Goto Step 11
• Step 6: Set TEMP = HEAD
• Step 7: Repeat Step 8 While TEMP -> NEXT != HEAD
• Step 8: Set TEMP = TEMP -> NEXT
• Step 9: Set PTR -> NEXT = HEAD
• Step 10: Set TEMP -> NEXT = PTR
• Step 11: End

Algorithms and data structures - First semester 2023-2024 12


Operations on Circular Singly linked list Cont.

• Insertion into circular singly linked list at the end

Algorithms and data structures - First semester 2023-2024 13


Operations on Circular Singly linked list Cont.
#include <iostream>
void lastinsert(int item)
using namespace std;
{
//node structure
struct Node
struct Node *ptr = new Node;
{
struct Node *temp;
int data;
if(ptr == NULL)
struct Node *next;
{
};
cout << "\nOVERFLOW\n";
class LinkedList {
}
private:
else
Node* head;
{
public:
ptr->data = item;
LinkedList(){
if(head == NULL)
head = NULL;
{
}
head = ptr;
//display the content of the list
ptr -> next = head;
void PrintList() {
}
struct Node *ptr;
else
ptr=head;
{
if(head == NULL)
temp = head;
{
while(temp -> next != head)
cout << "\nnothing to print";
{
} else {
temp = temp -> next;
while(ptr -> next != head) {
}
cout << ptr -> data << " ";
temp -> next = ptr;
ptr = ptr -> next; }
ptr -> next = head;
cout << ptr -> data << " ";
}} } };
cout<<endl; }}
Algorithms and data structures - First semester 2023-2024 14
Operations on Circular Singly linked list Cont.

// test the code


int main() {
LinkedList MyList;
// insert at the bagging of the list
MyList. lastinsert(10);
cout << "the list after fill : ";
MyList.PrintList();
MyList. lastinsert(20);
cout << "the list after fill : ";
MyList.PrintList();
MyList. lastinsert(30);
cout << "the list after fill : ";
MyList.PrintList();
return 0;
}

Algorithms and data structures - First semester 2023-2024 15


Operations on Circular Singly linked list Cont.

• Deletion in circular singly linked list at beginning


• In order to delete a node in circular singly linked list, we need to make a few
pointer adjustments.
• There are three scenarios of deleting a node from circular singly linked list at
beginning.
• Scenario 1: (The list is Empty)
• Scenario 2: (The list contains single node)
• Scenario 3: (The list contains more than one node)

Algorithms and data structures - First semester 2023-2024 16


Operations on Circular Singly linked list Cont.

• 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

Algorithms and data structures - First semester 2023-2024 17


Operations on Circular Singly linked list Cont.

• Deletion in circular singly linked list at beginning

Algorithms and data structures - First semester 2023-2024 18


Operations on Circular Singly linked list Cont.
#include <iostream> void beg_delete()
using namespace std; {
//node structure struct Node *ptr;
struct Node if(head == NULL)
{ {
int data; cout << "\nUNDERFLOW\n";
struct Node *next; }
}; else if(head->next == head)
class LinkedList { {
private: head = NULL;
Node* head; free(head);
public: cout << "\nNode Deleted\n";
LinkedList(){ }
head = NULL; else
} {
//display the content of the list ptr = head;
void PrintList() { while(ptr -> next != head)
struct Node *ptr; ptr = ptr -> next;
ptr=head; ptr->next = head->next;
if(head == NULL) free(head);
{ head = ptr->next;
cout << "\nnothing to print"; cout << "\nNode Deleted\n";
} else { }
while(ptr -> next != head) { }
cout << ptr -> data << " ";
ptr = ptr -> next; }
cout << ptr -> data << " ";
cout<<endl; }}
Algorithms and data structures - First semester 2023-2024 19
Operations on Circular Singly linked list Cont.
void lastinsert(int item)
{ // test the code
int main() {
struct Node *ptr = new Node; LinkedList MyList;
struct Node *temp; // insert at the bagging of the list
if(ptr == NULL) MyList.lastinsert(10);
{ MyList.lastinsert(20);
cout << "\nOVERFLOW\n"; MyList.lastinsert(30);
}
cout << "the list after fill : ";
else
MyList.PrintList();
{
ptr->data = item; MyList.beg_delete();
if(head == NULL) cout << "the list after deletion : ";
{ MyList.PrintList();
head = ptr;
ptr -> next = head;
} return 0;
else }
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}} } };

Algorithms and data structures - First semester 2023-2024 20


Operations on Circular Singly linked list Cont.

• Deletion in Circular singly linked list at the end


• There are three scenarios of deleting a node in circular singly linked list at the
end.
• Scenario 1 (the list is empty).
• Scenario 2(the list contains single element).
• Scenario 3(the list contains more than one element).

Algorithms and data structures - First semester 2023-2024 21


Operations on Circular Singly linked list Cont.

• 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

Algorithms and data structures - First semester 2023-2024 22


Operations on Circular Singly linked list Cont.

• Deletion in Circular singly linked list at the end

Algorithms and data structures - First semester 2023-2024 23


Operations on Circular Singly linked list Cont.
#include <iostream> void last_delete()
using namespace std; {
//node structure struct Node *ptr, *preptr;
struct Node if(head==NULL)
{ {
int data; cout << "\nUNDERFLOW\n";
struct Node *next; }
}; else if (head ->next == head)
class LinkedList { {
private: head = NULL;
Node* head; free(head);
public: cout << "\nNode Deleted\n";
LinkedList(){ }
head = NULL; else
} {
//display the content of the list ptr = head;
void PrintList() { while(ptr ->next != head)
struct Node *ptr; {
ptr=head; preptr=ptr;
if(head == NULL) ptr = ptr->next;
{ }
cout << "\nnothing to print"; preptr->next = head;
} else { free(ptr);
while(ptr -> next != head) { cout << "\nNode Deleted\n";
cout << ptr -> data << " "; }
ptr = ptr -> next; } }
cout << ptr -> data << " ";
cout<<endl; }}
Algorithms and data structures - First semester 2023-2024 24
Operations on Circular Singly linked list Cont.
void lastinsert(int item)
{ // test the code
int main() {
struct Node *ptr = new Node; LinkedList MyList;
struct Node *temp; // insert at the bagging of the list
if(ptr == NULL) MyList.lastinsert(10);
{ MyList.lastinsert(20);
cout << "\nOVERFLOW\n"; MyList.lastinsert(30);
}
cout << "the list after fill : ";
else
MyList.PrintList();
{
ptr->data = item; MyList.last_delete();
if(head == NULL) cout << "the list after deletion : ";
{ MyList.PrintList();
head = ptr;
ptr -> next = head;
} return 0;
else }
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}} } };

Algorithms and data structures - First semester 2023-2024 25


Operations on Circular Singly linked list Cont.

• Searching in circular singly linked list


• Searching in circular singly linked list needs traversing across the list. The item which is to be
searched in the list is matched with each node data of the list once and if the match found then
the location of that item is returned otherwise -1 is returned.
• Algorithm
• Step 1: Start.
• Step 2: Set PTR = HEAD
• Step 3: Set I = 0
• Step 4: If PTR = NULL, Write "EMPTY LIST”, Goto Step 10.
• Step 5: Repeat Step 6 To 8 Until PTR->NEXT != HEAD.
• Step 6: If PTR -> DATA = ITEM Return I, Goto Step 10.
• Step 7: Set I = I + 1
• Step 8: Set PTR = PTR -> NEXT
• Step 9: If PTR -> DATA = ITEM Return I, Goto Step 10.
• Step 10: End

Algorithms and data structures - First semester 2023-2024 26


Operations on Circular Singly linked list Cont.
void search()
#include <iostream> { struct Node *ptr;
using namespace std; int item,i=0,flag=1;
//node structure ptr = head;
struct Node if(ptr == NULL) {
{ cout << "\nEmpty List\n";
int data; } else {
struct Node *next; cout << "\nEnter item which you want to search?\n";
}; cin >> item;
class LinkedList { while (ptr->next != head) {
private: if(ptr->data == item) {
Node* head; cout << "item found at location "<< i;
public: flag=0;
LinkedList(){ return;
head = NULL; } else {
} flag=1;
//display the content of the list }
void PrintList() { i++;
struct Node *ptr; ptr = ptr -> next;
ptr=head; }
if(head == NULL) if(ptr->data == item) {
{ cout << "item found at location "<< i;
cout << "\nnothing to print"; flag=0;
} else { return;
while(ptr -> next != head) { }}
cout << ptr -> data << " "; if(flag != 0) {
ptr = ptr -> next; } cout << "Item not found\n";
cout << ptr -> data << " "; return;
cout<<endl; }} } }
Algorithms and data structures - First semester 2023-2024 27
Operations on Circular Singly linked list Cont.
void lastinsert(int item)
{ int main() {
LinkedList MyList;
struct Node *ptr = new Node; // insert at the bagging of the list
struct Node *temp; MyList.lastinsert(10);
if(ptr == NULL) MyList.lastinsert(20);
{ MyList.lastinsert(30);
cout << "\nOVERFLOW\n"; cout << "the list after fill : ";
}
MyList.PrintList();
else
MyList.search();
{
ptr->data = item;
if(head == NULL) return 0;
{ }
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 28


Operations on Circular Singly linked list Cont.

• Traversing in Circular Singly linked list


• Traversing in circular singly linked list can be done through a loop. Initialize
the temporary pointer variable ptr to head pointer and run the while loop until
the next pointer of temp becomes head.
• Algorithm
• Step 1: Start
• Step 2: If HEAD = NULL, Write "EMPTY LIST”, Goto Step 8.
• Step 3: Set PTR = HEAD
• Step 4: Repeat Step 5 and 6 While PTR -> NEXT != HEAD
• Step 5: Print PTR -> DATA
• Step 6: Set PTR = PTR -> NEXT
• Step 7: Print PTR- > DATA
• Step 8: End

Algorithms and data structures - First semester 2023-2024 29


Operations on Circular Singly linked list Cont.
#include <iostream>
using namespace std; //display the content of the list
//node structure void PrintList() {
struct Node struct Node *ptr;
{ ptr=head;
int data; if(head == NULL)
struct Node *next; {
}; cout << "\nnothing to print";
class LinkedList { } else {
private:
while(ptr -> next != head) {
Node* head;
public:
cout << ptr -> data << " ";
LinkedList(){ ptr = ptr -> next; }
head = NULL; cout << ptr -> data << " ";
} cout<<endl; }}

Algorithms and data structures - First semester 2023-2024 30


Operations on Circular Singly linked list Cont.

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).

Algorithms and data structures - First semester 2023-2024 32

You might also like