[go: up one dir, main page]

0% found this document useful (0 votes)
11 views11 pages

Circular Link List: The Last Node Pointer Means Tail Point To The First Node of The Link List Means Head

Uploaded by

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

Circular Link List: The Last Node Pointer Means Tail Point To The First Node of The Link List Means Head

Uploaded by

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

Circular Link List

The last node pointer means tail point to the first node of the
link list means head
In single link list
insertAtEnd Head Tail
10 102 20 103 30 Null
void insertAtEnd(int data) { 100 102 103

In circular link list


Node* newNode = new Node(data); Head Tail
10 102 20 103 100
30 Null
if (head == NULL) {
100 102 103
head = newNode;
tail = newNode;
Head , Tail
tail->next = head;
}
NewNode
else {
tail->next = newNode;
tail = newNode Head
Head , Tail Tail
tail->next = head;
} A newNode
}
insertAtFront In circular link list

Head Tail

void insertAtFront(int data) { 10 102 20 103 100


30 Null
Node* newNode = new Node(data); NewNode 100 102 103
1001
Head
if (head == NULL) { Head
head = newNode; 100 10 102 20 103 100
30 Null
tail = newNode; NewNode 100 102 103
tail->next = head;
}
else {
newNode->next = head;
head = newNode;
tail->next = head;
}
}
Delete from Beginning
void deleteFromBeginning() {
if (head == NULL) return;
if (head->next == head) {
delete head;
head = NULL;
}
else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
Node* firstNode = head;
head = head->next;
temp->next = head;
delete firstNode;
}
}
Search in Circular Linked List
bool search(int key) {
if (head == NULL)
return false;
Node* temp = head;
do {
if (temp->data == key)
return true;
temp = temp->next;
} while (temp != head);

return false;

}
Delete from End
void deleteFromEnd() {
if (head == nullptr) return;

if (head->next == head) {
delete head;
head = nullptr;
tail = nullptr;
} else {
Node* temp = head;
while (temp->next != tail) {
temp = temp->next;
}
delete tail;
tail = temp;
tail->next = head;
}
}
Update Method

void update(int key, int newData)


{
if (head == nullptr) return;
node* temp = head;
do {
if (temp->data == key) {
temp->data = newData;
return;
}
temp = temp->next;
} while (temp != head);
cout << "Key not found." << endl;
}
// Insert at Position
void insertAtPosition(int data, int position) {
Node* newNode = new Node(data);
if (position <= 0 || head == nullptr) {
insertAtBeginning(data);
return;
}

Node* temp = head;


for (int i = 1; i < position && temp->next != head;
i++) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;

if (temp == tail) {
tail = newNode;
}
}
// Delete at Position
void deleteAtPosition(int position) {
if (head == nullptr) return;

if (position == 0) {
deleteFromBeginning();
return;
}

Node* temp = head;


for (int i = 0; i < position - 1 && temp->next != head; i++)
{
temp = temp->next;
}

if (temp->next == head) return;

Node* nodeToDelete = temp->next;


temp->next = nodeToDelete->next;
if (nodeToDelete == tail) {
tail = temp;
}
delete nodeToDelete;
}
int main() {
CircularLinkedList list;

// Inserting nodes at the end


list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.display();

// Inserting nodes at the beginning


list.insertAtBeginning(5);
list.display();

// Deleting a node from the end


list.deleteFromEnd();
list.display();

// Deleting a node from the beginning


list.deleteFromBeginning();
list.display();
// Searching for a value
cout << "Searching for 20: " << (list.search(20) ? "Found" : "Not Found") << endl;
cout << "Searching for 15: " << (list.search(15) ? "Found" : "Not Found") << endl;

// Updating a node's data


list.update(20, 25);
list.display();

// Inserting a node at a specific position


list.insertAtPosition(15, 1); // Insert 15 at position 1
list.display();

// Deleting a node at a specific position


list.deleteAtPosition(1); // Delete node at position 1
list.display();

return 0;
}

You might also like