0% found this document useful (0 votes)
25 views12 pages

Name: M Abdulsalam Roll No:231762 Task 1: Single Linked List

Uploaded by

231762
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views12 pages

Name: M Abdulsalam Roll No:231762 Task 1: Single Linked List

Uploaded by

231762
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Name: M Abdulsalam

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
};

// Define a Linked List


class SinglyLinkedList {
private:
Node* head; // head pointer to the first node

public:
// Constructor
SinglyLinkedList() {
head = nullptr; // list starts empty
}

// Add value at the start


void addAtStart(int value) {
Node* newNode = new Node; // create a new node
newNode->data = value; // store the value
newNode->next = head; // point to current first node
head = newNode; // update head to point to the new node
}

// Add value at the end


void addAtEnd(int value) {
Node* newNode = new Node; // create a new node
newNode->data = value;
newNode->next = nullptr; // end node points to nothing

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
}
}

// Add a value after a specific node


void addAfter(int afterValue, int newValue) {
Node* temp = head;

// Find the node with 'afterValue'


while (temp != nullptr && temp->data != afterValue) {
temp = temp->next;
}

// If found, add a new node after it


if (temp != nullptr) {
Node* newNode = new Node;
newNode->data = newValue;
newNode->next = temp->next; // link new node to the next one
temp->next = newNode; // link current node to new node
}
}

void searchValue(int value) {


Node* temp = head;

while (temp != nullptr) {


if (temp->data == value) {
cout << "Value " << value << " found." << endl;
return;
}
temp = temp->next;
}
cout << "Value " << value << " not found." << endl;
}

void deleteValue(int value) {


// If the list is empty, do nothing
if (head == nullptr) {
return;
}

if (head->data == value) {
Node* toDelete = head;
head = head->next;
delete toDelete;
return;
}

Node* temp = head;


while (temp->next != nullptr && temp->next->data != value) {
temp = temp->next;
}

// If found, delete it
if (temp->next != nullptr) {
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
}
}

// Print the entire list


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

// Main function to test the linked list


int main() {
SinglyLinkedList list;

list.addAtStart(10);
list.addAtEnd(20);
list.addAfter(10, 15);
list.searchValue(15);

list.printList(); /

list.deleteValue(10);
list.printList();

return 0;
}

Double Linked List:


#include <iostream>
using namespace std;

// 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
};

// Define a Doubly Linked List


class DoublyLinkedList {
private:
Node* head; // head pointer to the first node
public:
// Constructor
DoublyLinkedList() {
head = nullptr; // list starts empty
}

// Add value at the start


void addAtStart(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->prev = nullptr; // no previous node (it's the head)
newNode->next = head; // point to the current first node

if (head != nullptr) { // if there's already a node, link it back


head->prev = newNode;
}
head = newNode; // update head to point to the new node
}

// Add value at the end


void addAtEnd(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = nullptr; // end node points to nothing

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
}
}

// Add a value after a specific node


void addAfter(int afterValue, int newValue) {
Node* temp = head;

// Find the node with 'afterValue'


while (temp != nullptr && temp->data != afterValue) {
temp = temp->next;
}

// If found, insert the new node after it


if (temp != nullptr) {
Node* newNode = new Node;
newNode->data = newValue;
newNode->next = temp->next; // link new node to the next one
newNode->prev = temp; // link new node back to current node
if (temp->next != nullptr) { // if there is a next node, link it back
temp->next->prev = newNode;
}
temp->next = newNode; // link current node to new node
}
}

// Search for a value in the list


void searchValue(int value) {
Node* temp = head;

// Traverse the list and check for the value


while (temp != nullptr) {
if (temp->data == value) {
cout << "Value " << value << " found." << endl;
return;
}
temp = temp->next;
}
cout << "Value " << value << " not found." << endl;
}

// Delete a node with a given value


void deleteValue(int value) {
Node* temp = head;

// If the list is empty, do nothing


if (temp == nullptr) {
return;
}

// If the node to delete is the head


if (temp->data == value) {
head = temp->next; // update head to the next node
if (head != nullptr) {
head->prev = nullptr; // remove the previous link
}
delete temp;
return;
}

// Find the node with the value to delete


while (temp != nullptr && temp->data != value) {
temp = temp->next;
}

// 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;
}
}

// Print the entire list


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

// Main function to test the doubly linked list


int main() {
DoublyLinkedList list;
list.addAtStart(10); // Add 10 at the start
list.addAtEnd(20); // Add 20 at the end
list.addAfter(10, 15); // Add 15 after 10
list.searchValue(15); // Search for 15

list.printList(); // Print the list

list.deleteValue(10); // Delete 10
list.printList(); // Print again

return 0;
}

Single Circular Linked List:


#include <iostream>
using namespace std;

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

// Single Circular Linked List Class


