Singly linked list
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() : head(nullptr) {}
// Function to insert a node at the beginning
void insertAtBeginning(int value) {
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
// Function to insert a node at the end
void insertAtEnd(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
return;
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
temp->next = newNode;
// Function to insert a node at a specific position
void insertAtPosition(int value, int position) {
Node* newNode = new Node(value);
if (position == 1) {
newNode->next = head;
head = newNode;
return;
Node* temp = head;
for (int i = 1; i < position - 1 && temp != nullptr; ++i) {
temp = temp->next;
if (temp == nullptr) {
std::cout << "Invalid position\n";
return;
}
newNode->next = temp->next;
temp->next = newNode;
// Function to delete a node at a specific position
void deleteAtPosition(int position) {
if (head == nullptr) {
std::cout << "List is empty\n";
return;
Node* temp = head;
if (position == 1) {
head = head->next;
delete temp;
return;
for (int i = 1; i < position - 1 && temp != nullptr; ++i) {
temp = temp->next;
if (temp == nullptr || temp->next == nullptr) {
std::cout << "Invalid position\n";
return;
Node* nodeToDelete = temp->next;
temp->next = nodeToDelete->next;
delete nodeToDelete;
// Function to display the linked list
void display() {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
std::cout << std::endl;
};
int main() {
LinkedList linkedList;
linkedList.insertAtBeginning(1);
linkedList.insertAtEnd(3);
linkedList.insertAtPosition(2, 2);
std::cout << "Linked List: ";
linkedList.display();
linkedList.deleteAtPosition(2);
std::cout << "Linked List after deletion: ";
linkedList.display();
return 0;
}
Doubly linked list
#include <iostream>
class Node {
public:
int data;
Node* next;
Node* prev;
Node(int value) : data(value), next(nullptr), prev(nullptr) {}
};
class DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
DoublyLinkedList() : head(nullptr), tail(nullptr) {}
// Function to insert a node at the beginning
void insertAtBeginning(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = tail = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
// Function to insert a node at the end
void insertAtEnd(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
// Function to insert a node at a specific position
void insertAtPosition(int value, int position) {
Node* newNode = new Node(value);
if (position == 1) {
insertAtBeginning(value);
return;
Node* temp = head;
for (int i = 1; i < position - 1 && temp != nullptr; ++i) {
temp = temp->next;
}
if (temp == nullptr) {
std::cout << "Invalid position\n";
return;
newNode->next = temp->next;
newNode->prev = temp;
if (temp->next != nullptr) {
temp->next->prev = newNode;
temp->next = newNode;
// Function to delete a node at a specific position
void deleteAtPosition(int position) {
if (head == nullptr) {
std::cout << "List is empty\n";
return;
Node* temp = head;
if (position == 1) {
head = head->next;
if (head != nullptr) {
head->prev = nullptr;
} else {
tail = nullptr;
}
delete temp;
return;
for (int i = 1; i < position - 1 && temp != nullptr; ++i) {
temp = temp->next;
if (temp == nullptr || temp->next == nullptr) {
std::cout << "Invalid position\n";
return;
Node* nodeToDelete = temp->next;
temp->next = nodeToDelete->next;
if (nodeToDelete->next != nullptr) {
nodeToDelete->next->prev = temp;
} else {
tail = temp;
delete nodeToDelete;
// Function to display the doubly linked list
void display() {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
};
int main() {
DoublyLinkedList doublyLinkedList;
doublyLinkedList.insertAtBeginning(1);
doublyLinkedList.insertAtEnd(3);
doublyLinkedList.insertAtPosition(2, 2);
std::cout << "Doubly Linked List: ";
doublyLinkedList.display();
doublyLinkedList.deleteAtPosition(2);
std::cout << "Doubly Linked List after deletion: ";
doublyLinkedList.display();
return 0;
Circular linked list
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
class CircularLinkedList {
private:
Node* head;
public:
CircularLinkedList() : head(nullptr) {}
// Function to insert a node at the beginning
void insertAtBeginning(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
newNode->next = head;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
temp->next = newNode;
newNode->next = head;
head = newNode;
// Function to insert a node at the end
void insertAtEnd(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
newNode->next = head;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
temp->next = newNode;
newNode->next = head;
// Function to insert a node at a specific position
void insertAtPosition(int value, int position) {
Node* newNode = new Node(value);
if (position == 1) {
insertAtBeginning(value);
return;
Node* temp = head;
for (int i = 1; i < position - 1 && temp->next != head; ++i) {
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
}
// Function to delete a node at a specific position
void deleteAtPosition(int position) {
if (head == nullptr) {
std::cout << "List is empty\n";
return;
Node* temp = head;
if (position == 1) {
while (temp->next != head) {
temp = temp->next;
if (head == temp) {
delete head;
head = nullptr;
} else {
Node* nodeToDelete = head;
head = head->next;
temp->next = head;
delete nodeToDelete;
return;
for (int i = 1; i < position - 1 && temp->next != head; ++i) {
temp = temp->next;
}
if (temp->next == head) {
std::cout << "Invalid position\n";
return;
Node* nodeToDelete = temp->next;
temp->next = nodeToDelete->next;
delete nodeToDelete;
// Function to display the circular linked list
void display() {
if (head == nullptr) {
std::cout << "List is empty\n";
return;
Node* temp = head;
do {
std::cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
std::cout << std::endl;
};
int main() {
CircularLinkedList circularLinkedList;
circularLinkedList.insertAtBeginning(1);
circularLinkedList.insertAtEnd(3);
circularLinkedList.insertAtPosition(2, 2);
std::cout << "Circular Linked List: ";
circularLinkedList.display();
circularLinkedList.deleteAtPosition(2);
std::cout << "Circular Linked List after deletion: ";
circularLinkedList.display();
return 0;