SCTR’s Pune Institute of Computer Technology
Department of Computer Engineering
Laboratory Manual
Course: Data Structures Lab (DSL) Class: S.Y. B. Tech.
Assignment
04
No:
Consider a person creating a playlist of media/music files. The person wants
to store music_id, title, time_duration for every entry and wants the following
functionalities:
1. Able to insert a media file at any position dynamically.
2. Able to delete any entry from the list dynamically.
3. Able to sort on any field (music_id/title/time_duration)
Title/Problem 4. Able to play continuous looping fashion.
Statement:
OR
There is a student's club named 'Techno-Fun' club where Students can get
membership on request. Similarly one may cancel the membership of a club.
First node is reserved for the president of the club and the last node is
reserved for the secretary of the club. Choose a type of linked list to store
student PRN and Name. Write functions to: a) Add members b) show
members c) delete members, d) Compute total number of members e) Two
linked lists exist for two divisions. Concatenate two lists.
The students will learn…
Learning 1. To understand the Linked List as a Dynamic Data Structure.
Objectives: 2. To understand the various operations on Singly Circular Linked List.
3. To find the Time and Space Complexities of SCLL operations.
The students will be able…
Learning 1. To implement various operations on Singly Circular Linked List.
Outcomes: 2. To apply Singly Circular Linked List (SCLL) for continuous looping.
3. To analyze the Time and Space Complexities of SCLL operations.
OS/
Programming OS FOSS-Ubuntu20, Fedora32,33. Tools: Eclipse, VScode.
tools used:
A] Theory:
This assignment needs a class (Media) having music_id, music_title and music_duration as
Data Members and a *next to store the address of next node in the list. Along with insert_Songs(),
display_Songs(), delete_Songs(), count_Songs(), sort_asperDuration(), play_inLoop() Member
Functions as given below...
Class Media
{
int music_id; //………….. Data Members
char music_title[10];
int music_duration;
Media *next;
public:
void insert_Songs(); // Member Functions
void display_Songs();
void delete_Songs();
void count_Songs();
void sort_asperDuration();
void play_inLoop()
};
A] Singly Linked List (SLL):
A Singly Linked List (SLL) is a linear data structure consisting of nodes, where each node points
to the next one in the sequence. It's one of the most basic and foundational data structures.
Structure of a Node:
In a singly linked list, each node has two parts:
1. Data – stores the value.
2. Next – pointer to the next node.
Visual Representation:
For a list: [ABC , XYZ, PQR]
Head-→ [ABC |*next] → [XYZ |*next] → [PQR | *next] → NULL
Node Definition (C++ Example):
class Node
{
int data;
Node* next;
};
Core Operations:
1. Traversal
2. Insertion
At beginning
After any node
At end / append
3. Deletion
From beginning
From end
4. Count nodes
5. Sort nodes
6. Reverse list
7. Concatenate two lists
Advantages
Dynamic size (no pre-allocation needed like array).
Efficient insertions/deletions at the beginning.
Disadvantages
No backward traversal (unlike doubly linked lists).
Linear-time access for searching a node.
B] Singly Circular Linked List (SCLL):
Singly Circular Linked List (SCLL) is a variation of a singly linked list, where
the last node points back to the first node, forming a circular structure.
Key Characteristics:
Each node contains:
data — the value stored.
next — a reference to the next node.
The last node’s next pointer refers to the first node, not NULL
No node has a prev pointer (unlike doubly circular linked lists).
There is typically a head pointer to the first node.
Structure of a Node:
class Node
{
int data;
Node* next;
};
Concept Illustration:
For a list like: 10 -> 20 -> 30, in a circular way:
Head
|
[10 | *next] → [20 | *next] → [30 | *next]--->
↑ ↓
└────────────────────────┘
Operations:
1. Traversal
2. Insertion
At beginning
After any node
At end
3. Deletion
From beginning
Search by value and delete
From end
4. Count nodes
5. Sort nodes
6. display in loop
7. Reverse list
8. Concatenate two lists
Advantages
Efficient traversal with circular nature.
Useful in round-robin scheduling, circular queues, buffer systems.
Disadvantages
Slightly more complex than regular singly linked list.
Care needed to avoid infinite loops during traversal.
B] Algorithms/Pseudo-codes:
1. Create a Singly Circular Linked List
OR Insert Nodes in Singly Circular Linked List:
1. START
2. Define a Node structure with:
data (stores the value)
next (pointer to the next node)
3. Initialize head = NULL
4. For each element to insert:
Create a new node
Set newNode->data = value
If head == NULL:
Set newNode->next = newNode (self-pointing)
Set head = newNode
Else:
Traverse to the last node (temp->next == head)
Set temp->next = newNode
Set newNode->next = head (lastnode-pointing-head)
5. Repeat step 4 for all new nodes
6. STOP
2. Display Nodes of Singly Circular Linked List:
1. Start
2. Check if the head is NULL:
If yes, print "List is empty" and exit
3. Initialize a temporary pointer temp = head
4. Do the following:
Print temp->data
Move temp = temp->next
5. Repeat Step 4 until temp == head again (loop completed)
6. End
3. Delete Node of Singly Circular Linked List:
Specific cases:
I. Delete first node
II. Delete last node
III. Delete node by value
1. Start
2. If head == NULL:
Print "List is empty" and exit
3. If the node to delete is the head: (Case-I: Delete first node)
If there's only one node (head->next == head):
Delete head and set head = NULL
Else: (Case-II: Delete last node)
Traverse to the last node (whose next == head)
Update last->next = head->next
Delete head and update head = head->next
4. If the node is not the head: (Case-III: Delete node by value)
Initialize two pointers: prev = head, curr = head->next
Loop while curr != head:
If curr->data == value, break
Move prev = curr, curr = curr->next
If curr == head: Node not found → exit
Else:
Set prev->next = curr->next
Delete curr
5. End
4. Count Nodes of Singly Circular Linked List:
Start
If head == NULL:
Return 0 (list is empty)
Initialize a counter: count = 1
Set a temporary pointer: temp = head->next
While temp != head:
Increment count by 1
Move temp = temp->next
When loop ends, return count
End
5. Sort Nodes in Singly Circular Linked List: (using Bubble Sort)
1. Start
2. If head == NULL or list has only one node (head->next == head), return (already sorted)
3. Repeat until no swaps are needed: (for Passes)
Initialize a flag swapped = false
Set a pointer current = head
Do: (for Comparisons)
Compare current->data with current->next->data
If out of order, swap the data and set swapped = true
Move to next node: current = current->next
Repeat until current->next == head
2. If swapped == false, list is sorted → stop
3. End
6. Display nodes in loop of Singly Circular Linked List:
Repeatedly traversing and printing the list over and over again without
stopping — like an infinite loop.
Applications:
Simulating a real-time process (e.g., round-robin scheduling)
Demonstrating circular behavior
Creating interactive or animated displays
Algorithm:
1. Start
2. Check if head == NULL:
If true, print "List is empty" and exit
3. Set a pointer temp = head
4. Use a while(true) loop:
Print temp->data
Move temp = temp->next
If temp == head → One full cycle completed
Wait for input (to STOP Loop)
5. Repeat infinitely
6. End (never reached unless interrupted)
Conclusion: In this way the Singly Circular Linked List is implemented to insert and delete media
files dynamically from memory and to play songs in continuous looping manner as per the demand
of user or customer.
Review Questions:
1. What is Linked List and explain as a dynamic data structure?
2. When we need to use Circular Linked list for problem solving?
3. Explain the different operations on Linked List?
4. What are different types of Linked List ?
5. Explain Sorting in Linked List? How it works?
6. Give the Time complexity of various linked list operations.