DSA Assignment 1 w1
DSA Assignment 1 w1
Assignment No. 01
Muhammad Abubaker
F2023065223
Q1: It is required to make a list of students registered in Data Structures course.
To achieve this functionality, develop a C++ program/Java and use linked list to store Employee ids and
names. Each node in the linked list will contain three items: employee id, employee name, pointer to
next node. When the program starts, it should display the following menu:
Code:
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
int id;
string name;
Employee* next;
if (head == nullptr) {
head = newEmployee;
} else {
Employee* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newEmployee;
}
cout << "Employee added successfully!" << endl;
}
void searchEmployeeById() {
int id;
cout << "Enter Employee ID to search: ";
cin >> id;
Employee* temp = head;
while (temp != nullptr) {
if (temp->id == id) {
cout << "Employee found!" << endl;
cout << "ID: " << temp->id << endl;
cout << "Name: " << temp->name << endl;
return;
}
temp = temp->next;
}
cout << "Employee not found!" << endl;
}
int main() {
int choice;
while (true) {
cout << "Data Structures Course Student List" << endl;
cout << "-----------------------------------" << endl;
cout << "1. Enter Employee Information" << endl;
cout << "2. Search Employee by ID" << endl;
cout << "3. Search Employee by Name" << endl;
cout << "4. Delete Employee Information" << endl;
cout << "5. Update Employee Information" << endl;
cout << "6. Print All Employees" << endl;
cout << "7. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
enterEmployee();
break;
case 2:
searchEmployeeById();
break;
case 3:
searchEmployeeByName();
break;
case 4:
deleteEmployee();
break;
case 5:
updateEmployee();
break;
case 6:
printEmployees();
break;
case 7:
cout << "Exiting program." << endl;
return 0;
default:
cout << "Invalid choice! Please enter a number between 1 and 7." << endl;
}
}
return 0;
}
3579
List 2:1 2 4 8
Output: 3 1 2 5 4 7 8 9
First element from the first list and first element of the second list then second element of
the first list and second element of the second list and so on.
Code:
#include <iostream>
using namespace std;
class Node {
public:
int A;
Node* next;
Node(int A)
this->A = A;
this->next = nullptr;
}
};
int main() {
Node* list1 = nullptr;
Node* list2 = nullptr;
append(&list1, 3);
append(&list1, 5);
append(&list1, 7);
append(&list1, 9);
append(&list2, 1);
append(&list2, 2);
append(&list2, 4);
append(&list2, 8);
return 0;
}
Q3 (a): Intersection of Link Lists Given two sorted lists, L1 and L2, write a procedure to
compute L1 ∩ L2 Intersection. Hint: Take a Link List of your choice
Example List A = (1,2,3) and List B = (3,4,5) their intersection is A ∩ B = (3).
Code:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
int main() {
Node* list1 = nullptr;
Node* list2 = nullptr;
// List A = (1, 2, 3)
append(&list1, 1);
append(&list1, 2);
append(&list1, 3);
// List B = (3, 4, 5)
append(&list2, 3);
append(&list2, 4);
append(&list2, 5);
return 0;
}
Q3 (b): Union of Link Lists Given two sorted lists, L1 and L2, write a procedure to
compute L1 u L2 Intersection. Hint: Take a Link List of your choice
Example List A = (1,2,3) and List B = (4,5,6) their union
Code:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
void append(Node** head, int newData) {
Node* newNode = new Node(newData);
Node* last = *head;
if (*head == nullptr) {
*head = newNode;
return;
}
while (last->next != nullptr) {
last = last->next;
}
last->next = newNode;
}
while (a != nullptr) {
append(lastPtrRef, a->data);
a = a->next;
lastPtrRef = &((*lastPtrRef)->next);
}
while (b != nullptr) {
append(lastPtrRef, b->data);
b = b->next;
lastPtrRef = &((*lastPtrRef)->next);
}
return result;
}
int main() {
Node* list1 = nullptr;
Node* list2 = nullptr;
// List A = (1, 2, 3)
append(&list1, 1);
append(&list1, 2);
append(&list1, 3);
// List B = (3, 4, 5)
append(&list2, 3);
append(&list2, 4);
append(&list2, 5);
return 0;
}
Q4: Sorting of Link List You are given a Linked List. Write an Insert Sort() function which
sorts the nodes in ascending order. Example: List A = { 8,9,1,2} which be sorted in to List
Code:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
Node* C = *head;
while (C != nullptr) {
Node* next = current->next;
if (S == nullptr || S->data >= C->data) {
C->next = sorted;
S = current;
} else {
Node* temp = sorted;
while (temp->next != nullptr && temp->next->data < C->data) {
temp = temp->next;
}
C->next = temp->next;
temp->next = C;
}
// Update current
C = next;
}
int main() {
Node* head = nullptr;
// List A = {8, 9, 1, 2}
appendOnRear(&head, 8);
appendOnRear(&head, 9);
appendOnRear(&head, 1);
appendOnRear(&head, 2);
return 0;
}
Q5: Removal of Duplicates in Link List. You are given a Linked List. Write a Remove All
Duplicates () function which takes a list sorted in an ascending and deletes any duplicate
nodes from the list. (Ideally, the list should only be traversed once)
Example: List A=4,1,1,2,2,3,3,3} which be sorted in to List A ={4,1,2,3}
Code:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
int main() {
Node* head = nullptr;
// List A = {4, 1, 1, 2, 2, 3, 3, 3}
appendOnRear(&head, 4);
appendOnRear(&head, 1);
appendOnRear(&head, 1);
appendOnRear(&head, 2);
appendOnRear(&head, 2);
appendOnRear(&head, 3);
appendOnRear(&head, 3);
appendOnRear(&head, 3);
removeAllDuplicates(head);
return 0;
}
Q7: Theoretical Part: Explore sentinel Link list by yourself for insertion and deletion
Code:
#include <iostream>
public:
int data;
Node* next;
this->data = data;
this->next = nullptr;
};
class SentinelList {
public:
Node* head;
SentinelList() {
temp = temp->next;
temp->next = newNode;
if (temp->next->data == value) {
temp->next = temp->next->next;
delete nodeToDelete;
return;
temp = temp->next;
void printList() {
temp = temp->next;
};
int main() {
SentinelList list;
// List A = {8, 9, 1, 2}
list.appendOnRear(8);
list.appendOnRear(9);
list.appendOnRear(1);
list.appendOnRear(2);
list.printList();
// Deleting an element
list.deleteNode(1);
list.deleteNode(5);
list.printList();
return 0;