0% found this document useful (0 votes)
54 views12 pages

02 134231 013 119449103568 25042024 105823am

The document discusses a lab exercise on doubly linked lists. It includes code to implement a navigation application using a doubly linked list to store route steps and a task management system using a doubly linked list to manage tasks. Functions like adding, deleting, and traversing nodes in both forward and backward directions are demonstrated.

Uploaded by

andreytagno
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)
54 views12 pages

02 134231 013 119449103568 25042024 105823am

The document discusses a lab exercise on doubly linked lists. It includes code to implement a navigation application using a doubly linked list to store route steps and a task management system using a doubly linked list to manage tasks. Functions like adding, deleting, and traversing nodes in both forward and backward directions are demonstrated.

Uploaded by

andreytagno
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/ 12

CSL-221 Data structure &

Algorithm
Semester 03

Name: ALI RASHID


Class: BSCS-3B
Enrolment: 02-134231-013
Course: DATA STRUCTURES AND ALGORITHMS
Instructor: RABIA AMJAD
Lab 8: Doubly Linked List

Objective(s): Upon completion of this lab session, students will be able to:

Insert elements in a double link list


Traverse the double link list
Delete elements from double link list

Exercise 1: Navigation Application for Computers


Design a user-friendly navigation application to assist commuters traveling between their
home and Bahria University. The application must utilize a doubly linked list data
structure to store the sequence of directions for the route. Users should be able to add
new route steps, delete the entire route, and seamlessly navigate between home and
university. Clear and concise navigation instructions, including turning directions and
approximate distances, must be provided for each route step.

CODE:
#include <iostream>
#include <string>
using namespace std;

struct RouteStep {
string direction;
double distance;
RouteStep* next;
RouteStep* prev;
};

void addRouteStep(RouteStep*& head, RouteStep*& tail, string direction, double distance) {


RouteStep* newStep = new RouteStep;
newStep->direction = direction;
newStep->distance = distance;
CSL-221 Data structure &
Algorithm
Semester 03

newStep->next = nullptr;

if (head == nullptr) {
newStep->prev = nullptr;
head = newStep;
tail = newStep;
}
else {
newStep->prev = tail;
tail->next = newStep;
tail = newStep;
}
}

// Function to delete the entire route


void deleteRoute(RouteStep*& head, RouteStep*& tail) {
RouteStep* current = head;
while (current != nullptr) {
RouteStep* temp = current;
current = current->next;
delete temp;
}
head = nullptr;
tail = nullptr;
cout << "Route deleted successfully." << endl;
}

void navigateHomeToUniversity(RouteStep* head) {


RouteStep* current = head;
cout << "Navigation from Dalmiya to University:" << endl;
while (current != nullptr) {
cout << "Go " << current->direction << " for " << current->distance << " km." << endl;
current = current->next;
}
}

// Function to navigate the route from university to home


void navigateUniversityToHome(RouteStep* tail) {
RouteStep* current = tail;
cout << "Navigation from University to Dalmiya:" << endl;
while (current != nullptr) {
cout << "Go " << current->direction << " for " << current->distance << " km." << endl;
current = current->prev;
}
}

int main() {
RouteStep* head = nullptr;
RouteStep* tail = nullptr;

// Adding route steps


addRouteStep(head, tail, "north", 5);
CSL-221 Data structure &
Algorithm
Semester 03

addRouteStep(head, tail, "east", 3);


addRouteStep(head, tail, "south", 6);

// Navigating from home to university


navigateHomeToUniversity(head);

cout << endl;

// Navigating from university to home


navigateUniversityToHome(tail);

// Deleting the entire route


deleteRoute(head, tail);

return 0;
}

OUTPUT:

Exercise 2: Task Management System

You are a project manager overseeing the development of a new software application.
Your team consists of developers, designers, and testers, each responsible for different
aspects of the project. You decide to use a task management system to organize and track
the progress of various tasks throughout the project lifecycle.

1. Inserting Tasks:
As a project manager, you need to add a new task at the beginning of the task list.
2. Managing Task Positions:
CSL-221 Data structure &
Algorithm
Semester 03

Suppose a critical task needs to be inserted at a specific position in the task list. How
would you use the provided menu options to accomplish this task?
3. Tracking Task Progress:
As the project progresses, you need to review the list of tasks in both forward and
backward directions. You can check the count of pending and completed tasks.
4. Displaying Tasks:
You can view tasks in reverse order to have a look at older tasks.
5. Deleting Tasks:
Remove a task from the end of the list if it is no longer relevant to the project
6. User Interaction:
How would you ensure that the task management system handles user input errors
effectively, such as attempting to delete a task from an empty list or specifying an invalid
position for insertion or deletion? Provide clear error messages and user prompts to guide
users through the task management process effectively. Design solution for above
program using doubly linked list.
CODE:
#include <iostream>
#include <string>
using namespace std;

