DATA STRUCTURES
Assignment 1
Submitted by: Zunaira Irshad
Submitted to: Ma’am Sadia Sahar
Course code: CSC-211
Roll no: 2324
Class: BSCS 3rd M(A)
OCTOBER 25, 2024
1
Question 1: Insertion
Code:
#include <iostream>
using namespace std;
struct Node { // Define the structure of a Node
int data;
Node* next;
Node(int val) : data(val), next(NULL) {} // Constructor for node initialization
};
class SinglyLinkedList { // Define the structure of a Singly Linked List
private:
Node* head;
public:
SinglyLinkedList() : head(NULL) {} // Constructor to initialize an empty list
void printList() { // Function to print the linked list
Node* temp = head;
while (temp != NULL) {
cout << temp->data << "->";
temp = temp->next;
cout << "NULL" << endl;
void insertAtBeginning(int value) { // A. Insert a node at the beginning
Node* newNode = new Node(value);
newNode->next = head;
2
head = newNode;
void insertAtEnd(int value) { // B. Insert a node at the end
Node* newNode = new Node(value);
if (head == NULL) {
head = newNode;
return;
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
temp->next = newNode;
void insertAtPosition(int value, int k) { // C. Insert a node at a given position k (1-based
index)
if (k <= 0) {
cout << "Position should be >= 1" << endl;
return;
Node* newNode = new Node(value);
if (k == 1) { // Insert at the beginning
newNode->next = head;
head = newNode;
return;
3
}
Node* temp = head;
int count = 1;
// Traverse to the (k-1)th node
while (temp != NULL && count < k - 1) {
temp = temp->next;
count++; }
if (temp == NULL) {
cout << "Position " << k << " is out of bounds." << endl;
return;
newNode->next = temp->next;
temp->next = newNode;
}};
int main() {
SinglyLinkedList list;
list.insertAtEnd(1);
list.insertAtEnd(2);
list.insertAtEnd(5);
list.insertAtEnd(7);
list.insertAtEnd(4);
cout << "Original List:" << endl;
list.printList();
cout << "\nAfter inserting 8 at the beginning:" << endl;
list.insertAtBeginning(8);
4
list.printList();
cout << "\nAfter inserting 8 at the end:" << endl;
list.insertAtEnd(8);
list.printList();
cout << "\nAfter inserting 8 at position 4:" << endl;
list.insertAtPosition(8, 4);
list.printList();
return 0;
Output:
Algorithm:
1. Create a linked list and initialize it by inserting nodes at the end to form the
sequence: `1 -> 2 -> 5 -> 7 -> 4 -> NULL`.
2. Insert node 8 at the beginning of the list, making the list: `8 -> 1 -> 2 -> 5 -> 7 -
> 4 -> NULL`.
3. Insert node 8 at the end of the list, resulting in: `1 -> 2 -> 5 -> 7 -> 4 -> 8 ->
NULL`.
4. Insert node 8 at position 4, updating the list to: `1 -> 2 -> 5 -> 8 -> 7 -> 4 ->
NULL`.
5
QUESTION 2: DELETION
Code:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void printList(Node* head) {
Node* current = head;
while (current !=nullptr) {
cout << current->data << "->";
current = current->next;
cout << "NULL" << endl;
void deleteAtBeginning(Node*& head) {
if (head == nullptr) return; // Empty list
Node* temp = head;
head = head->next;
delete temp;
void deleteAtEnd(Node*& head) {
if (head == nullptr) return; // Empty list
if (head->next == nullptr) { // Only one node
6
delete head;
head = nullptr;
return;
Node* current = head;
while (current->next->next != nullptr) {
current = current->next;
delete current->next;
current->next = nullptr;
void deleteAtPosition(Node*& head, int k) {
if (head == nullptr || k <= 0) return; // Empty list or invalid position
if (k == 1) { // If position is 1, delete the first node
deleteAtBeginning(head);
return;
Node* current = head;
int count = 1;
// Traverse to the node just before position k
while (current != nullptr && count < k - 1) {
current = current->next;
count++;
if (current == nullptr || current->next == nullptr) return; // k is out of bounds
7
Node* temp = current->next;
current->next = current->next->next;
delete temp;
// Function to insert a new node at the end (used to create the list)
void insertAtEnd(Node*& head, int value) {
Node* newNode = new Node{value, nullptr};
if (head == nullptr) {
head = newNode;
return;
Node* current = head;
while (current->next != nullptr) {
current = current->next;
current->next = newNode;
int main() {
Node* head = nullptr;
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 5);
insertAtEnd(head, 7);
insertAtEnd(head, 4);
cout << "Original List:" << endl;
8
printList(head);
cout << "\nAfter deleting at the beginning:" << endl;
deleteAtBeginning(head);
printList(head);
cout << "\nAfter deleting at the end:" << endl;
deleteAtEnd(head);
printList(head);
cout << "\nAfter deleting at position 3:" << endl;
deleteAtPosition(head, 3);
printList(head);
return 0;
Output:
Algorithm:
1. Traverse the list until the last node and append the new node at the end.
2. Update the head to point to the second node and delete the first node.
9
3. Traverse to the second-last node, update its `next` to `nullptr`, and delete the
last node.
4. Traverse to the node just before the desired position, update the link to skip the
node at the target position, and delete that node.
QUESTION 3:
Code:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void printList(Node* head) {
Node* current = head;
while (current != nullptr) {
cout << current->data << "->";
current = current->next;
cout << "NULL" << endl;
void reverseList(Node*& head) {
Node* prev = nullptr;
Node* current = head;
Node* next = nullptr;
while (current != nullptr) {
10
next = current->next; // Store the next node
current->next = prev; // Reverse the current node's next pointer
prev = current; // Move prev and current one step forward
current = next;
head = prev; // Update the head to the new first node
void insertAtEnd(Node*& head, int value) {
Node* newNode = new Node{value, nullptr};
if (head == nullptr) {
head = newNode;
return;
Node* current = head;
while (current->next != nullptr) {
current = current->next;
current->next = newNode;
int main() {
Node* head = nullptr;
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 5);
insertAtEnd(head, 7);
11
insertAtEnd(head, 4);
cout << "Original List:" << endl;
printList(head);
reverseList(head);
cout << "\nReversed List:" << endl;
printList(head);
return 0;
Output:
Algorithm:
1. Initialize three pointers: prev (set to nullptr), current (set to head), and next.
2. Traverse the list: for each node, store the next node, reverse the current node’s
next pointer to point to prev, and move prev and current forward.
3. Continue until current becomes nullptr.
4. Update the head of the list to prev (the new head).
5. The list is now reversed.
Question 4:
Code:
#include <iostream>
using namespace std;
12
struct Node {
int data;
Node* next;
};
void insertAtEnd(Node*& head, int value) {
Node* newNode = new Node{value, nullptr};
if (head == nullptr) {
head = newNode;
return;
Node* current = head;
while (current->next != nullptr) {
current = current->next;
current->next = newNode;
void printList(Node* head) {
Node* current = head;
while (current != nullptr) {
cout << current->data << "->";
current = current->next;
cout << "NULL" << endl;
void findMaxMinAndDifference(Node* head) {
13
if (head == nullptr) {
cout << "The list is empty." << endl;
return;
int maxValue = head->data;
int minValue = head->data;
Node* current = head;
while (current != nullptr) {
if (current->data > maxValue) {
maxValue = current->data;
if (current->data < minValue) {
minValue = current->data;
current = current->next;
int difference = maxValue - minValue;
cout << "Maximum value: " << maxValue << endl;
cout << "Minimum value: " << minValue << endl;
cout << "Difference (Maximum - Minimum): " << difference << endl;
int main() {
Node* head = nullptr;
insertAtEnd(head, 1);
insertAtEnd(head, 2);
14
insertAtEnd(head, 5);
insertAtEnd(head, 7);
insertAtEnd(head, 4);
cout << "Original List:" << endl;
printList(head);
findMaxMinAndDifference(head);
return 0;
Output:
Algorithm:
1. Initialize two variables maxValue and minValue with the head node's data.
2. Traverse the linked list, updating maxValue if a node's data is larger, and
minValue if it is smaller.
3. After finding the maxValue and minValue, calculate the difference
(maxValue - minValue).
4. Output the maximum, minimum, and difference values.
15
16