Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
25 views19 pages

DS Assignment 1

Uploaded by

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

DS Assignment 1

Uploaded by

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

COMSATS University Islamabad

DATA STRUCTURES
(CLASS)
ASSIGNMENT: 1
NAME: QURAISHA AZAM

REGISTRATION NO.: SP23-BDS-042

CLASS: BSDS-4A

INSTRUCTOR’S NAME: Dr. SAMERA BATOOL

DATE OF SUBMISSION: 2nd OCTOBER, 2024

1
COMSATS University Islamabad
Tasks of Singly Linked List
Task 1:
Code:
#include <iostream>
#include <string>

struct Node {
std::string name;
Node* next;

Node(std::string val) : name(val), next(nullptr) {} // Constructor for easy node creation


};

std::string findMiddle(Node* head) {


if (!head) return ""; // Return empty string if the list is empty

Node* slow = head;


Node* fast = head;

while (fast != nullptr && fast->next != nullptr) {


slow = slow->next; // Move slow by one step
fast = fast->next->next; // Move fast by two steps
}

return slow->name; // Return the middle node's name


}

int main() {
// Example usage:
Node* head = new Node("Alice");
head->next = new Node("Bob");
head->next->next = new Node("Charlie");
head->next->next->next = new Node("Diana");
head->next->next->next->next = new Node("Eve");

std::cout << "Middle node name: " << findMiddle(head) << std::endl; // Output: Charlie

// Clean up memory (optional)


Node* current = head;
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}

return 0;
2
COMSATS University Islamabad
}

Output:

Task 2:
Code:
#include <iostream>
using namespace std;

// Node structure to represent each fruit's price in the customer's shopping cart
struct Node {
int price;
Node* next;

// Constructor
Node(int p) : price(p), next(nullptr) {}
};

// Function to insert a node at the end of the linked list


