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

Assignmen

t
Name
Muhammad Hamza
Roll No
F22NSEEN1M01006
Submitted to
Dr. Hina Afreen

Department of Software Engineering


The Islamia university of Bahawalpur
( BWN CAMPUS)
1. Program to Insert a node at the begging of the linked list.

#include <iostream>

using namespace std;

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

// Linked list class


class LinkedList {
public:
LinkedList() : head(nullptr) {}

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


void insertAtBeginning(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = head;
head = newNode;
}

// Function to display the linked list


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

private:
Node* head;
};

int main() {
LinkedList linkedList;

// Initialize the linked list with five nodes


for (int i = 5; i >= 1; --i) {
linkedList.insertAtBeginning(i * 10); // Insert at the beginning
}

// Display the linked list


cout << "Linked List after initialization:" << endl;
linkedList.displayList();

// Get user input for additional nodes


int value;
cout << "Enter the data for the node to insert at the beginning: ";
cin >> value;

// Insert a node at the beginning based on user input


linkedList.insertAtBeginning(value);

// Display the updated linked list


cout << "Linked List after insertion:" << endl;
linkedList.displayList();

return 0;
}

2. Program to Insert a node to a specific position of the linked list.

#include <iostream>

using namespace std;

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

// Linked list class


class LinkedList {
public:
LinkedList() : head(nullptr) {}

// Function to insert a node at a specific position in the linked list


void insertAtPosition(int data, int position) {
Node* newNode = new Node;
newNode->data = data;
if (position == 1 || head == nullptr) {
// Insert at the beginning
newNode->next = head;
head = newNode;
} else {
// Insert at a specific position
Node* current = head;
for (int i = 1; i < position - 1 && current != nullptr; ++i) {
current = current->next;
}

if (current != nullptr) {
newNode->next = current->next;
current->next = newNode;
} else {
cout << "Invalid position. Node not inserted." << endl;
}
}
}

// Function to display the linked list


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

private:
Node* head;
};

int main() {
LinkedList linkedList;

// Initialize the linked list with five nodes


for (int i = 5; i >= 1; --i) {
linkedList.insertAtPosition(i * 10, 1); // Insert at the beginning
}

// Display the linked list


cout << "Linked List after initialization:" << endl;
linkedList.displayList();

// Get user input for additional nodes


int numNodes;
cout << "Enter the number of nodes to insert: ";
cin >> numNodes;

// Insert nodes at a specific position based on user input


for (int i = 0; i < numNodes; ++i) {
int value, position;
cout << "Enter the data for the node: ";
cin >> value;
cout << "Enter the position to insert the node: ";
cin >> position;
linkedList.insertAtPosition(value, position);
}

// Display the updated linked list


cout << "Linked List after insertion:" << endl;
linkedList.displayList();

return 0;
}

3. Program to Insert a node at the end of the linked list.

#include <iostream>

using namespace std;

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

// Linked list class


class LinkedList {
public:
LinkedList() : head(nullptr) {}

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


void insertAtEnd(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = nullptr;

if (head == nullptr) {
// If the list is empty, make the new node the head
head = newNode;
} else {
// Traverse the list to find the last node and append the new node
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
}

// Function to display the linked list


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

private:
Node* head;
};

int main() {
LinkedList linkedList;

// Initialize the linked list with five nodes


for (int i = 1; i <= 5; ++i) {
linkedList.insertAtEnd(i * 10); // Insert at the end
}

// Display the linked list


cout << "Linked List after initialization:" << endl;
linkedList.displayList();

// Get user input for additional nodes


int value;
cout << "Enter the data for the node to insert at the end: ";
cin >> value;

// Insert a node at the end based on user input


linkedList.insertAtEnd(value);

// Display the updated linked list


cout << "Linked List after insertion:" << endl;
linkedList.displayList();

return 0;
}

4. Program to delete a node from start of the linked list.

#include <iostream>

using namespace std;

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

// Linked list class


class LinkedList {
public:
LinkedList() : head(nullptr) {}

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


void insertAtBeginning(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = head;
head = newNode;
}

// Function to delete a node from the beginning of the linked list


void deleteFromStart() {
if (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
} else {
cout << "List is empty. Cannot delete from start." << endl;
}
}

// Function to display the linked list


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

// Destructor to free memory when the object is destroyed


~LinkedList() {
// Delete all nodes in the linked list
Node* current = head;
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}
head = nullptr;
}

private:
Node* head;
};

int main() {
LinkedList linkedList;

// Initialize the linked list with five nodes


for (int i = 5; i >= 1; --i) {
linkedList.insertAtBeginning(i * 10); // Insert at the beginning
}

// Display the linked list


cout << "Linked List after initialization:" << endl;
linkedList.displayList();

// Get user input for the number of nodes to delete


int numNodesToDelete;
cout << "Enter the number of nodes to delete from the beginning: ";
cin >> numNodesToDelete;

// Delete nodes from the beginning based on user input


for (int i = 0; i < numNodesToDelete; ++i) {
linkedList.deleteFromStart();
}

// Display the updated linked list


cout << "Linked List after deletion from start:" << endl;
linkedList.displayList();

return 0;
}

5. Program to delete a node from a specific position of the linked list.

#include <iostream>

using namespace std;

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

// Linked list class


class LinkedList {
public:
LinkedList() : head(nullptr) {}

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


void insertAtBeginning(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = head;
head = newNode;
}

// Function to delete a node from a specific position in the linked list


void deleteAtPosition(int position) {
if (position < 1) {
cout << "Invalid position. Node not deleted." << endl;
return;
}

Node* current = head;


Node* previous = nullptr;

// Traverse to the specified position


for (int i = 1; i < position && current != nullptr; ++i) {
previous = current;
current = current->next;
}

// If the position is valid, delete the node


if (current != nullptr) {
if (previous != nullptr) {
previous->next = current->next;
} else {
// If deleting the first node
head = current->next;
}

delete current;
} else {
cout << "Invalid position. Node not deleted." << endl;
}
}

// Function to display the linked list


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

// Destructor to free memory when the object is destroyed


~LinkedList() {
// Delete all nodes in the linked list
Node* current = head;
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}
head = nullptr;
}

private:
Node* head;
};

int main() {
LinkedList linkedList;

// Initialize the linked list with five nodes


for (int i = 5; i >= 1; --i) {
linkedList.insertAtBeginning(i * 10); // Insert at the beginning
}

// Display the linked list


cout << "Linked List after initialization:" << endl;
linkedList.displayList();

// Get user input for the position to delete


int position;
cout << "Enter the position to delete a node: ";
cin >> position;

// Delete a node from a specific position based on user input


linkedList.deleteAtPosition(position);

// Display the updated linked list


cout << "Linked List after deletion at position:" << endl;
linkedList.displayList();

return 0;
}

6. Program to delete a node from the of the linked list.

#include <iostream>

using namespace std;

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

// Linked list class


class LinkedList {
public:
LinkedList() : head(nullptr) {}

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


void insertAtBeginning(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = head;
head = newNode;
}

// Function to delete a node from the end of the linked list


void deleteFromEnd() {
if (head == nullptr) {
cout << "List is empty. Cannot delete from end." << endl;
return;
}

if (head->next == nullptr) {
// If there is only one node in the list
delete head;
head = nullptr;
return;
}

Node* current = head;


Node* previous = nullptr;

// Traverse to the end of the list


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

// Delete the last node


delete current;
if (previous != nullptr) {
previous->next = nullptr;
} else {
// If deleting the only node
head = nullptr;
}
}

// Function to display the linked list


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

// Destructor to free memory when the object is destroyed


~LinkedList() {
// Delete all nodes in the linked list
Node* current = head;
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}
head = nullptr;
}

private:
Node* head;
};

int main() {
LinkedList linkedList;

// Initialize the linked list with five nodes


for (int i = 5; i >= 1; --i) {
linkedList.insertAtBeginning(i * 10); // Insert at the beginning
}

// Display the linked list


cout << "Linked List after initialization:" << endl;
linkedList.displayList();

// Get user input for the number of nodes to delete from the end
int numNodesToDelete;
cout << "Enter the number of nodes to delete from the end: ";
cin >> numNodesToDelete;

// Delete nodes from the end based on user input


for (int i = 0; i < numNodesToDelete; ++i) {
linkedList.deleteFromEnd();
}

// Display the updated linked list


cout << "Linked List after deletion from end:" << endl;
linkedList.displayList();

return 0;
}

You might also like