0% found this document useful (0 votes)
13 views6 pages

Program10 Dsa

Uploaded by

Rakhi Sharma
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)
13 views6 pages

Program10 Dsa

Uploaded by

Rakhi Sharma
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/ 6

PROGRAM-10

AIM : Write a program to implement circular linked list for the following
operations: Create, Display, inserting, counting, searching, traversing and
deletion.
TOOLS USED : Visual Studio Code Editor
SOURCE CODE:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class CircularLinkedList {
private:
Node* tail;

public:

CircularLinkedList() : tail(nullptr) {}
void create(int data) {
Node* newNode = new Node();
newNode->data = data;
if (tail == nullptr) {
tail = newNode;
tail->next = tail;
} else {
newNode->next = tail->next;
tail->next = newNode;
tail = newNode;
}
}
void display() {
if (tail == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = tail->next;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != tail->next);
cout << endl;
}
int count() {
if (tail == nullptr) return 0;
int count = 0;
Node* temp = tail->next;
do {
count++;
temp = temp->next;
} while (temp != tail->next);
return count;
}
bool search(int key) {
if (tail == nullptr) return false;
Node* temp = tail->next;
do {
if (temp->data == key) return true;
temp = temp->next;
} while (temp != tail->next);
return false;
}
void insert(int data, int pos) {
int size = count();
if (pos < 1 || pos > size + 1) {
cout << "Invalid position!" << endl;
return;
}
Node* newNode = new Node();
newNode->data = data;
if (pos == 1) {
if (tail == nullptr) {
tail = newNode;
tail->next = tail;
} else {
newNode->next = tail->next;
tail->next = newNode;
}
} else {
Node* temp = tail->next;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
if (pos == size + 1) tail = newNode;
}
}
void deleteNode(int pos) {
int size = count();
if (pos < 1 || pos > size) {
cout << "Invalid position!" << endl;
return;
}

Node* temp = tail->next;

if (pos == 1) {
if (tail->next == tail) {
delete tail;
tail = nullptr;
} else {
tail->next = temp->next;
delete temp;
}
} else {
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
}
Node* toDelete = temp->next;
temp->next = toDelete->next;
if (toDelete == tail) tail = temp;
delete toDelete;
}
}
~CircularLinkedList() {
if (tail == nullptr) return;

Node* current = tail->next;


Node* nextNode;
while (current != tail) {
nextNode = current->next;
delete current;
current = nextNode;
}
delete tail;
tail = nullptr;
}
};
int main() {
CircularLinkedList cll;
cll.create(10);
cll.create(20);
cll.create(30);

cout << "List after creation: ";


cll.display();

cout << "Count of nodes: " << cll.count() << endl;

cout << "Searching for 20: " << (cll.search(20) ? "Found" : "Not Found") << endl;

cout << "Inserting 15 at position 2: " << endl;


cll.insert(15, 2);
cll.display();

cout << "Deleting node at position 3: " << endl;


cll.deleteNode(3);
cll.display();
return 0;
}

OUTPUT:

LEARNING OUTCOMES:
1. Understanding Circular Linked List Concept: Learn how circular linked lists differ from
singly and doubly linked lists
2. Dynamic Memory Management: Gain hands-on experience with dynamic memory allocation
(new and delete) in C++.

You might also like