void insertNode(Node*& head, int price) {
Node* newNode = new Node(price);
if (!head) {
head = newNode;
} else {
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to print the linked list


void printList(Node* head) {
while (head) {
cout << head->price << " -> ";
head = head->next;
3
COMSATS University Islamabad
}
cout << "nullptr" << endl;
}

// Function to separate the even and odd priced fruits into two separate lists
void separateEvenOdd(Node* cart, Node*& evenCart, Node*& oddCart) {
while (cart != nullptr) {
if (cart->price % 2 == 0) {
// Insert in evenCart
insertNode(evenCart, cart->price);
} else {
// Insert in oddCart
insertNode(oddCart, cart->price);
}
cart = cart->next;
}
}

int main() {
Node* cart = nullptr;
Node* evenCart = nullptr;
Node* oddCart = nullptr;

// Sample input: customer picks fruits with prices 10, 21, 4, 17, 8
insertNode(cart, 10);
insertNode(cart, 21);
insertNode(cart, 4);
insertNode(cart, 17);
insertNode(cart, 8);

// Display the original cart


cout << "Original cart: ";
printList(cart);

// Separate the prices into even and odd carts


separateEvenOdd(cart, evenCart, oddCart);

// Display the results


cout << "Even cart: ";
printList(evenCart);
cout << "Odd cart: ";
printList(oddCart);

return 0;
}

4
COMSATS University Islamabad
Output:

Task 3:
Code:
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

// Function to reverse a linked list


Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* current = head;
Node* next = nullptr;

while (current != nullptr) {


next = current->next;
current->next = prev;
prev = current;
current = next;
}

return prev;
}

// Function to find the middle of the list


Node* findMiddle(Node* head) {
Node* slow = head;
Node* fast = head;
while (fast != nullptr && fast->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
5
COMSATS University Islamabad
return slow;
}

// Function to reverse first and second halves separately


void reverseHalves(Node*& head) {
if (head == nullptr) return;

Node* mid = findMiddle(head);


Node* secondHalf = mid->next;
mid->next = nullptr;

head = reverseList(head); // Reverse first half


secondHalf = reverseList(secondHalf); // Reverse second half

// Reconnect the two halves


Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = secondHalf;
}

// Function to insert a new node


void insertNode(Node*& head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to print the linked list


void printList(Node* head) {
while (head != nullptr) {
cout << head->data << " -> ";
head = head->next;
}
cout << "nullptr" << endl;
}

// Driver function

6
COMSATS University Islamabad
int main() {
Node* head = nullptr;

for (int i = 1; i <= 8; i++) {


insertNode(head, i);
}

cout << "Original list: ";


printList(head);

reverseHalves(head);

cout << "Reversed halves list: ";


printList(head);
return 0;
}

Output:

Task 4:
Code:
#include <iostream>
#include <unordered_set>
using namespace std;

struct Node {
int data;
Node* next;
};

// Function to remove duplicates from a linked list


void removeDuplicates(Node* head) {
if (head == nullptr) return;

unordered_set<int> seen;
Node* current = head;
Node* prev = nullptr;

while (current != nullptr) {


if (seen.find(current->data) != seen.end()) {
prev->next = current->next;
delete current;
7
COMSATS University Islamabad
} else {
seen.insert(current->data);
prev = current;
}
current = prev->next;
}
}

// Function to insert a new node


void insertNode(Node*& head, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to print the linked list


void printList(Node* head) {
while (head != nullptr) {
cout << head->data << " -> ";
head = head->next;
}
cout << "nullptr" << endl;
}

// Driver function
int main() {
Node* head = nullptr;

insertNode(head, 1);
insertNode(head, 2);
insertNode(head, 3);
insertNode(head, 2);
insertNode(head, 4);
insertNode(head, 5);

cout << "Original list: ";


printList(head);

removeDuplicates(head);

8
COMSATS University Islamabad
cout << "List after removing duplicates: ";
printList(head);
return 0;
}

Output:

Task 5:
Code:
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

// Function to delete all nodes with a specific identifier


void deleteNodes(Node*& head, int identifier) {
while (head != nullptr && head->data == identifier) {
Node* temp = head;
head = head->next;
delete temp;
}

Node* current = head;


while (current != nullptr && current->next != nullptr) {
if (current->next->data == identifier) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
} else {
current = current->next;
}
}
}

// Function to insert a new node


void insertNode(Node*& head, int data) {
Node* newNode = new Node();

9
COMSATS University Islamabad
newNode->data = data;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}

// Function to print the linked list


void printList(Node* head) {
while (head != nullptr) {
cout << head->data << " -> ";
head = head->next;
}
cout << "nullptr" << endl;
}

// Driver function
int main() {
Node* head = nullptr;

insertNode(head, 1);
insertNode(head, 2);
insertNode(head, 3);
insertNode(head, 2);
insertNode(head, 4);

cout << "Original list: ";


printList(head);

deleteNodes(head, 2);

cout << "List after deleting nodes with identifier 2: ";


printList(head);
return 0;
}

Output:

10
COMSATS University Islamabad
Tasks of Doubly Linked List
Task 1:
Code:
#include <iostream>
using namespace std;

struct Node {
string data;
Node* prev;
Node* next;
};

// Function to swap nodes from start to end


void swapDesks(Node*& head) {
if (head == nullptr) return;

Node* start = head;


Node* end = head;

// Move 'end' to the last node


while (end->next != nullptr) {
end = end->next;
}

// Swap nodes from start to end


while (start != end && start->prev != end) {
// Swap data of start and end
swap(start->data, end->data);

// Move start forward and end backward


start = start->next;
end = end->prev;
}
}

// Function to insert a new node at the end


void insertNode(Node*& head, const string& data) {
Node* newNode = new Node();
newNode->data = data;
newNode->prev = nullptr;
newNode->next = nullptr;

if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
11
COMSATS University Islamabad
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}

// Function to print the list


void printList(Node* head) {
while (head != nullptr) {
cout << head->data << " <-> ";
head = head->next;
}
cout << "nullptr" << endl;
}

// Driver function
int main() {
Node* head = nullptr;

// Inserting desks
insertNode(head, "Alice");
insertNode(head, "Bob");
insertNode(head, "Charlie");
insertNode(head, "Dana");
insertNode(head, "Eva");
insertNode(head, "Frank");

cout << "Original arrangement: ";


printList(head);

// Perform the swaps


swapDesks(head);

cout << "New arrangement after swaps: ";


printList(head);

return 0;
}
Output:

12
COMSATS University Islamabad
Task 2:
Code:
#include <iostream>
using namespace std;

struct Song {
string title;
Song* prev;
Song* next;
};

// Function to insert a new song at the end of the playlist


void insertSong(Song*& head, const string& title) {
Song* newSong = new Song();
newSong->title = title;
newSong->prev = nullptr;
newSong->next = nullptr;

if (head == nullptr) {
head = newSong;
} else {
Song* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newSong;
newSong->prev = temp;
}
}

// Function to print the playlist


void printPlaylist(Song* head) {
cout << "Playlist: ";
while (head != nullptr) {
cout << head->title << " <-> ";
head = head->next;
}
cout << "nullptr" << endl;
}

// Function to play songs forward


void playForward(Song* head) {
cout << "Playing forward: ";
while (head != nullptr) {
cout << head->title << " -> ";
head = head->next;
}
13
COMSATS University Islamabad
cout << "End" << endl;
}

// Function to play songs backward


void playBackward(Song* tail) {
cout << "Playing backward: ";
while (tail != nullptr) {
cout << tail->title << " -> ";
tail = tail->prev;
}
cout << "Start" << endl;
}

// Driver function
int main() {
Song* playlist = nullptr;

// Insert songs into the playlist


insertSong(playlist, "Song A");
insertSong(playlist, "Song B");
insertSong(playlist, "Song C");
insertSong(playlist, "Song D");

// Print the playlist


printPlaylist(playlist);

// Play songs forward


playForward(playlist);

// Move to the last song in the playlist


Song* tail = playlist;
while (tail->next != nullptr) {
tail = tail->next;
}

// Play songs backward


playBackward(tail);

return 0;
}

Output:

14
COMSATS University Islamabad
Tasks of Circularly Linked List
Task 1:
Code:
#include <iostream>
#include <string>
using namespace std;

struct Task {
string name;
int priority;
string status; // Can be "pending", "in-progress", or "completed"
Task* next;
};

// Function to insert a task at the end of the circular linked list


void addTask(Task*& head, const string& name, int priority, const string& status) {
Task* newTask = new Task();
newTask->name = name;
newTask->priority = priority;
newTask->status = status;
newTask->next = nullptr;

if (head == nullptr) {
// The first task in the list
head = newTask;
head->next = head; // Circular link to itself
} else {
Task* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newTask;
newTask->next = head; // Circular link back to the head
}
}

// Function to remove a task by its name from the circular linked list
void removeTask(Task*& head, const string& name) {
if (head == nullptr) return;

Task* temp = head;


Task* prev = nullptr;

// If the task to be removed is the head task

15
COMSATS University Islamabad
if (head->name == name) {
if (head->next == head) {
delete head; // Only one task in the list
head = nullptr;
return;
} else {
// Move to the last node and re-link the head
while (temp->next != head) {
temp = temp->next;
}
Task* toDelete = head;
temp->next = head->next;
head = head->next;
delete toDelete;
return;
}
}

// Search for the task to be removed


do {
prev = temp;
temp = temp->next;
if (temp->name == name) {
prev->next = temp->next;
delete temp;
return;
}
} while (temp != head);
}

// Function to get the next pending task in round-robin fashion


Task* getNextTask(Task*& current) {
if (current == nullptr) return nullptr;

Task* temp = current;


do {
if (temp->status == "pending") {
current = temp->next; // Update the current task to the next in round-robin
return temp;
}
temp = temp->next;
} while (temp != current);

return nullptr; // No pending tasks found


}

// Function to display all tasks


void displayTasks(Task* head) {

16
COMSATS University Islamabad
if (head == nullptr) {
cout << "No tasks available." << endl;
return;
}

Task* temp = head;


do {
cout << "Task: " << temp->name << ", Priority: " << temp->priority
<< ", Status: " << temp->status << endl;
temp = temp->next;
} while (temp != head);
}

// Function to update the status of a task by its name


void updateTaskStatus(Task* head, const string& name, const string& newStatus) {
if (head == nullptr) return;

Task* temp = head;


do {
if (temp->name == name) {
temp->status = newStatus;
cout << "Task " << name << " status updated to: " << newStatus << endl;
return;
}
temp = temp->next;
} while (temp != head);

cout << "Task " << name << " not found!" << endl;
}

// Driver function
int main() {
Task* head = nullptr;
Task* current = nullptr;

// Adding tasks
addTask(head, "Task1", 1, "pending");
addTask(head, "Task2", 2, "pending");
addTask(head, "Task3", 3, "completed");
addTask(head, "Task4", 4, "in-progress");
current = head; // Initialize current task pointer

// Display all tasks


cout << "All Tasks:" << endl;
displayTasks(head);

// Get next pending task in round-robin


Task* nextTask = getNextTask(current);

17
COMSATS University Islamabad
if (nextTask) {
cout << "\nNext task to execute: " << nextTask->name << endl;
} else {
cout << "No pending tasks found." << endl;
}

// Update a task status


cout << "\nUpdating Task2 to 'completed':" << endl;
updateTaskStatus(head, "Task2", "completed");

// Display all tasks again after status update


cout << "\nAll Tasks after status update:" << endl;
displayTasks(head);

// Remove a task
cout << "\nRemoving Task3 from the list:" << endl;
removeTask(head, "Task3");

// Display tasks after removal


cout << "\nAll Tasks after removing Task3:" << endl;
displayTasks(head);

return 0;
}

Output:

18
COMSATS University Islamabad

19

You might also like