DSA Class1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

COMSATS University Islamabad

DATA STRUCTURES AND ALGORITHMS


(THEORY)
CLASS ASSIGNMENT NO: 1
NAME: NAYAB KHALID

REGISTRATION NO.: SP23-BDS-039

CLASS: BSDS-3A

INSTRUCTOR’S NAME: MS. SAMERA BATOOL

DATE OF SUBMISSION: 15TH MARCH, 2024

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

Tasks of Singly Linked List:


1. Print the linked list in reverse order.
Code:
#include <iostream>

using namespace std;

struct LinkedListElement {

int value; // Renamed data to value

LinkedListElement* next;

LinkedListElement(int value) {

this->value = value;

this->next = nullptr;

};

void printListInReverse(LinkedListElement* start) {

if (start == nullptr) {

return;

printListInReverse(start->next);

cout << start->value << " ";

int main() {

// Create the linked list (same logic)

LinkedListElement* head = new LinkedListElement(1);

head->next = new LinkedListElement(2);

head->next->next = new LinkedListElement(3);

3
COMSATS University Islamabad
head->next->next->next = new LinkedListElement(4);

cout << "Original list: ";

LinkedListElement* temp = head;

while (temp != nullptr) {

cout << temp->value << " ";

temp = temp->next;

cout << endl;

cout << "List in reverse order: ";

printListInReverse(head);

cout << endl;

return 0;

Dry Run:
Step Operation head temp output

1 Create the linked list with nodes 1->2->3->4

2 Print the original list 1->2->3->4 1 Original list: 1 2 3 4

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>

using namespace std;

struct ListElement {

int value;

ListElement* next;

ListElement(int value) {

this->value = value;

this->next = nullptr;

};

ListElement* reverseList(ListElement* head) {

ListElement* previous = nullptr;

ListElement* current = head;

ListElement* temp; // New temporary variable

while (current != nullptr) {

temp = current->next; // Store next node

current->next = previous; // Reverse the link

previous = current; // Move previous pointer

current = temp; // Move current pointer

return previous; // New head is the previous last node

5
COMSATS University Islamabad
// Function to print the linked list

void printList(ListElement* head) {

while (head != nullptr) {

cout << head->value << " ";

head = head->next;

cout << endl;

int main() {

ListElement* first = new ListElement(1); // Renamed head to first

first->next = new ListElement(2);

first->next->next = new ListElement(3);

first->next->next->next = new ListElement(4);

cout << "Original list: ";

printList(first);

first = reverseList(first); // Reverse the list

cout << "Reversed list: ";

printList(first);

return 0;

6
COMSATS University Islamabad
Dry Run:
Step Operation first previous current temp output

1 Create the linked list with nodes 1->2->3->4

2 Print the original list 1->2->3->4 Original list: 1 2 3 4

3 Reverse the linked list 1->2->3->4 nullptr 1 2->3->4

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 Print the reversed list 4->3->2->1 Reversed list: 4 3 2 1

7
COMSATS University Islamabad
3. Remove duplicates from a linked list.

Code:

#include <iostream>

using namespace std;

struct ListItem {

int value;

ListItem* next;

ListItem(int value) {

this->value = value;

this->next = nullptr;

};

void removeDuplicates(ListItem* head) {

ListItem* current = head;

while (current != nullptr) {

ListItem* runner = current->next;

while (runner != nullptr) {

if (runner->value == current->value) {

ListItem* temp = runner;

current->next = runner->next;

delete temp;

} else {

runner = runner->next;

current = current->next;

8
COMSATS University Islamabad
void printList(ListItem* head) {

while (head != nullptr) {

cout << head->value << " ";

head = head->next;

cout << endl;

int main() {

ListItem* first = new ListItem(1);

first->next = new ListItem(2);

first->next->next = new ListItem(2);

first->next->next->next = new ListItem(3);

first->next->next->next->next = new ListItem(4);

cout << "Original list: ";

printList(first);

removeDuplicates(first);

cout << "List after removing duplicates: ";

printList(first);

return 0;

9
COMSATS University Islamabad
Dry Run:
Step Operation first current runner temp output

Create the linked list with 1->2->2-


1 nodes >3->4

1->2->2-
2 Print the original list >3->4 Original list: 1 2 2 3 4

Remove duplicates from 1->2->3- 1->2->2->3- 2->2->3-


3 the linked list >4 >4 >4 nullptr

4 2->2->3->4 2->3->4 nullptr

5 2->3->4 3->4 nullptr

6 3->4 4 nullptr

Print the list after removing 1->2->3- List after removing


7 duplicates >4 duplicates: 1 2 3 4

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:

Step Operation head slow fast Loop Detected?

1 Create the linked list with nodes 1->2->3->4->5->2


Move slow and fast pointers through the linked
2 list 1->2->3->4->5->2 1 1 No

3 1->2->3->4->5->2 2 3 No

4 1->2->3->4->5->2 3 5 No

5 1->2->3->4->5->2 4 nullptr Yes

12
COMSATS University Islamabad
5. Swap any two nodes (only links will change, not to swap the values).

Code:
#include <iostream>

using namespace std;

struct ListItem {

int value;

ListItem* next;

ListItem(int value) {

this->value = value;

this->next = nullptr;

} };

void swapNodes(ListItem** headRef, int x, int y) {

// If x and y are the same, do nothing

if (x == y) return;

ListItem* prevX = nullptr, *currX = *headRef;

ListItem* prevY = nullptr, *currY = *headRef;

while (currX != nullptr && currX->value != x) {

prevX = currX;

currX = currX->next;

while (currY != nullptr && currY->value != y) {

prevY = currY;

currY = currY->next;

// If either x or y is not found, return

13
COMSATS University Islamabad
if (currX == nullptr || currY == nullptr) return;

// Swap the next pointers of the nodes

if (prevX != nullptr) {

prevX->next = currY;

} else {

*headRef = currY; // If x is the head, update head

if (prevY != nullptr) {

prevY->next = currX;

} else {

*headRef = currX; // If y is the head, update head

ListItem* temp = currX->next;

currX->next = currY->next;

currY->next = temp;

void printList(ListItem* head) {

while (head != nullptr) {

cout << head->value << " ";

head = head->next;

cout << endl;

int main() {

ListItem* head = new ListItem(1);

head->next = new ListItem(2);

head->next->next = new ListItem(3);

head->next->next->next = new ListItem(4);

cout << "Original list: ";

14
COMSATS University Islamabad
printList(head);

int x = 2, y = 4;

swapNodes(&head, x, y);

cout << "Swapped list: ";

printList(head);

return 0;

Dry Run:
Step Operation head prevX currX prevY currY temp

1 Create the linked list with nodes 1->2->3->4

2 Print the original list 1->2->3->4

3 Identify pointers for nodes with values x and y 1->2->3->4 2 4

Swap the next pointers of nodes with values x and


4 y 1->4->3->2 nullptr nullptr

5 Print the swapped list 1->4->3->2

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;

while (head != nullptr) {


if (head->value % 2 == 0) {
if (evenHead == nullptr) {
evenHead = evenTail = head;
} else {
evenTail->next = head;
evenTail = evenTail->next;
}
} else {
if (oddHead == nullptr) {
oddHead = oddTail = head;
} else {
oddTail->next = head;
oddTail = oddTail->next;
}
}
head = head->next;
}
if (evenTail != nullptr) {
evenTail->next = nullptr;
}
if (oddTail != nullptr) {
oddTail->next = nullptr;
}
}\
void printList(DataNode* head) {
while (head != nullptr) {
cout << head->value << " ";

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

Separate even and odd numbers into separate linked 1->2->3->4-


2 lists >5 2->4->nullptr 1->3->5->nullptr

1->2->3->4-
3 Print the original list >5

4 Print the even list 2->4->nullptr

5 Print the odd list 1->3->5->nullptr

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>

using namespace std;

struct ListNode {

int value;

ListNode* next;

ListNode(int value) : value(value), next(nullptr) {}

};

// Function to find the length of the linked list

int getLength(ListNode* head) {

int length = 0;

while (head) {

length++;

head = head->next;

return length;

// Function to reverse a linked list

ListNode* reverseList(ListNode* head) {

ListNode* prev = nullptr;

ListNode* current = head;

while (current) {

ListNode* nextNode = current->next;

18
COMSATS University Islamabad
current->next = prev;

prev = current;

current = nextNode;

return prev;

// Function to reverse the first and second half of a linked list

void reverseFirstAndSecondHalf(ListNode** headRef) {

if (*headRef == nullptr || (*headRef)->next == nullptr) {

return;

int length = getLength(*headRef);

int halfLength = length / 2;

// Find the middle of the linked list

ListNode* prev = nullptr;

ListNode* current = *headRef;

for (int i = 0; i < halfLength; i++) {

prev = current;

current = current->next;

// Reverse the second half

ListNode* secondHalf = reverseList(current);

// Break the link between first and second half

prev->next = nullptr;

// Reverse the first half

*headRef = reverseList(*headRef);

// Join the two halves

19
COMSATS University Islamabad
current = *headRef;

while (current->next) {

current = current->next;

current->next = secondHalf;

// Function to print a linked list

void printList(ListNode* head) {

while (head) {

cout << head->value << " ";

head = head->next;

cout << endl;

int main() {

// Create the linked list

ListNode* head = new ListNode(1);

ListNode* current = head;

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

current->next = new ListNode(i);

current = current->next;

cout << "Original list: ";

printList(head);

20
COMSATS University Islamabad
// Reverse the first and second half

reverseFirstAndSecondHalf(&head);

cout << "Reversed list: ";

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

Find the middle of the 1->2->3->4- 5->6->7->8- 4->5->6->7-


3 linked list >5->6->7->8 >nullptr >8->nullptr 8 4

1->2->3->4-
Reverse the second >5->4->3- 5->4->3->2-
4 half of the linked list >2->1 >1

Break the link


between first and 1->2->3->4- 4->3->2->1-
5 second half >5 >nullptr

Reverse the first half 5->4->3->2-


6 of the linked list >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

head = nullptr nullptr

Create node with data 1 and assign to head 1 -> nullptr

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

Move pointers for next iteration

Swap nodes 3 and 4 2 -> 1 -> 4 -> 3 -> 5 -> 6 -> nullptr

Move pointers for next iteration

Swap nodes 5 and 6 2 -> 1 -> 4 -> 3 -> 6 -> 5 -> nullptr

Move pointers for next iteration

Swapped list: 2 1 4 3 6 5

Step Operation head curr prev

23
COMSATS University Islamabad
1 Create the linked list with nodes 1->2->3->4->5->6

2 Print the original list 1->2->3->4->5->6

3 Perform pairwise swapping 2->1->4->3->6->5

4 Print the swapped list 2->1->4->3->6->5

24
COMSATS University Islamabad

Task of Circular Linked List:


Write the program to solve the Josephus problem. In Josephus problem you are given the
total number of persons N and the number M, which indicates that M-1 persons are
skipped and the Mth person is killed in the circle, your task is to choose the place in the
initial circle so that you are the last one remaining (survive).
Code:

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

void append(int value) {


Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
head->next = head; // Circular
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;

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);
}

cout << "Initial circle: ";


circle.display();

int survivor = circle.josephus(M);


cout << "The position of the last survivor is: " << survivor << endl;
return 0;
}
Dry Run:
Round Circle Action Survivor Position

1 1234567 Skip 2, Kill 3 -

2 124567 Skip 2, Kill 6 -

3 12457 Skip 2, Kill 2 -

4 1457 Skip 2, Kill 7 -

5 145 Skip 2, Kill 5 -

6 14 Skip 2, Kill 1 -

7 4 No skipping, Last person 4

27
COMSATS University Islamabad

Tasks of Doubly Linked List:


1. Swap every alternate node of the doubly linked list starting from start and end i.e.
assume that the list:
Input: 1->2->3->4->5->6->7->8->9
After calling the method the list should be:
Input: 1->8->3->6->5->4->7->2->9.
Code:
#include <iostream>
using namespace std;
struct ListNode {
int data;
ListNode* prev;
ListNode* next;
ListNode(int data) : data(data), prev(nullptr), next(nullptr) {}
};

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


void insert(ListNode*& head, int data) {
ListNode* newNode = new ListNode(data);
if (head == nullptr) {
head = newNode;
} else {
ListNode* temp = head;

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;

ListNode* result = nullptr;


ListNode* current = nullptr;
while (list1 != nullptr && list2 != nullptr) {
if (result == nullptr) {

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

// Step 3: Merge both lists alternatively


ListNode* mergedList = mergeLists(evenIndexList, oddIndexList);
// Print the merged list
cout << "Merged List: ";
printList(mergedList);
// Free memory
ListNode* current = mergedList;
while (current != nullptr) {
ListNode* temp = current;
current = current->next;
delete temp;
}
return 0;
}

31
COMSATS University Islamabad
Dry Run:
Step Operation head evenIndexList oddIndexList current result

Insert nodes into input 1->2->3->4->5-


1 list >6->7->8->9

Split the list into even 1->2->3->4->5-


2 and odd indices >6->7->8->9 1->3->5->7->9 2->4->6->8

Reverse the list 1->2->3->4->5-


3 containing odd indices >6->7->8->9 1->3->5->7->9 9->7->5->3->1

Merge both lists 1->2->3->4->5-


4 alternatively >6->7->8->9 1->3->5->7->9 9->7->5->3->1 1->9

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- 7->5->3->1->2->4- 1->9-


>4->6->8 >6->8 >7

1->3->5->7->9->2- 5->3->1->2->4->6-
>4->6->8 >8

1->3->5->7->9->2- 5->3->1->2->4->6- 1->9-


>4->6->8 >8 >7->5

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:

 Add a new section in a store (toys, grocery, fruits….)


 Store an item in a particular section of a particular store.
 Remove an item in a particular section of a particular store.
 Display the list of all items of a particular section of a store.
 Display the list of items for a given store.

Code:

#include <iostream>
#include <string>
using namespace std;
struct ShoeNode {
string name;
int pieces;
int size;
string color;
ShoeNode* prev;
ShoeNode* next;
};

// Function to create a new shoe node


ShoeNode* createShoeNode(string name, int pieces, int size, string color) {
ShoeNode* newNode = new ShoeNode;
newNode->name = name;
newNode->pieces = pieces;
newNode->size = size;
newNode->color = color;
newNode->prev = nullptr;
newNode->next = nullptr;
return newNode;
}
// Function to add a new shoe to the store
void addShoe(ShoeNode*& head, string name, int pieces, int size, string color) {
ShoeNode* newNode = createShoeNode(name, pieces, size, color);
if (head == nullptr) {
head = newNode;
} else {
ShoeNode* current = head;
while (current->next != nullptr) {

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;
}
}

// Function to delete a shoe from the store


void deleteShoe(ShoeNode*& head, string name) {
if (head == nullptr) {
cout << "Shoe not found!" << endl;
return;
}
if (head->name == name) {
ShoeNode* temp = head;
head = head->next;
if (head != nullptr) {
head->prev = nullptr;
}
delete temp;
cout << "Shoe deleted successfully!" << endl;
return;
}
ShoeNode* current = head;
while (current != nullptr && current->name != name) {
current = current->next;
}
if (current == nullptr) {
cout << "Shoe not found!" << endl;
return;
}

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;
}

// Function to display all shoes in the store


void displayShoes(const ShoeNode* head) {
if (head == nullptr) {
cout << "No shoes found in the store." << endl;
return;
}
cout << "Shoes in the store:" << endl;
const ShoeNode* current = head;
while (current != nullptr) {
cout << "Name: " << current->name << ", Pieces: " << current->pieces << ", Size: " << current-
>size << ", Color: " << current->color << endl;
current = current->next;
}
}
// Function to free memory allocated to the linked list
void deleteLinkedList(ShoeNode*& head) {
ShoeNode* current = head;
while (current != nullptr) {
ShoeNode* temp = current;
current = current->next;
delete temp;
}
head = nullptr;
}
int main() {
ShoeNode* happyFeet = nullptr;
ShoeNode* theShoeSpot = nullptr;
ShoeNode* shoeArtistry = nullptr;
ShoeNode* lacesOfHappiness = nullptr;

// Adding shoes for each store


// Happy Feet
addShoe(happyFeet, "kid", 100, 24, "red");
addShoe(happyFeet, "women", 100, 38, "blue");
// The Shoe Spot
addShoe(theShoeSpot, "kid", 100, 22, "green");
addShoe(theShoeSpot, "men", 100, 43, "black");
// ShoeArtistry
addShoe(shoeArtistry, "men", 100, 42, "brown");

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;
}

Step Operation happyFeet theShoeSpot shoeArtistry lacesOfHappiness current


Initialize store linked
1 list pointers nullptr nullptr nullptr nullptr

2 Add shoes to each store kid->women kid->men men->women kid->men


(100, 22, (100, 42,
(100, 24, red), green), brown), (100, 25, pink),
(100, 43, (100, 37,
(100, 38, blue) black) yellow) (100, 44, brown)
Search for a shoe in a
3 store kid
Name: kid, Pieces: 100,
Size: 24, Color: red
Free memory for each
4 store linked list nullptr nullptr nullptr nullptr

Dry Run:

37
COMSATS University Islamabad
This experiment leads to the
description of logic gates; NAND,
AND, NOT, NOR, OR and
XOR gate.

38

You might also like