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

Assignment DSA

Uploaded by

2n6dhkjm6p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment DSA

Uploaded by

2n6dhkjm6p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

University of Management and Technology Lahore

Name: Hamza Iqbal

Roll number : f2023266765

Section : V18

Course Instructor: BILAL HUSSAIN

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>

using namespace std;

struct Song {

string name;

string artist;

float duration;

Song* next;
};

class Playlist {

private:

Song* tail;

public:

Playlist() : tail(nullptr) {}

void addAtEnd(string songName, string songArtist, float songDuration) {

Song* newSong = new Song{songName, songArtist, songDuration, nullptr};

if (!tail) {

newSong->next = newSong;

tail = newSong;

} else {

newSong->next = tail->next;

tail->next = newSong;

tail = newSong;

cout << songName << " added to playlist." << endl;

void addAtFront(string songName, string songArtist, float songDuration) {

Song* newSong = new Song{songName, songArtist, songDuration, nullptr};

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

cout << "Playlist is empty, nothing to remove." << endl;

return;

if (tail->next == tail) {

cout << tail->name << " removed from playlist." << endl;

delete tail;

tail = nullptr;

} else {

Song* current = tail->next;

while (current->next != tail) {

current = current->next;

current->next = tail->next;

cout << tail->name << " removed from playlist." << endl;
delete tail;

tail = current;

void findSong(string songName) {

if (!tail) {

cout << "Playlist is empty." << endl;

return;

Song* current = tail->next;

do {

if (current->name == songName) {

cout << "Found: " << songName << " by " << current->artist << endl;

return;

current = current->next;

} while (current != tail->next);

cout << songName << " not found in playlist." << endl;

void changeSongName(string oldName, string newName) {

if (!tail) {

cout << "Playlist is empty." << endl;

return;
}

Song* current = tail->next;

do {

if (current->name == oldName) {

current->name = newName;

cout << oldName << " updated to " << newName << endl;

return;

current = current->next;

} while (current != tail->next);

cout << oldName << " not found in playlist." << endl;

void showPlaylist() {

if (!tail) {

cout << "Playlist is empty." << endl;

return;

Song* current = tail->next;

do {

cout << "Song: " << current->name << ", Artist: " << current->artist

<< ", Duration: " << current->duration << " mins" << endl;

current = current->next;

} while (current != tail->next);

}
};

int main() {

Playlist playlist;

playlist.addAtEnd("Pasoori", "Shae Gill", 4.3);

playlist.addAtEnd("Afreen Afreen", "Rahat Fateh Ali Khan", 5.2);

playlist.addAtFront("Dil Dil Pakistan", "Vital Signs", 4.5);

cout << "Current Playlist:" << endl;

playlist.showPlaylist();

playlist.findSong("Afreen Afreen");

playlist.changeSongName("Pasoori", "Pasoori Remix");

cout << "Playlist After Update:" << endl;

playlist.showPlaylist();

playlist.removeFromEnd();

cout << "Final Playlist:" << endl;

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:

Insert at End: Add a new player to the end of the queue.

Delete from Beginning: Remove the first player after they enter the game.

Insert at Position: Add a player to a specific position in the queue.

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>

using namespace std;

struct Player {

string name;

int id;

int rank;

Player* next;

};

class Queue {

private:

Player* head;

Player* tail;

public:

Queue() : head(nullptr), tail(nullptr) {}

void addPlayer(string pname, int pid, int prank) {

Player* newPlayer = new Player{pname, pid, prank, nullptr};

if (!tail) {

head = tail = newPlayer;

} else {
tail->next = newPlayer;

tail = newPlayer;

cout << pname << " added to queue." << endl;

void removeFirst() {

if (!head) {

cout << "Queue is empty, nothing to remove." << endl;

return;

Player* temp = head;

head = head->next;

if (!head) {

tail = nullptr;

cout << temp->name << " removed from queue." << endl;

delete temp;

void insertAt(string pname, int pid, int prank, int pos) {

if (pos < 1) {

cout << "Invalid position." << endl;

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;

Player* current = head;

for (int i = 1; current && i < pos - 1; i++) {

current = current->next;

if (!current) {

cout << "Position too large." << endl;

delete newPlayer;

return;

newPlayer->next = current->next;

current->next = newPlayer;

if (!newPlayer->next) {

tail = newPlayer;

cout << pname << " added at position " << pos << "." << endl;
}

void removeAt(int pos) {

if (pos < 1 || !head) {

cout << "Invalid position or queue is empty." << endl;

return;

if (pos == 1) {

removeFirst();

return;

Player* current = head;

for (int i = 1; current->next && i < pos - 1; i++) {

current = current->next;

if (!current->next) {

cout << "Position too large." << endl;

return;

Player* temp = current->next;

current->next = temp->next;

if (!current->next) {

tail = current;

cout << temp->name << " removed from position " << pos << "." << endl;
delete temp;

bool findPlayer(int pid) {

Player* current = head;

while (current) {

if (current->id == pid) {

cout << "Found " << current->name << " in queue." << endl;

return true;

current = current->next;

cout << "Player not found in queue." << endl;

return false;

void showQueue() {

if (!head) {

cout << "Queue is empty." << endl;

return;

Player* current = head;

while (current) {

cout << "Name: " << current->name << ", ID: " << current->id << ", Rank: " <<
current->rank << endl;
current = current->next;

};

int main() {

Queue q;

q.addPlayer("Ali", 101, 5);

q.addPlayer("Sara", 102, 3);

q.addPlayer("Ahmed", 103, 2);

cout << "Current Queue:" << endl;

q.showQueue();

q.removeFirst();

cout << "Queue after removing first player:" << endl;

q.showQueue();

q.insertAt("Zain", 104, 4, 2);

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:

Traffic Light Control System Create a system to manage traffic lights at an


intersection that cycles through red, yellow, and green in a loop.
Node Attributes: Signal Color, Signal Duration, Intersection ID

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.

Display: Show the current sequence of light signals.

Code :

#include <iostream>

#include <string>

using namespace std;

struct Signal {

string color;

int duration;

int intersectionID;

Signal* next;

};

class TrafficLightSystem {

private:

Signal* tail;

public:
TrafficLightSystem() : tail(nullptr) {}

void insertAtEnd(string color, int duration, int intersectionID) {

Signal* newSignal = new Signal{color, duration, intersectionID, 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;

void insertAtPosition(string color, int duration, int intersectionID, int position) {

if (position < 1) {

cout << "Invalid position." << endl;

return;

Signal* newSignal = new Signal{color, duration, intersectionID, nullptr};

if (!tail) {

newSignal->next = newSignal;

tail = newSignal;

} else if (position == 1) {
newSignal->next = tail->next;

tail->next = newSignal;

} else {

Signal* current = tail->next;

for (int i = 1; current != tail && i < position - 1; ++i) {

current = current->next;

newSignal->next = current->next;

current->next = newSignal;

if (current == tail) {

tail = newSignal;

cout << "Signal " << color << " added at position " << position << "." << endl;

void deleteAtPosition(int position) {

if (!tail || position < 1) {

cout << "Invalid position or sequence is empty." << endl;

return;

Signal* toDelete = nullptr;

if (position == 1) {

toDelete = tail->next;

if (tail->next == tail) {
tail = nullptr;

} else {

tail->next = toDelete->next;

} else {

Signal* current = tail->next;

for (int i = 1; current != tail && i < position - 1; ++i) {

current = current->next;

if (current->next == tail->next) {

cout << "Position out of range." << endl;

return;

toDelete = current->next;

current->next = toDelete->next;

if (toDelete == tail) {

tail = current;

cout << "Signal " << toDelete->color << " deleted from position " << position <<
"." << endl;

delete toDelete;

void updateDuration(string color, int newDuration) {


if (!tail) {

cout << "Sequence is empty." << endl;

return;

Signal* current = tail->next;

do {

if (current->color == color) {

current->duration = newDuration;

cout << "Duration of " << color << " updated to " << newDuration << "
seconds." << endl;

return;

current = current->next;

} while (current != tail->next);

cout << "Signal " << color << " not found." << endl;

void displaySequence() {

if (!tail) {

cout << "Sequence is empty." << endl;

return;

Signal* current = tail->next;

cout << "Current Traffic Light Sequence:" << endl;

do {
cout << "Color: " << current->color

<< ", Duration: " << current->duration

<< " seconds, Intersection ID: " << current->intersectionID << endl;

current = current->next;

} while (current != tail->next);

};

int main() {

TrafficLightSystem trafficSystem;

trafficSystem.insertAtEnd("Red", 30, 1);

trafficSystem.insertAtEnd("Yellow", 5, 1);

trafficSystem.insertAtEnd("Green", 25, 1);

trafficSystem.displaySequence();

trafficSystem.insertAtPosition("Flashing Yellow", 3, 1, 2);

trafficSystem.displaySequence();

trafficSystem.updateDuration("Green", 20);

trafficSystem.displaySequence();
trafficSystem.deleteAtPosition(3);

trafficSystem.displaySequence();

return 0;

Output :

Question no 4:

Printer Job Scheduler

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:

Insert at End: Add a new print job to the queue.


Delete from Beginning: Remove a completed print job from the queue.

Insert at Position: Add a high-priority job to a specific position.

Update: Change the details of a specific job. Search: Find a specific print job in the
queue.

Code :

#include <iostream>

#include <string>

using namespace std;

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

PrintJob* newJob = new PrintJob{jobID, fileName, pages, nullptr};

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;

void insertAtPosition(int jobID, string fileName, int pages, int position) {

if (position < 1) {

cout << "Invalid position." << endl;

return;

PrintJob* newJob = new PrintJob{jobID, fileName, pages, nullptr};

if (!tail) {

newJob->next = newJob;

tail = newJob;

} else if (position == 1) {

newJob->next = tail->next;

tail->next = newJob;
} else {

PrintJob* current = tail->next;

for (int i = 1; current != tail && i < position - 1; ++i) {

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

cout << "Queue is empty, nothing to remove." << endl;

return;

PrintJob* toDelete = tail->next;

if (tail->next == tail) {

tail = nullptr;

} else {

tail->next = toDelete->next;

}
cout << "Job " << toDelete->jobID << " completed and removed from the
queue." << endl;

delete toDelete;

void updateJob(int jobID, string newFileName, int newPages) {

if (!tail) {

cout << "Queue is empty." << endl;

return;

PrintJob* current = tail->next;

do {

if (current->jobID == jobID) {

current->fileName = newFileName;

current->pages = newPages;

cout << "Job " << jobID << " updated." << endl;

return;

current = current->next;

} while (current != tail->next);

cout << "Job " << jobID << " not found." << endl;

void searchJob(int jobID) {

if (!tail) {
cout << "Queue is empty." << endl;

return;

PrintJob* current = tail->next;

do {

if (current->jobID == jobID) {

cout << "Job Found - ID: " << jobID << ", File: " << current->fileName

<< ", Pages: " << current->pages << endl;

return;

current = current->next;

} while (current != tail->next);

cout << "Job " << jobID << " not found." << endl;

void displayQueue() {

if (!tail) {

cout << "Queue is empty." << endl;

return;

PrintJob* current = tail->next;

cout << "Current Print Job Queue:" << endl;

do {

cout << "Job ID: " << current->jobID << ", File: " << current->fileName

<< ", Pages: " << current->pages << endl;


current = current->next;

} while (current != tail->next);

};

int main() {

PrintJobScheduler scheduler;

scheduler.insertAtEnd(1, "Document1.pdf", 10);

scheduler.insertAtEnd(2, "Document2.pdf", 5);

scheduler.insertAtEnd(3, "Presentation.pptx", 20);

scheduler.displayQueue();

scheduler.insertAtPosition(4, "UrgentReport.docx", 3, 2);

scheduler.displayQueue();

scheduler.updateJob(2, "Document2_Updated.pdf", 6);

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:

Insert at Front: Add a new high-priority task at the beginning.

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>

using namespace std;

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

Node* newNode = new Node;

newNode->taskName = taskName;

newNode->dueDate = dueDate;

newNode->priorityLevel = priorityLevel;

if (head == nullptr) {

head = newNode;

newNode->next = head;

} else {

newNode->next = head;

Node* last = head;

while (last->next != head) {

last = last->next;

last->next = newNode;

head = newNode;

void deleteFromEnd() {

if (head == nullptr) {

cout << "List is empty\n";

return;

} else if (head->next == head) {


delete head;

head = nullptr;

} else {

Node* last = head;

while (last->next->next != head) {

last = last->next;

delete last->next;

last->next = head;

void search(string taskName) {

if (head == nullptr) {

cout << "List is empty\n";

return;

Node* current = head;

bool found = false;

do {

if (current->taskName == taskName) {

cout << "Task found: " << current->taskName << ", Due Date: " << current-
>dueDate << ", Priority: " << current->priorityLevel << endl;

found = true;
break;

current = current->next;

} while (current != head);

if (!found) {

cout << "Task not found\n";

void insertAtPosition(string taskName, string dueDate, int priorityLevel, int


position) {

// ... (Implement this method similarly to insertAtFront)

void deleteAtPosition(int position) {

// ... (Implement this method similarly to deleteFromEnd)

void display() {

if (head == nullptr) {

cout << "List is empty\n";

return;

}
Node* current = head;

do {

cout << current->taskName << " - " << current->dueDate << " - " << current-
>priorityLevel << endl;

current = current->next;

} while (current != head);

};

int main() {

CircularTaskReminder reminder;

reminder.insertAtFront("Task 1", "04-08-2024", 2);

reminder.insertAtFront("Task 2", "12-10-2024", 1);

reminder.insertAtFront("Task 3", "16-11-2024", 3);

reminder.display();

reminder.deleteFromEnd();

reminder.display();

reminder.search("Task 2");

return 0;
}

Output :

You might also like