Assignment DSA
Assignment DSA
Section : V18
Question no 1:
Circular Music Playlist Implement a music playlist where the last song automatically
loops back to the first song. Node Attributes: Song Name, Artist, Duration (in
minutes)
Operations to Implement:
Insert at End: Add a new song at the end of the playlist. Insert at Front: Add a new
song at the beginning of the playlist.
Delete from End: Remove the last song from the playlist. Search: Find if a song
exists in the playlist. Update: Change the name of a song in the playlist.
Code :
#include <iostream>
#include <string>
struct Song {
string name;
string artist;
float duration;
Song* next;
};
class Playlist {
private:
Song* tail;
public:
Playlist() : tail(nullptr) {}
if (!tail) {
newSong->next = newSong;
tail = newSong;
} else {
newSong->next = tail->next;
tail->next = newSong;
tail = newSong;
if (!tail) {
newSong->next = newSong;
tail = newSong;
} else {
newSong->next = tail->next;
tail->next = newSong;
cout << songName << " added at the start of the playlist." << endl;
void removeFromEnd() {
if (!tail) {
return;
if (tail->next == tail) {
cout << tail->name << " removed from playlist." << endl;
delete tail;
tail = nullptr;
} else {
current = current->next;
current->next = tail->next;
cout << tail->name << " removed from playlist." << endl;
delete tail;
tail = current;
if (!tail) {
return;
do {
if (current->name == songName) {
cout << "Found: " << songName << " by " << current->artist << endl;
return;
current = current->next;
cout << songName << " not found in playlist." << endl;
if (!tail) {
return;
}
do {
if (current->name == oldName) {
current->name = newName;
cout << oldName << " updated to " << newName << endl;
return;
current = current->next;
cout << oldName << " not found in playlist." << endl;
void showPlaylist() {
if (!tail) {
return;
do {
cout << "Song: " << current->name << ", Artist: " << current->artist
<< ", Duration: " << current->duration << " mins" << endl;
current = current->next;
}
};
int main() {
Playlist playlist;
playlist.showPlaylist();
playlist.findSong("Afreen Afreen");
playlist.showPlaylist();
playlist.removeFromEnd();
playlist.showPlaylist();
return 0;
}
Output :
Question no 2:
Online Game Queue Design a queue system for an online multiplayer game where
players rejoin the queue after playing. Node Attributes: Player Name, Player ID,
Player Rank
Operations to Implement:
Delete from Beginning: Remove the first player after they enter the game.
Delete at Position: Remove a player from a specific position in the queue. Search:
Check if a specific player is in the queue.
Code :
#include <iostream>
#include <string>
struct Player {
string name;
int id;
int rank;
Player* next;
};
class Queue {
private:
Player* head;
Player* tail;
public:
if (!tail) {
} else {
tail->next = newPlayer;
tail = newPlayer;
void removeFirst() {
if (!head) {
return;
head = head->next;
if (!head) {
tail = nullptr;
cout << temp->name << " removed from queue." << endl;
delete temp;
if (pos < 1) {
return;
}
Player* newPlayer = new Player{pname, pid, prank, nullptr};
if (pos == 1) {
newPlayer->next = head;
head = newPlayer;
if (!tail) {
tail = newPlayer;
cout << pname << " added at position 1." << endl;
return;
current = current->next;
if (!current) {
delete newPlayer;
return;
newPlayer->next = current->next;
current->next = newPlayer;
if (!newPlayer->next) {
tail = newPlayer;
cout << pname << " added at position " << pos << "." << endl;
}
return;
if (pos == 1) {
removeFirst();
return;
current = current->next;
if (!current->next) {
return;
current->next = temp->next;
if (!current->next) {
tail = current;
cout << temp->name << " removed from position " << pos << "." << endl;
delete temp;
while (current) {
if (current->id == pid) {
cout << "Found " << current->name << " in queue." << endl;
return true;
current = current->next;
return false;
void showQueue() {
if (!head) {
return;
while (current) {
cout << "Name: " << current->name << ", ID: " << current->id << ", Rank: " <<
current->rank << endl;
current = current->next;
};
int main() {
Queue q;
q.showQueue();
q.removeFirst();
q.showQueue();
cout << "Queue after inserting Zain at position 2:" << endl;
q.showQueue();
q.removeAt(3);
cout << "Queue after removing player at position 3:" << endl;
q.showQueue();
q.findPlayer(104);
return 0;
Output :
Question no 3:
Operations to Implement:
Insert at End: Add a new light signal to the sequence. Insert at Position: Insert a
signal at a specific sequence position.
Delete at Position: Remove a specific signal from the sequence. Update: Change
the duration of a light signal.
Code :
#include <iostream>
#include <string>
struct Signal {
string color;
int duration;
int intersectionID;
Signal* next;
};
class TrafficLightSystem {
private:
Signal* tail;
public:
TrafficLightSystem() : tail(nullptr) {}
if (!tail) {
newSignal->next = newSignal;
tail = newSignal;
} else {
newSignal->next = tail->next;
tail->next = newSignal;
tail = newSignal;
cout << "Signal " << color << " added to the sequence." << endl;
if (position < 1) {
return;
if (!tail) {
newSignal->next = newSignal;
tail = newSignal;
} else if (position == 1) {
newSignal->next = tail->next;
tail->next = newSignal;
} else {
current = current->next;
newSignal->next = current->next;
current->next = newSignal;
if (current == tail) {
tail = newSignal;
cout << "Signal " << color << " added at position " << position << "." << endl;
return;
if (position == 1) {
toDelete = tail->next;
if (tail->next == tail) {
tail = nullptr;
} else {
tail->next = toDelete->next;
} else {
current = current->next;
if (current->next == tail->next) {
return;
toDelete = current->next;
current->next = toDelete->next;
if (toDelete == tail) {
tail = current;
cout << "Signal " << toDelete->color << " deleted from position " << position <<
"." << endl;
delete toDelete;
return;
do {
if (current->color == color) {
current->duration = newDuration;
cout << "Duration of " << color << " updated to " << newDuration << "
seconds." << endl;
return;
current = current->next;
cout << "Signal " << color << " not found." << endl;
void displaySequence() {
if (!tail) {
return;
do {
cout << "Color: " << current->color
<< " seconds, Intersection ID: " << current->intersectionID << endl;
current = current->next;
};
int main() {
TrafficLightSystem trafficSystem;
trafficSystem.insertAtEnd("Yellow", 5, 1);
trafficSystem.displaySequence();
trafficSystem.displaySequence();
trafficSystem.updateDuration("Green", 20);
trafficSystem.displaySequence();
trafficSystem.deleteAtPosition(3);
trafficSystem.displaySequence();
return 0;
Output :
Question no 4:
Develop a system for managing print jobs where the last job loops back to the first
for fair distribution.Node Attributes: Job ID, File Name, Pages
Operations to Implement:
Update: Change the details of a specific job. Search: Find a specific print job in the
queue.
Code :
#include <iostream>
#include <string>
struct PrintJob {
int jobID;
string fileName;
int pages;
PrintJob* next;
};
class PrintJobScheduler {
private:
PrintJob* tail;
public:
PrintJobScheduler() : tail(nullptr) {}
void insertAtEnd(int jobID, string fileName, int pages) {
if (!tail) {
newJob->next = newJob;
tail = newJob;
} else {
newJob->next = tail->next;
tail->next = newJob;
tail = newJob;
cout << "Job " << jobID << " added to the queue." << endl;
if (position < 1) {
return;
if (!tail) {
newJob->next = newJob;
tail = newJob;
} else if (position == 1) {
newJob->next = tail->next;
tail->next = newJob;
} else {
current = current->next;
newJob->next = current->next;
current->next = newJob;
if (current == tail) {
tail = newJob;
cout << "Job " << jobID << " added at position " << position << "." << endl;
void deleteFromBeginning() {
if (!tail) {
return;
if (tail->next == tail) {
tail = nullptr;
} else {
tail->next = toDelete->next;
}
cout << "Job " << toDelete->jobID << " completed and removed from the
queue." << endl;
delete toDelete;
if (!tail) {
return;
do {
if (current->jobID == jobID) {
current->fileName = newFileName;
current->pages = newPages;
cout << "Job " << jobID << " updated." << endl;
return;
current = current->next;
cout << "Job " << jobID << " not found." << endl;
if (!tail) {
cout << "Queue is empty." << endl;
return;
do {
if (current->jobID == jobID) {
cout << "Job Found - ID: " << jobID << ", File: " << current->fileName
return;
current = current->next;
cout << "Job " << jobID << " not found." << endl;
void displayQueue() {
if (!tail) {
return;
do {
cout << "Job ID: " << current->jobID << ", File: " << current->fileName
};
int main() {
PrintJobScheduler scheduler;
scheduler.displayQueue();
scheduler.displayQueue();
scheduler.displayQueue();
scheduler.searchJob(3);
scheduler.deleteFromBeginning();
scheduler.displayQueue();
return 0;
Output :
Question no 5:
Circular Task Reminder Design a task reminder app where tasks repeat in a cycle.
Node Attributes: Task Name, Due Date, Priority Level
Operations to Implement:
Delete from End: Remove the last task from the reminder. Search: Find a specific
task in the cycle.
Insert at Position: Add a task to a specific position in the reminder. Delete at
Position: Remove a task from a specific position.
Code :
#include <iostream>
#include <string>
struct Node {
string taskName;
string dueDate;
int priorityLevel;
Node* next;
};
class CircularTaskReminder {
public:
Node* head;
CircularTaskReminder() {
head = nullptr;
}
void insertAtFront(string taskName, string dueDate, int priorityLevel) {
newNode->taskName = taskName;
newNode->dueDate = dueDate;
newNode->priorityLevel = priorityLevel;
if (head == nullptr) {
head = newNode;
newNode->next = head;
} else {
newNode->next = head;
last = last->next;
last->next = newNode;
head = newNode;
void deleteFromEnd() {
if (head == nullptr) {
return;
head = nullptr;
} else {
last = last->next;
delete last->next;
last->next = head;
if (head == nullptr) {
return;
do {
if (current->taskName == taskName) {
cout << "Task found: " << current->taskName << ", Due Date: " << current-
>dueDate << ", Priority: " << current->priorityLevel << endl;
found = true;
break;
current = current->next;
if (!found) {
void display() {
if (head == nullptr) {
return;
}
Node* current = head;
do {
cout << current->taskName << " - " << current->dueDate << " - " << current-
>priorityLevel << endl;
current = current->next;
};
int main() {
CircularTaskReminder reminder;
reminder.display();
reminder.deleteFromEnd();
reminder.display();
reminder.search("Task 2");
return 0;
}
Output :