Professional Documents
Culture Documents
DSA Class1
DSA Class1
DSA Class1
CLASS: BSDS-3A
1
COMSATS University Islamabad
Contents
Tasks of Singly Linked List:.....................................................................................................................3
Task of Circular Linked List:.................................................................................................................25
Tasks of Doubly Linked List:..................................................................................................................28
2
COMSATS University Islamabad
struct LinkedListElement {
LinkedListElement* next;
LinkedListElement(int value) {
this->value = value;
this->next = nullptr;
};
if (start == nullptr) {
return;
printListInReverse(start->next);
int main() {
3
COMSATS University Islamabad
head->next->next->next = new LinkedListElement(4);
temp = temp->next;
printListInReverse(head);
return 0;
Dry Run:
Step Operation head temp output
3 Print the list in reverse order 1->2->3->4 nullptr List in reverse order: 4 3 2 1
4
COMSATS University Islamabad
2. Reverse the linked list (before and after the operation, address of each node will
remain same).
Code:
#include <iostream>
struct ListElement {
int value;
ListElement* next;
ListElement(int value) {
this->value = value;
this->next = nullptr;
};
5
COMSATS University Islamabad
// Function to print the linked list
head = head->next;
int main() {
printList(first);
printList(first);
return 0;
6
COMSATS University Islamabad
Dry Run:
Step Operation first previous current temp output
4 2->1->3->4 1 2 3->4
5 3->2->1->4 2 3 4
6 4->3->2->1 3 4 nullptr
7
COMSATS University Islamabad
3. Remove duplicates from a linked list.
Code:
#include <iostream>
struct ListItem {
int value;
ListItem* next;
ListItem(int value) {
this->value = value;
this->next = nullptr;
};
if (runner->value == current->value) {
current->next = runner->next;
delete temp;
} else {
runner = runner->next;
current = current->next;
8
COMSATS University Islamabad
void printList(ListItem* head) {
head = head->next;
int main() {
printList(first);
removeDuplicates(first);
printList(first);
return 0;
9
COMSATS University Islamabad
Dry Run:
Step Operation first current runner temp output
1->2->2-
2 Print the original list >3->4 Original list: 1 2 2 3 4
6 3->4 4 nullptr
10
COMSATS University Islamabad
4. Detect loop or cycle in a linked list.
Code:
#include <iostream>
using namespace std;
struct LinkItem {
int value;
LinkItem* next;
LinkItem(int value) {
this->value = value;
this->next = nullptr;
}
}
bool hasLoop(LinkItem* head) {
LinkItem* slow = head;
LinkItem* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
return true;
}
}
return false;
}
int main() {
LinkItem* first = new LinkItem(1);
first->next = new LinkItem(2);
first->next->next = new LinkItem(3);
first->next->next->next = new LinkItem(4);
first->next->next->next->next = new LinkItem(5);
first->next->next->next->next->next = first->next;
if (hasLoop(first)) {
cout << "Loop detected in the linked list." << endl;
} else {
cout << "No loop detected in the linked list." << endl;
}
return 0;
}
11
COMSATS University Islamabad
Dry Run:
3 1->2->3->4->5->2 2 3 No
4 1->2->3->4->5->2 3 5 No
12
COMSATS University Islamabad
5. Swap any two nodes (only links will change, not to swap the values).
Code:
#include <iostream>
struct ListItem {
int value;
ListItem* next;
ListItem(int value) {
this->value = value;
this->next = nullptr;
} };
if (x == y) return;
prevX = currX;
currX = currX->next;
prevY = currY;
currY = currY->next;
13
COMSATS University Islamabad
if (currX == nullptr || currY == nullptr) return;
if (prevX != nullptr) {
prevX->next = currY;
} else {
if (prevY != nullptr) {
prevY->next = currX;
} else {
currX->next = currY->next;
currY->next = temp;
head = head->next;
int main() {
14
COMSATS University Islamabad
printList(head);
int x = 2, y = 4;
swapNodes(&head, x, y);
printList(head);
return 0;
Dry Run:
Step Operation head prevX currX prevY currY temp
15
COMSATS University Islamabad
6. Assume that you have a linked list having even and odd values. Your task is to spit
the list into two separate lists of even and odd.
Code:
#include <iostream>
using namespace std;
struct DataNode {
int value;
DataNode* next;
DataNode(int value) {
this->value = value;
this->next = nullptr;
}
};
void separateEvenOdd(DataNode* head, DataNode*& evenHead, DataNode*& oddHead) {
DataNode* evenTail = nullptr;
DataNode* oddTail = nullptr;
16
COMSATS University Islamabad
head = head->next;
}
cout << endl;
}
int main() {
DataNode* head = new DataNode(1);
head->next = new DataNode(2);
head->next->next = new DataNode(3);
head->next->next->next = new DataNode(4);
head->next->next->next->next = new DataNode(5);
DataNode* evenHead = nullptr;
DataNode* oddHead = nullptr;
separateEvenOdd(head, evenHead, oddHead);
cout << "Original list: ";
printList(head);
cout << "Even list: ";
printList(evenHead);
cout << "Odd list: ";
printList(oddHead);
return 0;
}
Dry Run:
Step Operation head evenHead oddHead
1->2->3->4-
1 Create the linked list >5
1->2->3->4-
3 Print the original list >5
17
COMSATS University Islamabad
7. Reverse the first half and second of the linked list i.e. assume that the list is:1->2->3-
>4->5->6->7->8 After calling the reverse method the list should be:4->3->2->1->8-
>7->6->5.
Code:
#include <iostream>
struct ListNode {
int value;
ListNode* next;
};
int length = 0;
while (head) {
length++;
head = head->next;
return length;
while (current) {
18
COMSATS University Islamabad
current->next = prev;
prev = current;
current = nextNode;
return prev;
return;
prev = current;
current = current->next;
prev->next = nullptr;
*headRef = reverseList(*headRef);
19
COMSATS University Islamabad
current = *headRef;
while (current->next) {
current = current->next;
current->next = secondHalf;
while (head) {
head = head->next;
int main() {
current = current->next;
printList(head);
20
COMSATS University Islamabad
// Reverse the first and second half
reverseFirstAndSecondHalf(&head);
printList(head);
return 0;
Dry Run:
Step Operation head current prev length halfLength secondHalf
1->2->3->4-
1 Create the linked list >5->6->7->8 8 4
1->2->3->4-
2 Print the original list >5->6->7->8
1->2->3->4-
Reverse the second >5->4->3- 5->4->3->2-
4 half of the linked list >2->1 >1
5->4->3->2-
7 Join the two halves >1->6->7->8
5->4->3->2-
8 Print the reversed list >1->6->7->8
21
COMSATS University Islamabad
8. Given a singly linked list, write a function to swap elements pairwise i.e. assume that
the list is:Input : 1->2->3->4->5->6->NULL Output : 2->1->4->3->6->5->NULL.
Code:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
// Function to swap elements pairwise in a linked list
void swapPairs(Node** head_ref) {
// Handle empty or single-node lists
if (*head_ref == nullptr || (*head_ref)->next == nullptr) {
return;
}
Node* curr = *head_ref;
Node* prev = nullptr;
while (curr != nullptr && curr->next != nullptr) {
// Swap current and next node
if (prev != nullptr) {
prev->next = curr->next;
} else {
*head_ref = curr->next; // Update head if swapping the first node
}
Node* next = curr->next->next;
curr->next->next = curr;
curr->next = next;
// Update pointers for next iteration
prev = curr;
curr = next;
}
}
void printList(Node* head) {
while (head != nullptr) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
int main() {
Node* head = new Node(1);
22
COMSATS University Islamabad
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
head->next->next->next->next->next = new Node(6);
cout << "Original list: ";
printList(head);
swapPairs(&head); // Pass the address of head to modify the original list
cout << "Swapped list: ";
printList(head);
return 0; }
Dry Run:
Action Updated State of List
Create node with data 2 and link to node with 1 1 -> 2 -> nullptr
Create node with data 3 and link to node with 2 1 -> 2 -> 3 -> nullptr
Create node with data 4 and link to node with 3 1 -> 2 -> 3 -> 4 -> nullptr
Create node with data 5 and link to node with 4 1 -> 2 -> 3 -> 4 -> 5 -> nullptr
Create node with data 6 and link to node with 5 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> nullptr
Original list: 1 2 3 4 5 6
Swap nodes 1 and 2 2 -> 1 -> 3 -> 4 -> 5 -> 6 -> nullptr
Swap nodes 3 and 4 2 -> 1 -> 4 -> 3 -> 5 -> 6 -> nullptr
Swap nodes 5 and 6 2 -> 1 -> 4 -> 3 -> 6 -> 5 -> nullptr
Swapped list: 2 1 4 3 6 5
23
COMSATS University Islamabad
1 Create the linked list with nodes 1->2->3->4->5->6
24
COMSATS University Islamabad
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
class CircularLinkedList {
private:
Node* head;
int size;
public:
CircularLinkedList() : head(nullptr), size(0) {}
25
COMSATS University Islamabad
newNode->next = head; // Circular
}
size++;
}
int josephus(int M) {
Node* prev = head;
Node* current = head;
while (size > 1) {
// Skip M-1 nodes
for (int i = 0; i < M - 1; ++i) {
prev = current;
current = current->next;
}
// Remove the Mth node
prev->next = current->next;
Node* temp = current;
current = current->next;
delete temp;
size--;
}
return head->data;
}
void display() {
if (head == nullptr) {
cout << "List is empty" << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
int N, M;
26
COMSATS University Islamabad
cout << "Enter the total number of persons (N): ";
cin >> N;
cout << "Enter the value of M: ";
cin >> M;
CircularLinkedList circle;
for (int i = 1; i <= N; ++i) {
circle.append(i);
}
6 14 Skip 2, Kill 1 -
27
COMSATS University Islamabad
28
COMSATS University Islamabad
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}
// Function to split the list into two lists - even index and odd index
void splitList(ListNode* head, ListNode*& evenIndexList, ListNode*& oddIndexList) {
ListNode* current = head;
int index = 0;
while (current != nullptr) {
if (index % 2 == 0) {
insert(evenIndexList, current->data);
} else {
insert(oddIndexList, current->data);
}
current = current->next;
index++;
}
}
// Function to reverse a linked list
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* current = head;
while (current != nullptr) {
ListNode* next = current->next;
current->next = prev;
current->prev = next;
prev = current;
current = next;
}
return prev;
}
// Function to merge two linked lists alternatively
ListNode* mergeLists(ListNode* list1, ListNode* list2) {
if (list1 == nullptr) return list2;
if (list2 == nullptr) return list1;
29
COMSATS University Islamabad
result = list1;
current = list1;
list1 = list1->next;
} else {
current->next = list2;
list2->prev = current;
current = list2;
list2 = list2->next;
current->next = list1;
list1->prev = current;
current = list1;
list1 = list1->next;
}
}
if (list1 != nullptr) {
current->next = list1;
list1->prev = current;
}
return result;
}
// Function to print the linked list
void printList(ListNode* head) {
ListNode* current = head;
while (current != nullptr) {
cout << current->data << "->";
current = current->next;
}
cout << "None" << endl;
}
int main() {
ListNode* head = nullptr; // Input list
insert(head, 1);
insert(head, 2);
insert(head, 3);
insert(head, 4);
insert(head, 5);
insert(head, 6);
insert(head, 7);
insert(head, 8);
insert(head, 9);
ListNode* evenIndexList = nullptr;
ListNode* oddIndexList = nullptr;
// Step 1: Split the list into two lists
splitList(head, evenIndexList, oddIndexList);
30
COMSATS University Islamabad
// Step 2: Reverse the list containing nodes at odd indices
oddIndexList = reverseList(oddIndexList);
31
COMSATS University Islamabad
Dry Run:
Step Operation head evenIndexList oddIndexList current result
1->3->5->7->9 7->5->3->1
7->5->3->1->2->4- 2->4->6-
1->3->5->7->9 >6->8 >8 1->9
1->3->5->7->9->2- 7->5->3->1->2->4-
>4->6->8 >6->8
1->3->5->7->9->2- 5->3->1->2->4->6-
>4->6->8 >8
1->3->5->7->9->2-
>4->6->8 3->1->2->4->6->8
1->9-
1->3->5->7->9->2- >7-
>4->6->8 3->1->2->4->6->8 >5->3
1->3->5->7->9->2-
>4->6->8 1->2->4->6->8
1->9-
>7-
1->3->5->7->9->2- >5-
>4->6->8 1->2->4->6->8 >3->2
1->3->5->7->9->2-
>4->6->8 4->6->8
32
COMSATS University Islamabad
Step Operation head evenIndexList oddIndexList current result
1->9-
>7-
>5-
1->3->5->7->9->2- >3-
>4->6->8 4->6->8 >2->4
1->3->5->7->9->2-
>4->6->8 6->8
1->9-
>7-
>5-
>3-
1->3->5->7->9->2- >2-
>4->6->8 6->8 >4->6
1->9-
>7-
>5-
>3-
>2-
>4-
>6->8
33
COMSATS University Islamabad
2. Suppose you are working on a project to develop an inventory management system
for a retail chain that has multiple stores in different cities across the country. The
system should be able to maintain the records of the items in each store. Each store
has different sections, and each section has different products. To implement this
system, use best suitable Data Structure to maintain the records of items, locations,
stores, and sections. Write the methods to perform following operations:
Code:
#include <iostream>
#include <string>
using namespace std;
struct ShoeNode {
string name;
int pieces;
int size;
string color;
ShoeNode* prev;
ShoeNode* next;
};
34
COMSATS University Islamabad
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
// Function to search for a shoe in the store
void searchShoe(const ShoeNode* head, string name) {
const ShoeNode* current = head;
bool found = false;
while (current != nullptr) {
if (current->name == name) {
cout << "Name: " << current->name << ", Pieces: " << current->pieces << ", Size: " << current-
>size << ", Color: " << current->color << endl;
found = true;
break;
}
current = current->next;
}
if (!found) {
cout << "Shoe not found!" << endl;
}
}
35
COMSATS University Islamabad
ShoeNode* prevNode = current->prev;
prevNode->next = current->next;
if (current->next != nullptr) {
current->next->prev = prevNode;
}
delete current;
cout << "Shoe deleted successfully!" << endl;
}
36
COMSATS University Islamabad
addShoe(shoeArtistry, "women", 100, 37, "yellow");
// Laces of Happiness
addShoe(lacesOfHappiness, "kid", 100, 25, "pink");
addShoe(lacesOfHappiness, "men", 100, 44, "brown");
// For example, search for a shoe in a store:
searchShoe(happyFeet, "kid");
// Free memory
deleteLinkedList(happyFeet);
deleteLinkedList(theShoeSpot);
deleteLinkedList(shoeArtistry);
deleteLinkedList(lacesOfHappiness);
return 0;
}
Dry Run:
37
COMSATS University Islamabad
This experiment leads to the
description of logic gates; NAND,
AND, NOT, NOR, OR and
XOR gate.
38