0% found this document useful (0 votes)
13 views

DSA LAB 8

The document contains a C++ implementation of a singly linked list with various functionalities such as inserting and deleting nodes at different positions, checking if the list is empty, and printing the list. It defines a Node class for individual elements and a LinkedList class to manage the list operations. The main function provides a menu-driven interface for users to interact with the linked list.

Uploaded by

bsee23008
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

DSA LAB 8

The document contains a C++ implementation of a singly linked list with various functionalities such as inserting and deleting nodes at different positions, checking if the list is empty, and printing the list. It defines a Node class for individual elements and a LinkedList class to manage the list operations. The main function provides a menu-driven interface for users to interact with the linked list.

Uploaded by

bsee23008
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

using namespace std;

class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = nullptr;
}
};

class LinkedList {
private:
Node* head;

public:
LinkedList() {
head = nullptr;
}

void InsertAtFront(int val) {


Node* newNode = new Node(val);
newNode->next = head;
head = newNode;
}

void InsertAtTail(int val) {


Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}

void insertAtAnyNode(int val, int index) {


if (index == 0) {
InsertAtFront(val);
return;
}
Node* newNode = new Node(val);
Node* temp = head;
int count = 0;
while (temp && count < index - 1) {
temp = temp->next;
count++;
}
if (!temp) {
cout << "Index out of bounds!" << endl;
delete newNode;
return;
}
newNode->next = temp->next;
temp->next = newNode;
}

void deleteFromStart() {
if (!head) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
head = head->next;
delete temp;
}

void deleteFromEnd() {
if (!head) {
cout << "List is empty!" << endl;
return;
}
if (!head->next) {
delete head;
head = nullptr;
return;
}
Node* temp = head;
while (temp->next && temp->next->next) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
}

void deleteNodeFromAnyNode(int index) {


if (!head) {
cout << "List is empty!" << endl;
return;
}
if (index == 0) {
deleteFromStart();
return;
}
Node* temp = head;
int count = 0;
while (temp && count < index - 1) {
temp = temp->next;
count++;
}
if (!temp || !temp->next) {
cout << "Index out of bounds!" << endl;
return;
}
Node* nodeToDelete = temp->next;
temp->next = temp->next->next;
delete nodeToDelete;
}

bool IsEmpty() {
return head == nullptr;
}
void PrintLinkedList() {
if (!head) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
while (temp) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

int main() {
LinkedList list;
int choice, val, index;
do {
cout << "Menu:\n1. Insert at Front\n2. Insert at Tail\n3. Insert at Any
Node\n4. Delete from Start\n5. Delete from End\n6. Delete Node from Any Node\n7.
Check if Empty\n8. Print Linked List\n9. Exit\nEnter your choice: ";
cin >> choice;

switch (choice) {
case 1: cout << "Enter value to insert at front: "; cin >> val;
list.InsertAtFront(val); break;
case 2: cout << "Enter value to insert at tail: "; cin >> val;
list.InsertAtTail(val); break;
case 3: cout << "Enter value and index to insert at: "; cin >> val >>
index; list.insertAtAnyNode(val, index); break;
case 4: list.deleteFromStart(); break;
case 5: list.deleteFromEnd(); break;
case 6: cout << "Enter index to delete node from: "; cin >> index;
list.deleteNodeFromAnyNode(index); break;
case 7: if (list.IsEmpty()) cout << "The list is empty.\n"; else cout
<< "The list is not empty.\n"; break;
case 8: list.PrintLinkedList(); break;
case 9: cout << "Exiting program.\n"; break;
default: cout << "Invalid choice! Please try again.\n";
}
} while (choice != 9);
return 0;
}

You might also like