SUBMITTED BY: NOOR FATIMA
SUBMITTED TO: SIR NADEEM
REG NO. 4678-FOC/BSCS/F22
DATA STRUCTURES AND ALGORITHMS
PROGRAM:
// singly linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node (int value) : data(value) , next(NULL) {
cout << "A node with value " << data << " is created." << endl;
}
~Node() {
cout << "Deleting node with data " << data << endl;
};
class SinglyLinkedList {
private:
Node* head;
public:
SinglyLinkedList() : head(NULL) {}
~SinglyLinkedList() {
// Destructor to free memory
Node* current = head;
while (current != NULL) {
Node* temp = current;
current = current->next;
delete temp;
}
cout <<endl<< "**** LINKED LIST IS DELETED ****" << endl;
}
void createNode(int value) {
Node* newNode = new Node(value);
if (head == NULL) {
head = newNode;
}
else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void insertAtBeginning(int value) {
Node* newNode = new Node(value);
if (head == NULL) {
head = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
}
void insertAtEnd(int value) {
Node* newNode = new Node(value);
if (head == NULL) {
head = newNode;
}
else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void insertAtPosition(int value, int position) {
if (position == 1) {
insertAtBeginning(value);
return;
}
Node* newNode = new Node(value);
Node* temp = head;
for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
cout << "Position out of bounds" << endl;
//delete newNode;
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
void deleteFromBeginning() {
if (head == NULL) {
cout << "List is empty! Deletion is not possible." << endl;
return;
}
Node* temp = head;
head = head->next;
delete temp;
}
void deleteFromEnd() {
if (head == NULL) {
cout << "List is empty! Deletion is not possible." << endl;
return;
}
//only single node
if (head->next == NULL) {
delete head; // deallocating / deleting the memory that head points to
head = NULL;
return;
}
Node* temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
delete temp->next;
temp->next = NULL;
}
void deleteAtSpecificPosition(int position) {
if (head == NULL) {
cout << "List is empty! Deletion is not possible." << endl;
return;
}
if (position == 1) {
deleteFromBeginning();
return;
}
Node* temp = head;
Node* prev = NULL;
for (int i = 1; i < position && temp != NULL; i++) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
cout << "Position out of bounds" << endl;
return;
}
prev->next = temp->next;
delete temp;
}
void deleteSpecificNode(int value) {
if (head == NULL) {
cout << "List is empty! Deletion is not possible." << endl;
return;
}
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* temp = head;
while (temp->next != NULL && temp->next->data != value) {
temp = temp->next;
}
if (temp->next == NULL) {
cout << "Node not found." << endl;
return;
}
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
}
void search(int value) {
if (head == NULL) {
cout << "List is Empty!!!" << endl;
return;
}
Node* temp = head;
int pos = 1;
while (temp != NULL) {
if (temp->data == value) {
cout << "Value " << value << " found at position " << pos << endl;
return;
}
temp = temp->next;
pos++;
}
cout << "Value not found in the list." << endl;
}
void display() {
if (head == NULL) {
cout << "List is Empty!!!" << endl;
return;
}
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};
int main() {
SinglyLinkedList list;
int choice, value, position;
do {
cout << "\nMenu:\n";
cout << "1. Create Node\n";
cout << "2. Insert at Beginning\n";
cout << "3. Insert at End\n";
cout << "4. Insert at Specific Position\n";
cout << "5. Delete from Beginning\n";
cout << "6. Delete from End\n";
cout << "7. Delete at Specific Position\n";
cout << "8. Delete a Specific Node\n";
cout << "9. Search for a Value\n";
cout << "10. Display List\n";
cout << "11. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to create node: ";
cin >> value;
list.createNode(value);
break;
case 2:
cout << "Enter value to insert at beginning: ";
cin >> value;
list.insertAtBeginning(value);
break;
case 3:
cout << "Enter value to insert at end: ";
cin >> value;
list.insertAtEnd(value);
break;
case 4:
cout << "Enter value which you want to insert: ";
cin >> value;
cout << "Enter position at which you want to insert the value: ";
cin >> position;
list.insertAtPosition(value, position);
break;
case 5:
list.deleteFromBeginning();
break;
case 6:
list.deleteFromEnd();
break;
case 7:
cout << "Enter position of the node to delete: ";
cin >> position;
list.deleteAtSpecificPosition(position);
break;
case 8:
cout << "Enter value of the node to delete: ";
cin >> value;
list.deleteSpecificNode(value);
break;
case 9:
cout << "Enter value to search: ";
cin >> value;
list.search(value);
break;
case 10:
list.display();
break;
case 11:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 11);
return 0;
}
OUTPUT: