Name: M Abdulsalam Roll No:231762 Task 1: Single Linked List
Name: M Abdulsalam Roll No:231762 Task 1: Single Linked List
Roll no:231762
Task 1:
Single Linked List:
#include <iostream>
using namespace std;
// Define a Node
struct Node {
int data; /
Node* next; // pointer to the next node
};
public:
// Constructor
SinglyLinkedList() {
head = nullptr; // list starts empty
}
if (head == nullptr) { // if the list is empty, the new node is the head
head = newNode;
}
else {
Node* temp = head;
while (temp->next != nullptr) { // go to the end of the list
temp = temp->next;
}
temp->next = newNode; // link the last node to the new node
}
}
if (head->data == value) {
Node* toDelete = head;
head = head->next;
delete toDelete;
return;
}
// If found, delete it
if (temp->next != nullptr) {
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
}
}
list.addAtStart(10);
list.addAtEnd(20);
list.addAfter(10, 15);
list.searchValue(15);
list.printList(); /
list.deleteValue(10);
list.printList();
return 0;
}
// Define a Node
struct Node {
int data; // the value stored in the node
Node* next; // pointer to the next node
Node* prev; // pointer to the previous node
};
if (head == nullptr) { // if the list is empty, the new node is the head
newNode->prev = nullptr;
head = newNode;
}
else {
Node* temp = head;
while (temp->next != nullptr) { // go to the last node
temp = temp->next;
}
temp->next = newNode; // link last node to new node
newNode->prev = temp; // link new node back to the last node
}
}
// If found, delete it
if (temp != nullptr) {
if (temp->next != nullptr) {
temp->next->prev = temp->prev;
}
if (temp->prev != nullptr) {
temp->prev->next = temp->next;
}
delete temp;
}
}
list.deleteValue(10); // Delete 10
list.printList(); // Print again
return 0;
}
struct Node {
int data;
Node* next;
};
public:
// Constructor
SingleCircularLinkedList() {
head = nullptr; // List starts empty
}
cout << "Value " << afterValue << " not found.\n";
}
cout << "Value " << value << " not found.\n";
}
list.addAtStart(10);
list.addAtEnd(20);
list.addAfter(10, 15);
list.searchValue(15);
list.printList();
list.deleteValue(10);
list.printList();
return 0;
}
struct Node {
int data;
Node* next;
Node* prev;
};
public:
// Constructor
DoubleCircularLinkedList() {
head = nullptr; // List starts empty
}
if (head == nullptr) {
newNode->next = newNode;
newNode->prev = newNode;
head = newNode;
}
else {
Node* last = head->prev;
newNode->next = head;
newNode->prev = last;
last->next = newNode;
head->prev = newNode;
}
}
do {
if (temp->data == afterValue) {
Node* newNode = new Node;
newNode->data = newValue;
newNode->next = temp->next;
newNode->prev = temp;
temp->next->prev = newNode;
temp->next = newNode;
return;
}
temp = temp->next;
} while (temp != head);
cout << "Value " << afterValue << " not found.\n";
}
// Search for a value in the list
void searchValue(int value) {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}
cout << "Value " << value << " not found.\n";
}
// If found, delete it
if (temp->data == value) {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
}
}
list.addAtStart(10);
list.addAtEnd(20);
list.addAfter(10, 15);
list.searchValue(15);
list.printList();
list.deleteValue(10);
list.printList();
return 0;
}