0% found this document useful (0 votes)
4 views5 pages

CircularLL.cpp

The document contains a C++ implementation of a Circular Linked List, including methods for insertion, deletion, searching, and displaying the list. It defines a Node structure and a CircularLinkedList class with various functionalities to manage the list. The main function demonstrates the usage of these methods with example operations.

Uploaded by

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

CircularLL.cpp

The document contains a C++ implementation of a Circular Linked List, including methods for insertion, deletion, searching, and displaying the list. It defines a Node structure and a CircularLinkedList class with various functionalities to manage the list. The main function demonstrates the usage of these methods with example operations.

Uploaded by

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

#include <iostream>

using namespace std;

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

class CircularLinkedList {
private:
Node* head;
Node* cursor; // Points to the last node

public:
CircularLinkedList() : head(nullptr), cursor(nullptr) {}

~CircularLinkedList() {
if (!isEmpty()) {
Node* current = head;
Node* next;
cursor->next = nullptr; // Break the loop
while (current != nullptr) {
next = current->next;
delete current;
current = next;
}
}
}

bool isEmpty() const {


return head == nullptr;
}

void insertAtBeginning(int data) {


Node* newNode = new Node{data, nullptr};

if (isEmpty()) {
head = cursor = newNode;
newNode->next = head;
} else {
newNode->next = head;
head = newNode;
cursor->next = head;
}
}

void insertAtEnd(int data) {


Node* newNode = new Node{data, nullptr};

if (isEmpty()) {
head = cursor = newNode;
newNode->next = head;
} else {
newNode->next = head;
cursor->next = newNode;
cursor = newNode;
}
}

void deleteFirst() {
if (isEmpty()) {
cout << "List is empty." << endl;
return;
}

if (head == cursor) {
delete head;
head = cursor = nullptr;
} else {
Node* temp = head;
head = head->next;
cursor->next = head;
delete temp;
}
}

void deleteLast() {
if (isEmpty()) {
cout << "List is empty." << endl;
return;
}

if (head == cursor) {
delete cursor;
head = cursor = nullptr;
} else {
Node* current = head;
while (current->next != cursor) {
current = current->next;
}
current->next = head;
delete cursor;
cursor = current;
}
}

void deleteNodeWithValue(int key) {


if (isEmpty()) {
cout << "List is empty." << endl;
return;
}

if (head->data == key) {
deleteFirst();
return;
}

Node* prev = head;


Node* curr = head->next;

while (curr != head && curr->data != key) {


prev = curr;
curr = curr->next;
}

if (curr == head) {
cout << "Value not found." << endl;
return;
}

prev->next = curr->next;
if (curr == cursor) {
cursor = prev;
}

delete curr;
}
bool search(int key) const {
if (isEmpty()) return false;

Node* current = head;


do {
if (current->data == key) return true;
current = current->next;
} while (current != head);
return false;
}

int length() const {


if (isEmpty()) return 0;

int count = 0;
Node* current = head;
do {
count++;
current = current->next;
} while (current != head);
return count;
}

void display() const {


if (isEmpty()) {
cout << "List is empty." << endl;
return;
}

Node* current = head;


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

int main() {
CircularLinkedList list;

list.insertAtEnd(1);
list.insertAtEnd(2);
list.insertAtBeginning(3);
list.insertAtEnd(4);

cout << "List: ";


list.display();

list.deleteFirst();
cout << "After deleting first: ";
list.display();

list.deleteLast();
cout << "After deleting last: ";
list.display();

list.insertAtBeginning(10);
list.insertAtEnd(20);
cout << "After more insertions: ";
list.display();

cout << "Search 20: " << (list.search(20) ? "Found" : "Not Found") << endl;
cout << "Search 5: " << (list.search(5) ? "Found" : "Not Found") << endl;

list.deleteNodeWithValue(2);
cout << "After deleting node with value 2: ";
list.display();

cout << "Length: " << list.length() << endl;

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy