Dsa Lab Assignment (3)
Dsa Lab Assignment (3)
Algorithm:
Insertion at End in Circular Linked List
1. Create a new node.
2. Set the data of the new node to the desired value.
3. If the circular linked list is empty, make the new node point to itself and set it as the head.
4. Traverse to the last node in the circular linked list.
- This can be done by starting from the head and moving to the last node (where next points to the head).
5. Set the next pointer of the new node to point to the head of the circular linked list.
6. Update the next pointer of the last node to point to the new node.
7. Exit.
Insertion at specific place:
Algorithm:
Insertion at Specific Location in Circular Linked List
1. Create a new node.
2. Set the data of the new node to the desired value.
3. If the circular linked list is empty, make the new node point to itself and set it as the head.
4. Traverse to the node at the (position - 1) in the circular linked list.
- If the position is 1, go to step 7.
- If the position is greater than the current size of the list, display an error message and exit.
5. Update pointers to insert the new node at the specified position.
- Set the next pointer of the new node to the next node.
- Set the next pointer of the current node to the new node.
6. Exit.
7. Update pointers to insert the new node at the beginning.
- Set the next pointer of the new node to point to the head.
- Update the next pointer of the last node to point to the new node.
- Update the head of the circular linked list to be the new node.
8. Exit.
Deletion at start:
Algorithm:
Deletion from Start in Circular Linked List
1. If the circular linked list is empty, display an error message and exit.
2. If the circular linked list has only one node, free the memory of the node, set head to null, and exit.
3. Traverse to the last node in the circular linked list.
- This can be done by starting from the head and moving to the last node (where next points to the head).
4. Update pointers to delete the first node.
- Set the next node as the new head of the circular linked list.
- Set the next pointer of the last node to point to the new head.
- Free the memory of the original head.
5. Exit.
Deletion at end:
Algorithm:
Deletion from End in Circular Linked List
1. If the circular linked list is empty, display an error message and exit.
2. If the circular linked list has only one node, free the memory of the node, set head to null, and exit.
3. Traverse to the last node in the circular linked list.
- This can be done by starting from the head and moving to the last node (where next points to the head).
4. Traverse to the second-to-last node in the circular linked list.
5. Update pointers to delete the last node.
- Set the next pointer of the second-to-last node to the head.
- Free the memory of the original last node.
6. Exit.
Deletion at specific end:
Algorithm:
Deletion from Specific Location in Circular Linked List
1. If the circular linked list is empty, display an error message and exit.
2. If the position is 1, delete the node from the beginning using the algorithm for deletion from the start.
3. Traverse to the node at the (position - 1) in the circular linked list.
- If the position is greater than the current size of the list, display an error message and exit.
4. Update pointers to delete the node at the specified position.
- Set the next pointer of the current node to the next node.
- Free the memory of the original node.
5. Exit.
CODE:
#include <iostream>
using namespace std;
// Node structure for circular linked list
struct Node {
int data;
Node* next;
};
// Class for circular linked list operations
class CircularLinkedList {
private:
Node* head;
public:
// Constructor
CircularLinkedList() : head(nullptr) {}
// Function to insert node at the end of the list
void insertAtEnd(int value) {
Node* newNode = new Node{value, nullptr};
if (!head) {
head = newNode;
newNode->next = head;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}
// Function to insert node at the beginning of the list
void insertAtBeginning(int value) {
Node* newNode = new Node{value, nullptr};
if (!head) {
head = newNode;
newNode->next = head;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
newNode->next = head;
temp->next = newNode;
head = newNode;
}
}
// Function to insert node at a specific position
void insertAtPosition(int value, int position) {
Node* newNode = new Node{value, nullptr};
if (!head || position <= 1) {
insertAtBeginning(value);
} else {
Node* temp = head;
for (int i = 1; i < position - 1 && temp->next != head; ++i) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
// Function to delete node from the end of the list
void deleteFromEnd() {
if (!head) {
cout << "List is empty." << endl;
} else {
Node* temp = head;
Node* prev = nullptr;
while (temp->next != head) {
prev = temp;
temp = temp->next;
}
if (prev) {
prev->next = head;
delete temp;
} else {
// Only one node in the list
delete head;
head = nullptr;
}
}
}
// Function to delete node from the beginning of the list
void deleteFromBeginning() {
if (!head) {
cout << "List is empty." << endl;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
if (temp == head) {
// Only one node in the list
delete head;
head = nullptr;
} else {
temp->next = head->next;
delete head;
head = temp->next;
}
}
}
// Function to delete node from a specific position
void deleteFromPosition(int position) {
if (!head || position <= 1) {
deleteFromBeginning();
} else {
Node* temp = head;
Node* prev = nullptr;
for (int i = 1; i < position && temp->next != head; ++i) {
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
delete temp;
}
}
// Function to search for a value in the list
bool search(int value) {
if (!head) {
return false;
}
Node* temp = head;
do {
if (temp->data == value) {
return true;
}
temp = temp->next;
} while (temp != head);
return false;
}
// Function to display the circular linked list
void display() {
if (!head) {
cout << "List is empty." << endl;
} else {
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
}
};
int main() {
CircularLinkedList myList;
// Menu-driven program
int choice;
do {
cout << "\nCircular Linked List Operations:\n";
cout << "1. Insert at end\n";
cout << "2. Insert at beginning\n";
cout << "3. Insert at specific position\n";
cout << "4. Delete from end\n";
cout << "5. Delete from beginning\n";
cout << "6. Delete from specific position\n";
cout << "7. Display\n";
cout << "0. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
int valueEnd;
cout << "Enter value to insert at end: ";
cin >> valueEnd;
myList.insertAtEnd(valueEnd);
break;
case 2:
int valueBeginning;
cout << "Enter value to insert at beginning: ";
cin >> valueBeginning;
myList.insertAtBeginning(valueBeginning);
break;
case 3:
int valuePosition, position;
cout << "Enter value to insert: ";
cin >> valuePosition;
cout << "Enter position to insert at: ";
cin >> position;
myList.insertAtPosition(valuePosition, position);
break;
case 4:
myList.deleteFromEnd();
break;
case 5:
myList.deleteFromBeginning();
break;
case 6:
int deletePosition;
cout << "Enter position to delete from: ";
cin >> deletePosition;
myList.deleteFromPosition(deletePosition);
break;
case 7:
cout << "Circular Linked List: ";
myList.display();
break;
case 0:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 0);
return 0;
}