// Structure for each task


struct Task {
string description;
bool completed;
Task* next;
Task* prev;
};
// Function to create a new task
Task* createTask(string description) {
Task* newTask = new Task;
newTask->description = description;
newTask->completed = false;
newTask->next = nullptr;
newTask->prev = nullptr;
return newTask;
}
// Function to insert a new task at the beginning of the list
void insertTaskAtBeginning(Task*& head, Task*& tail, string description) {
Task* newTask = createTask(description);

if (head == nullptr) {
head = newTask;
tail = newTask;
}
else {
newTask->next = head;
head->prev = newTask;
head = newTask;
}
CSL-221 Data structure &
Algorithm
Semester 03

}
// Function to insert a new task at a specific position in the list
void insertTaskAtPosition(Task*& head, Task*& tail, string description, int position) {
if (position < 1) {
cout << "Invalid position. Task not inserted." << endl;
return;
}

if (position == 1) {
insertTaskAtBeginning(head, tail, description);
return;
}

Task* current = head;


int currentPosition = 1;
while (current != nullptr && currentPosition < position - 1) {
current = current->next;
currentPosition++;
}

if (current == nullptr) {
cout << "Invalid position. Task not inserted." << endl;
return;
}

Task* newTask = createTask(description);


newTask->next = current->next;
newTask->prev = current;
if (current->next != nullptr) {
current->next->prev = newTask;
}
current->next = newTask;

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

// Function to delete the last task from the list


void deleteLastTask(Task*& head, Task*& tail) {
if (head == nullptr) {
cout << "No tasks to delete." << endl;
return;
}

Task* temp = tail;


if (head == tail) {
head = nullptr;
tail = nullptr;
}
else {
tail = tail->prev;
tail->next = nullptr;
CSL-221 Data structure &
Algorithm
Semester 03

}
delete temp;
}

// Function to display tasks from head to tail


void displayTasksForward(Task* head) {
if (head == nullptr) {
cout << "No tasks to display." << endl;
return;
}

Task* current = head;


while (current != nullptr) {
cout << current->description << " - ";
if (current->completed) {
cout << "Completed";
}
else {
cout << "Pending";
}
cout << endl;
current = current->next;
}
}

// Function to display tasks from tail to head (in reverse order)


void displayTasksReverse(Task* tail) {
if (tail == nullptr) {
cout << "No tasks to display." << endl;
return;
}

Task* current = tail;


while (current != nullptr) {
cout << current->description << " - ";
if (current->completed) {
cout << "Completed";
}
else {
cout << "Pending";
}
cout << endl;
current = current->prev;
}
}

// Function to track count of pending and completed tasks


void trackTaskProgress(Task* head) {
int pendingCount = 0;
int completedCount = 0;
Task* current = head;
while (current != nullptr) {
if (current->completed) {
CSL-221 Data structure &
Algorithm
Semester 03

completedCount++;
}
else {
pendingCount++;
}
current = current->next;
}
cout << "Pending Tasks: " << pendingCount << endl;
cout << "Completed Tasks: " << completedCount << endl;
}

int main() {
Task* head = nullptr;
Task* tail = nullptr;

// Inserting tasks
insertTaskAtBeginning(head, tail, "Task 1");
insertTaskAtBeginning(head, tail, "Task 2");
insertTaskAtBeginning(head, tail, "Task 3");

// Managing task positions


insertTaskAtPosition(head, tail, "Critical Task", 2);

// Tracking task progress


trackTaskProgress(head);

// Displaying tasks
cout << "Tasks in forward order:" << endl;
displayTasksForward(head);

cout << "Tasks in reverse order:" << endl;


displayTasksReverse(tail);

// Deleting tasks
deleteLastTask(head, tail);

return 0;
}
OUTPUT:
CSL-221 Data structure &
Algorithm
Semester 03

Exercise 3: Music Playlist Application

Sara is passionate about music and enjoys listening to music during her free time. Over
the years, she has amassed a vast collection of music albums comprising her favorite
songs. Now, she desires to organize her favorite songs into a new playlist named "My Hit
Collection". Each song in her playlist is characterized by its Song ID, song name, singer's
name, and the year the song was released.

Your task is to develop a C++ program that helps Sara manage her music playlist using a
doubly linked list. The program should incorporate the following functionalities:

1. Adding New Songs: Sara should be able to add new songs to her playlist. Each
new song will be appended to the end of the playlist.
2. Playing Playlist: The program should enable Sara to play her playlist in both forward
and backward directions, allowing her to enjoy her favorite songs in the sequence they
were added or in reverse order.
3. Circular Playback: Sara should have the option to play her playlist in a circular
fashion, ensuring a seamless listening experience without interruption.
4. Searching by Year: The program should provide functionality for Sara to search
for songs based on the year of release. Upon searching, the program should display all
songs released in the specified year.
5. Deleting from the Beginning: Sara should be able to remove songs from the
beginning of the playlist, enabling her to manage her playlist dynamically.
Your program should efficiently implement these functionalities to cater to Sara's needs
and enhance her music listening experience.
CSL-221 Data structure &
Algorithm
Semester 03

CODE:
#include <iostream>
#include <string>
using namespace std;

// Structure for each song


struct Song {
int songID;
string songName;
string singerName;
int releaseYear;
Song* next;
Song* prev;
};

// Function to create a new song


Song* createSong(int songID, string songName, string singerName, int releaseYear) {
Song* newSong = new Song;
newSong->songID = songID;
newSong->songName = songName;
newSong->singerName = singerName;
newSong->releaseYear = releaseYear;
newSong->next = nullptr;
newSong->prev = nullptr;
return newSong;
}

// Function to add a new song to the end of the playlist


void addSongToEnd(Song*& head, Song*& tail, int songID, string songName, string singerName, int
releaseYear) {
Song* newSong = createSong(songID, songName, singerName, releaseYear);

if (head == nullptr) {
head = newSong;
tail = newSong;
}
else {
tail->next = newSong;
newSong->prev = tail;
tail = newSong;
}
}

// Function to display songs in forward order


void playPlaylistForward(Song* head) {
if (head == nullptr) {
cout << "Playlist is empty." << endl;
return;
}

Song* current = head;


while (current != nullptr) {
CSL-221 Data structure &
Algorithm
Semester 03

cout << "Song ID: " << current->songID << ", Name: " << current->songName << ", Singer: " << current-
>singerName << ", Year: " << current->releaseYear << endl;
current = current->next;
}
}

// Function to display songs in reverse order


void playPlaylistBackward(Song* tail) {
if (tail == nullptr) {
cout << "Playlist is empty." << endl;
return;
}

Song* current = tail;


while (current != nullptr) {
cout << "Song ID: " << current->songID << ", Name: " << current->songName << ", Singer: " << current-
>singerName << ", Year: " << current->releaseYear << endl;
current = current->prev;
}
}

// Function to play playlist in circular fashion


void playPlaylistCircular(Song* head) {
if (head == nullptr) {
cout << "Playlist is empty." << endl;
return;
}

Song* current = head;


do {
cout << "Song ID: " << current->songID << ", Name: " << current->songName << ", Singer: " << current-
>singerName << ", Year: " << current->releaseYear << endl;
current = current->next;
} while (current != head);
}

// Function to search for songs based on release year


void searchByYear(Song* head, int year) {
if (head == nullptr) {
cout << "Playlist is empty." << endl;
return;
}

Song* current = head;


bool found = false;
while (current != nullptr) {
if (current->releaseYear == year) {
cout << "Song ID: " << current->songID << ", Name: " << current->songName << ", Singer: " <<
current->singerName << ", Year: " << current->releaseYear << endl;
found = true;
}
current = current->next;
}
CSL-221 Data structure &
Algorithm
Semester 03

if (!found) {
cout << "No songs found for the specified year." << endl;
}
}

// Function to delete the first song from the playlist


void deleteFirstSong(Song*& head, Song*& tail) {
if (head == nullptr) {
cout << "Playlist is empty. No song to delete." << endl;
return;
}

Song* temp = head;


head = head->next;
if (head != nullptr) {
head->prev = nullptr;
}
else {
tail = nullptr;
}
delete temp;
}

int main() {
Song* head = nullptr;
Song* tail = nullptr;

// Adding new songs to the playlist


addSongToEnd(head, tail, 1, "Song1", "Singer1", 2000);
addSongToEnd(head, tail, 2, "Song2", "Singer2", 2005);
addSongToEnd(head, tail, 3, "Song3", "Singer3", 2010);

// Playing playlist in forward direction


cout << "Playlist in forward direction:" << endl;
playPlaylistForward(head);
cout << endl;

// Playing playlist in backward direction


cout << "Playlist in backward direction:" << endl;
playPlaylistBackward(tail);
cout << endl;

// Searching for songs by year


cout << "Songs released in 2005:" << endl;
searchByYear(head, 2005);
cout << endl;

// Deleting the first song from the playlist


deleteFirstSong(head, tail);
cout << "Playlist after deleting the first song:" << endl;
playPlaylistForward(head);
CSL-221 Data structure &
Algorithm
Semester 03

return 0;
}

OUTPUT:

You might also like