class SingleCircularLinkedList {
private:
Node* head;

public:
// Constructor
SingleCircularLinkedList() {
head = nullptr; // List starts empty
}

// Add value at the start


void addAtStart(int value) {
Node* newNode = new Node;
newNode->data = value;
if (head == nullptr) { // If list is empty
newNode->next = newNode; // Point new node to itself
head = newNode;
}
else {
Node* temp = head;
// Find the last node
while (temp->next != head) {
temp = temp->next;
}
newNode->next = head; // Link new node to the first node
temp->next = newNode; // Link the last node to the new node
head = newNode; // Update head to the new node
}
}

// Add value at the end


void addAtEnd(int value) {
Node* newNode = new Node;
newNode->data = value;

if (head == nullptr) { // If the list is empty


newNode->next = newNode;
head = newNode;
}
else {
Node* temp = head;
while (temp->next != head) { // Traverse to the last node
temp = temp->next;
}
temp->next = newNode; // Link last node to the new node
newNode->next = head; // Link new node to the head
}
}

// Add a value after a specific node


void addAfter(int afterValue, int newValue) {
Node* temp = head;

// Search for the node with 'afterValue'


do {
if (temp->data == afterValue) {
Node* newNode = new Node;
newNode->data = newValue;
newNode->next = temp->next;
temp->next = newNode;
return;
}
temp = temp->next;
} while (temp != head); // Continue until we complete the loop

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;
}

Node* temp = head;


do {
if (temp->data == value) {
cout << "Value " << value << " found.\n";
return;
}
temp = temp->next;
} while (temp != head);

cout << "Value " << value << " not found.\n";
}

// Delete a node with a given value


void deleteValue(int value) {
if (head == nullptr) { // If the list is empty
return;
}

Node* temp = head;


Node* prev = nullptr;

// If the node to delete is the head node


if (head->data == value) {
if (head->next == head) { // If there's only one node
delete head;
head = nullptr;
}
else {
while (temp->next != head) { // Find the last node
temp = temp->next;
}
temp->next = head->next; // Link last node to the new head
Node* toDelete = head;
head = head->next; // Update head
delete toDelete;
}
return;
}

// Search for the node to delete


do {
prev = temp;
temp = temp->next;
} while (temp != head && temp->data != value);

// If the node is found, delete it


if (temp->data == value) {
prev->next = temp->next;
delete temp;
}
}

// Print the entire list


void printList() {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}

Node* temp = head;


do {
cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
cout << "(back to head)\n";
}
};

// Main function to test the circular linked list


int main() {
SingleCircularLinkedList list;

list.addAtStart(10);
list.addAtEnd(20);
list.addAfter(10, 15);
list.searchValue(15);

list.printList();

list.deleteValue(10);
list.printList();

return 0;
}

Double Circular Linked List:


#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
Node* prev;
};

// Double Circular Linked List Class


class DoubleCircularLinkedList {
private:
Node* head;

public:
// Constructor
DoubleCircularLinkedList() {
head = nullptr; // List starts empty
}

// Add value at the start


void addAtStart(int value) {
Node* newNode = new Node;
newNode->data = value;

if (head == nullptr) { // If list is empty


newNode->next = newNode;
newNode->prev = newNode;
head = newNode;
}
else {
Node* last = head->prev;
newNode->next = head;
newNode->prev = last;
head->prev = newNode;
last->next = newNode;
head = newNode; // Update head
}
}

// Add value at the end


void addAtEnd(int value) {
Node* newNode = new Node;
newNode->data = value;

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;
}
}

// Add a value after a specific node


void addAfter(int afterValue, int newValue) {
Node* temp = head;

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;
}

Node* temp = head;


do {
if (temp->data == value) {
cout << "Value " << value << " found.\n";
return;
}
temp = temp->next;
} while (temp != head);

cout << "Value " << value << " not found.\n";
}

// Delete a node with a given value


void deleteValue(int value) {
if (head == nullptr) { // If the list is empty
return;
}

Node* temp = head;

// If the node to delete is the head


if (head->data == value) {
if (head->next == head) { // If there's only one node
delete head;
head = nullptr;
}
else {
Node* last = head->prev;
Node* toDelete = head;
last->next = head->next;
head->next->prev = last;
head = head->next; // Update head
delete toDelete;
}
return;
}

// Search for the node to delete


do {
temp = temp->next;
} while (temp != head && temp->data != value);

// If found, delete it
if (temp->data == value) {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
delete temp;
}
}

// Print the entire list


void printList() {
if (head == nullptr) {
cout << "List is empty.\n";
return;
}

Node* temp = head;


do {
cout << temp->data << " <-> ";
temp = temp->next;
} while (temp != head);

cout << "(back to head)\n";


}
};

// Main function to test the circular doubly linked list


int main() {
DoubleCircularLinkedList list;

list.addAtStart(10);
list.addAtEnd(20);
list.addAfter(10, 15);
list.searchValue(15);

list.printList();

list.deleteValue(10);
list.printList();

return 0;
}

You might also like