Menu Driven Program to implement all the operations of Doubly Circular Linked List
Last Updated : 18 Mar, 2024
Circular Doubly Linked List has properties of both Doubly Linked List and Circular Linked List in which two consecutive elements are linked or connected by previous and next pointer and the last node points to the first node by next pointer and also the first node points to the last node by the previous pointer. The program implements all the functions and operations that are possible on a doubly circular list following functions in a menu-driven program.
Approach: Each function (except display) takes the address of the pointer to the head i.e. the first element of the list. This allows us to change the address of the head pointer. The idea is the use only one pointer, which is of type node, to perform all the operations. node is the class that contains three data members. One member is of type int which stores data of the node and two members of type node*, one store's address of next element and other stores address of the previous element. This way it is possible to traverse the list in either direction. It is possible to modify, insert, delete search for the element using a single pointer.
Functions Covered:
- void insert_front (node **head): This function allows the user to enter a new node at the front of the list.
- void insert_end (node **head): This function allows the user to enter a new node at the end of the list.
- void inser_after (node **head): This function allows the user to enter a new node after a node is entered by the user.
- void insert_before (node **head): This function allows the user to enter a new node before a node is entered by the user.
- void delete_front (node **head): This function allows the user to delete a node from the front of the list.
- void delete_end(node**head): This function allows the user to delete a node from the end of the list.
- void delete_mid( node **head): This function allows deletion of a node entered by the user.
- void search (node *head): Function to search for the location of the array entered by the user.
- void reverse (node **head): Function to reverse the list and make the last element of the list head.
- void display (node *head): Function to print the list.
Below is the C++ program to implement the above approach:
C++ // C++ program for the above approach #include <iostream> using namespace std; class node { public: node* next; node* prev; int data; }; // Function to add the node at the front // of the doubly circular LL void insert_front(node** head) { // Function to insert node // in front of list cout << "\nEnter Data for New Node:"; // Create a new node named // new_node node* new_node = new node; // Enter data for new_node cin >> new_node->data; if (*head == NULL) { // If there is no node in // the list, create a node // pointing to itself and // make it head new_node->next = new_node; new_node->prev = new_node; *head = new_node; } else { // If there already exists // elements in the list // Next of new_node will point // to head new_node->next = *head; // prev of new_node will point // to prev of head new_node->prev = (*head)->prev; // next of prev of head i.e. next // of last node will point to // new_node ((*head)->prev)->next = new_node; // prev of head will point // to new_node (*head)->prev = new_node; // new_node will become the // head of list *head = new_node; } } // Function to add the node at the end // of the doubly circular LL void insert_end(node** head) { // Function to insert node at // last of list cout << "\nEnter Data for New Node:"; // Create new node node* new_node = new node; cin >> new_node->data; if (*head == NULL) { // If there is no element in the // list create a node pointing // to itself and make it head new_node->next = new_node; new_node->prev = new_node; *head = new_node; } else { // If there are elements in the // list then create a temp node // pointing to current element node* curr = *head; while (curr->next != *head) // Traverse till the end of // list curr = curr->next; // next of new_node will point to // next of current node new_node->next = curr->next; // prev of new_node will // point to current element new_node->prev = curr; // prev of next of current node // i.e. prev of head will point to // new_node (curr->next)->prev = new_node; // next of current node will // point to new_node curr->next = new_node; } } // Function to add the node after the // given node of doubly circular LL void insert_after(node** head) { // Function to enter a node after // the element entered by user // Create new node node* new_node = new node; cout << "\nEnter Data for New Node:"; cin >> new_node->data; if (*head == NULL) { // If there is no element in // the list then create a node // pointing to itself and make // it head cout << "\nThere is No element in the List"; cout << "\nCreating a new node"; new_node->prev = new_node; new_node->next = new_node; *head = new_node; } else { int num; // Ask user after which node new // node is to be inserted cout << "Enter After Element:"; cin >> num; // temp node to traverse list // and point to current element node* curr = *head; while (curr->data != num) { curr = curr->next; // If current becomes equal // to head i.e. if entire list // has been traversed then // element entered is not found // in list if (curr == *head) { cout << "\nEntered Element" << " Not Found in " "List\n"; return; } } // Control will reach here only if // element is found in list // next of new node will point to // next of current node new_node->next = curr->next; // prev of new node will // point to current node new_node->prev = curr; // prev of next of current node // will point to new node (curr->next)->prev = new_node; // next of current node will // point to new node curr->next = new_node; } } // Function to add the node before the // given node of doubly circular LL void insert_before(node** head) { // Function to enter node before // a node entered by the user node* new_node = new node; if (*head == NULL) { // If there is no element in the // list create new node and make // it head cout << "List is Empty!! Creating New node..."; cout << "\nEnter Data for New Node:"; cin >> new_node->data; new_node->prev = new_node; new_node->next = new_node; *head = new_node; } else { int num; // Ask user before which node // new node is to be inserted cout << "\nEnter Before Element:"; cin >> num; // If user wants to enter new node // before the first node i.e. // before head then call insert_front // function if ((*head)->data == num) insert_front(head); else { // temp node current for traversing // the list and point to current // element we assign curr to // *head->next this time because // data of head has already been // checked in previous condition node* curr = (*head)->next; while (curr->data != num) { if (curr == *head) { // If current equal head then // entire list has been traversed // and the entered element is not // found in list cout << "\nEntered Element Not Found " "in List!!\n"; return; } curr = curr->next; } cout << "\nEnter Data For New Node:"; cin >> new_node->data; // Control will reaach here only // if entered node exists in list // and current has found the element // next of new node will point to // current node new_node->next = curr; // prev of new node will point // to prev of current node new_node->prev = curr->prev; // next of prev of current node // will point to new node (curr->prev)->next = new_node; // prev of current will // point to new node curr->prev = new_node; } } } // Function to delete the front node // of doubly circular LL void delete_front(node** head) { // Function to delete a node // from front of list if (*head == NULL) { // If list is already empty // print a message cout << "\nList in empty!!\n"; } else if ((*head)->next == *head) { // If head is the only element // in the list delete head and // assign it to NULL delete *head; *head = NULL; } else { node* curr = new node; // temp node to save address // of node next to head curr = (*head)->next; // prev of temp will // point to prev of head curr->prev = (*head)->prev; // next of prev of head i.e. // next of last node will point // to temp ((*head)->prev)->next = curr; // delete head delete *head; // assign head to temp *head = curr; } } // Function to delete the end node // of doubly circular LL void delete_end(node** head) { // Function to delete a node // from end of list if (*head == NULL) { // If list is already empty // print a message cout << "\nList is Empty!!\n"; } else if ((*head)->next == *head) { // If head is the only element // in the list delete head and // assign it to NULL delete *head; *head = NULL; } else { // Create temporary node curr // to traverse list and point // to current element node* curr = new node; // Assign curr to head curr = *head; while (curr->next != (*head)) { // Traverse till end of list curr = curr->next; } // next of prev of curr will point // to next of curr (curr->prev)->next = curr->next; // prev of next of curr will point // to prev of curr (curr->next)->prev = curr->prev; // delete curr delete curr; } } // Function to delete the middle node // of doubly circular LL void delete_mid(node** head) { // Function to delete a node // entered by user if (*head == NULL) { // If list is already empty // print a message cout << "\nList is Empty!!!"; } else { cout << "\nEnter Element to be deleted:"; int num; cin >> num; if ((*head)->data == num) { // If user wants to delete // the head node i.e front // node call delete_front(head) // function delete_front(head); } else { // temp node to traverse list // and point to current node node* curr = (*head)->next; while ((curr->data) != num) { if (curr == (*head)) { // If curr equals head then // entire list has been // traversed element to be // deleted is not found cout << "\nEntered Element Not Found " "in List!!\n"; return; } curr = curr->next; } // control will reach here only // if element is found in the list // next of prev of curr will // point to next of curr (curr->prev)->next = curr->next; // prev of next of curr will // point to prev of curr (curr->next)->prev = curr->prev; delete curr; } } } // Function to search any node in the // doubly circular LL void search(node* head) { if (head == NULL) { // If head is null list is empty cout << "List is empty!!"; return; } int item; cout << "Enter item to be searched:"; // Ask user to enter item to // be searched cin >> item; // curr pointer is used to // traverse list it will point // to the current element node* curr = head; int index = 0, count = 0; do { // If data in curr is equal to item // to be searched print its position // index+1 if (curr->data == item) { cout << "\nItem found at position:" << index + 1; // increment count count++; } // Index will increment by 1 in // each iteration index++; curr = curr->next; } while (curr != head); // If count is still 0 that means // item is not found if (count == 0) cout << "Item searched not found in list"; } // Function to reverse the doubly // circular Linked List void reverse(node** head) { if (*head == NULL) { // If head is null list is empty cout << "List is Empty !!"; return; } // curr is used to traverse list node* curr = *head; while (curr->next != *head) { // use a temp node to store // address of next of curr node* temp = curr->next; // make next of curr to point // its previous curr->next = curr->prev; // make previous of curr to // point its next curr->prev = temp; // After each iteration move // to element which was earlier // next of curr curr = temp; } // Update the last node separately node* temp = curr->next; curr->next = curr->prev; curr->prev = temp; // only change is this node will now // become head *head = curr; } // Function to display the doubly // circular linked list void display(node* head) { node* curr = head; if (curr == NULL) cout << "\n List is Empty!!"; else { do { cout << curr->data << "->"; curr = curr->next; } while (curr != head); } } void display_menu() { cout << "==============================================" "======================"; cout << "\nMenu:\n"; cout << "1. Insert At Front\n"; cout << "2. Insert At End\n"; cout << "3. Insert After Element\n"; cout << "4. Insert Before Element\n"; cout << "5. Delete From Front\n"; cout << "6. Delete From End\n"; cout << "7. Delete A Node\n"; cout << "8. Search for a element\n"; cout << "9. Reverse a the list\n"; cout << "==============================================" "======================"; } // Driver Code int main() { int choice; char repeat_menu = 'y'; // Declaration of head node node* head = NULL; display_menu(); do { cout << "\nEnter Your Choice:"; cin >> choice; switch (choice) { case 1: { insert_front(&head); display(head); break; } case 2: { insert_end(&head); display(head); break; } case 3: { insert_after(&head); display(head); break; } case 4: { insert_before(&head); display(head); break; } case 5: { delete_front(&head); display(head); break; } case 6: { delete_end(&head); display(head); break; } case 7: { delete_mid(&head); display(head); break; } case 8: { search(head); break; } case 9: { reverse(&head); display(head); break; } default: { cout << "\nWrong Choice!!!"; display_menu(); break; } } cout << "\nEnter More(Y/N)"; cin >> repeat_menu; } while (repeat_menu == 'y' || repeat_menu == 'Y'); return 0; }
Java import java.util.Scanner; class Node { int data; Node next; Node prev; } public class Main { static Scanner scanner = new Scanner(System.in); public static void displayMenu() { System.out.println("=============================================="); System.out.println("Menu:"); System.out.println("1. Insert At Front"); System.out.println("2. Insert At End"); System.out.println("3. Insert After Element"); System.out.println("4. Insert Before Element"); System.out.println("5. Delete From Front"); System.out.println("6. Delete From End"); System.out.println("7. Delete A Node"); System.out.println("8. Search for an element"); System.out.println("9. Reverse the list"); System.out.println("=============================================="); } static Node insertFront(Node head) { System.out.println("\nEnter Data for New Node:"); Node newNode = new Node(); newNode.data = scanner.nextInt(); if (head == null) { newNode.next = newNode; newNode.prev = newNode; head = newNode; } else { newNode.next = head; newNode.prev = head.prev; head.prev.next = newNode; head.prev = newNode; head = newNode; } return head; } // Function to insert node at the end of the doubly circular LL static Node insertEnd(Node head) { System.out.println("\nEnter Data for New Node:"); Node newNode = new Node(); newNode.data = scanner.nextInt(); if (head == null) { newNode.next = newNode; newNode.prev = newNode; head = newNode; } else { Node curr = head; while (curr.next != head) curr = curr.next; newNode.next = curr.next; newNode.prev = curr; curr.next.prev = newNode; curr.next = newNode; } return head; } // Function to insert node after a specific node in the doubly circular LL static Node insertAfter(Node head) { System.out.println("\nEnter Data for New Node:"); Node newNode = new Node(); newNode.data = scanner.nextInt(); if (head == null) { System.out.println("\nThere is No element in the List"); System.out.println("\nCreating a new node"); newNode.prev = newNode; newNode.next = newNode; head = newNode; } else { System.out.println("Enter After Element:"); int num = scanner.nextInt(); Node curr = head; while (curr.data != num) { curr = curr.next; if (curr == head) { System.out.println("\nEntered Element Not Found in List\n"); return head; } } newNode.next = curr.next; newNode.prev = curr; curr.next.prev = newNode; curr.next = newNode; } return head; } // Function to insert node before a specific node in the doubly circular LL static Node insertBefore(Node head) { System.out.println("\nEnter Before Element:"); int num = scanner.nextInt(); if (head == null || head.data == num) { return insertFront(head); } else { Node curr = head.next; while (curr.data != num) { if (curr == head) { System.out.println("\nEntered Element Not Found in List!!\n"); return head; } curr = curr.next; } System.out.println("\nEnter Data For New Node:"); Node newNode = new Node(); newNode.data = scanner.nextInt(); newNode.next = curr; newNode.prev = curr.prev; curr.prev.next = newNode; curr.prev = newNode; } return head; } // Function to delete the front node of the doubly circular LL static Node deleteFront(Node head) { if (head == null) { System.out.println("\nList is empty!!\n"); } else if (head.next == head) { head = null; } else { Node curr = head.next; curr.prev = head.prev; head.prev.next = curr; head = curr; } return head; } // Function to delete the end node of the doubly circular LL static Node deleteEnd(Node head) { if (head == null) { System.out.println("\nList is Empty!!\n"); } else if (head.next == head) { head = null; } else { Node curr = head; while (curr.next != head) curr = curr.next; curr.prev.next = curr.next; curr.next.prev = curr.prev; } return head; } // Function to delete a specific node in the doubly circular LL static Node deleteMid(Node head) { if (head == null) { System.out.println("\nList is Empty!!!"); } else { System.out.println("\nEnter Element to be deleted:"); int num = scanner.nextInt(); if (head.data == num) { head = deleteFront(head); } else { Node curr = head.next; while (curr.data != num) { if (curr == head) { System.out.println("\nEntered Element Not Found in List!!\n"); return head; } curr = curr.next; } curr.prev.next = curr.next; curr.next.prev = curr.prev; } } return head; } // Function to search any node in the doubly circular LL static void search(Node head) { if (head == null) { System.out.println("List is empty!!"); return; } System.out.println("Enter item to be searched:"); int item = scanner.nextInt(); Node curr = head; int index = 0, count = 0; do { if (curr.data == item) { System.out.println("\nItem found at position:" + (index + 1)); count++; } index++; curr = curr.next; } while (curr != head); if (count == 0) System.out.println("Item searched not found in list"); } // Function to reverse the doubly circular Linked List static Node reverse(Node head) { if (head == null) { System.out.println("List is Empty !!"); return head; } Node curr = head; while (curr.next != head) { Node temp = curr.next; curr.next = curr.prev; curr.prev = temp; curr = temp; } Node temp = curr.next; curr.next = curr.prev; curr.prev = temp; head = curr; return head; } // Function to display the doubly circular linked list static void display(Node head) { Node curr = head; if (curr == null) System.out.println("\n List is Empty!!"); else { do { System.out.print(curr.data + "->"); curr = curr.next; } while (curr != head); } } public static void main(String[] args) { Node head = null; char repeatMenu = 'y'; int choice; displayMenu(); // Display the menu do { System.out.println("\nEnter Your Choice:"); choice = scanner.nextInt(); switch (choice) { case 1: head = insertFront(head); display(head); break; case 2: head = insertEnd(head); display(head); break; case 3: head = insertAfter(head); display(head); break; case 4: head = insertBefore(head); display(head); break; case 5: head = deleteFront(head); display(head); break; case 6: head = deleteEnd(head); display(head); break; case 7: head = deleteMid(head); display(head); break; case 8: search(head); break; case 9: head = reverse(head); display(head); break; default: System.out.println("\nWrong Choice!!!"); break; } System.out.println("\nEnter More(Y/N)"); repeatMenu = scanner.next().charAt(0); } while (repeatMenu == 'y' || repeatMenu == 'Y'); } }
C# using System; public class Node { public int data; public Node next; public Node prev; } public class DoublyCircularLinkedList { Node head; // Insert node at the front of the list public void InsertFront(int data) { Node newNode = new Node { data = data }; if (head == null) { newNode.next = newNode; newNode.prev = newNode; head = newNode; } else { newNode.next = head; newNode.prev = head.prev; head.prev.next = newNode; head.prev = newNode; head = newNode; } } // Insert node at the end of the list public void InsertEnd(int data) { Node newNode = new Node { data = data }; if (head == null) { newNode.next = newNode; newNode.prev = newNode; head = newNode; } else { Node curr = head; while (curr.next != head) { curr = curr.next; } newNode.next = curr.next; newNode.prev = curr; curr.next.prev = newNode; curr.next = newNode; } } // Insert node after a specific element public void InsertAfter(int data, int element) { Node newNode = new Node { data = data }; if (head == null) { Console.WriteLine("List is empty. Creating new node."); newNode.prev = newNode; newNode.next = newNode; head = newNode; } else { Node curr = head; do { if (curr.data == element) { newNode.next = curr.next; newNode.prev = curr; curr.next.prev = newNode; curr.next = newNode; return; } curr = curr.next; } while (curr != head); Console.WriteLine("Element not found in the list."); } } // Insert node before a specific element public void InsertBefore(int data, int element) { Node newNode = new Node { data = data }; if (head == null) { Console.WriteLine("List is empty. Creating new node."); newNode.prev = newNode; newNode.next = newNode; head = newNode; } else { if (head.data == element) { InsertFront(data); } else { Node curr = head.next; while (curr.data != element) { if (curr == head) { Console.WriteLine("Element not found in the list."); return; } curr = curr.next; } newNode.next = curr; newNode.prev = curr.prev; curr.prev.next = newNode; curr.prev = newNode; } } } // Delete node from the front of the list public void DeleteFront() { if (head == null) { Console.WriteLine("List is empty."); } else if (head.next == head) { head = null; } else { Node curr = head.next; curr.prev = head.prev; head.prev.next = curr; head = curr; } } // Delete node from the end of the list public void DeleteEnd() { if (head == null) { Console.WriteLine("List is empty."); } else if (head.next == head) { head = null; } else { Node curr = head; while (curr.next != head) { curr = curr.next; } curr.prev.next = curr.next; curr.next.prev = curr.prev; } } // Delete a specific element public void DeleteElement(int element) { if (head == null) { Console.WriteLine("List is empty."); } else { if (head.data == element) { DeleteFront(); } else { Node curr = head.next; while (curr.data != element) { if (curr == head) { Console.WriteLine("Element not found in the list."); return; } curr = curr.next; } curr.prev.next = curr.next; curr.next.prev = curr.prev; } } } // Search for an element public void Search(int element) { if (head == null) { Console.WriteLine("List is empty."); return; } Node curr = head; int index = 0; do { if (curr.data == element) { Console.WriteLine($"Element found at position: {index + 1}"); return; } index++; curr = curr.next; } while (curr != head); Console.WriteLine("Element not found in the list."); } // Reverse the list public void Reverse() { if (head == null) { Console.WriteLine("List is empty."); return; } Node curr = head; do { Node temp = curr.next; curr.next = curr.prev; curr.prev = temp; curr = temp; } while (curr != head); head = curr.prev; } // Display the list public void Display() { if (head == null) { Console.WriteLine("List is empty."); return; } Node curr = head; do { Console.WriteLine(curr.data); curr = curr.next; } while (curr != head); } } class Program { static void Main(string[] args) { DoublyCircularLinkedList list = new DoublyCircularLinkedList(); char repeatMenu = 'y'; int choice; do { Console.WriteLine("=============================================="); Console.WriteLine("Menu:"); Console.WriteLine("1. Insert At Front"); Console.WriteLine("2. Insert At End"); Console.WriteLine("3. Insert After Element"); Console.WriteLine("4. Insert Before Element"); Console.WriteLine("5. Delete From Front"); Console.WriteLine("6. Delete From End"); Console.WriteLine("7. Delete A Node"); Console.WriteLine("8. Search for a element"); Console.WriteLine("9. Reverse a the list"); Console.WriteLine("=============================================="); Console.WriteLine("\nEnter Your Choice:"); choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("Enter data:"); list.InsertFront(Convert.ToInt32(Console.ReadLine())); list.Display(); break; case 2: Console.WriteLine("Enter data:"); list.InsertEnd(Convert.ToInt32(Console.ReadLine())); list.Display(); break; case 3: Console.WriteLine("Enter data and element after which to insert:"); list.InsertAfter(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine())); list.Display(); break; case 4: Console.WriteLine("Enter data and element before which to insert:"); list.InsertBefore(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine())); list.Display(); break; case 5: list.DeleteFront(); list.Display(); break; case 6: list.DeleteEnd(); list.Display(); break; case 7: Console.WriteLine("Enter element to delete:"); list.DeleteElement(Convert.ToInt32(Console.ReadLine())); list.Display(); break; case 8: Console.WriteLine("Enter element to search:"); list.Search(Convert.ToInt32(Console.ReadLine())); break; case 9: list.Reverse(); list.Display(); break; default: Console.WriteLine("\nWrong Choice!!!"); break; } Console.WriteLine("\nEnter More(Y/N)"); repeatMenu = Convert.ToChar(Console.ReadLine()); } while (repeatMenu == 'y' || repeatMenu == 'Y'); } }
Python3 class Node: def __init__(self, data): self.data = data self.next = None self.prev = None # Function to add a node at the front of the doubly circular LL def insert_front(head): data = int(input("\nEnter Data for New Node: ")) new_node = Node(data) if head is None: new_node.next = new_node new_node.prev = new_node return new_node else: new_node.next = head new_node.prev = head.prev head.prev.next = new_node head.prev = new_node return new_node # Function to add a node at the end of the doubly circular LL def insert_end(head): data = int(input("\nEnter Data for New Node: ")) new_node = Node(data) if head is None: new_node.next = new_node new_node.prev = new_node return new_node else: curr = head while curr.next != head: curr = curr.next new_node.next = curr.next new_node.prev = curr curr.next.prev = new_node curr.next = new_node return head # Function to add a node after the given node of doubly circular LL def insert_after(head): data = int(input("\nEnter Data for New Node: ")) if head is None: print("\nThere is No element in the List") print("Creating a new node") new_node = Node(data) new_node.next = new_node new_node.prev = new_node return new_node else: num = int(input("Enter After Element: ")) curr = head while curr.data != num: curr = curr.next if curr == head: print("\nEntered Element Not Found in List") return head new_node = Node(data) new_node.next = curr.next new_node.prev = curr curr.next.prev = new_node curr.next = new_node return head # Function to add a node before the given node of doubly circular LL def insert_before(head): if head is None: print("List is Empty!! Creating New node...") data = int(input("\nEnter Data for New Node: ")) new_node = Node(data) new_node.next = new_node new_node.prev = new_node return new_node else: num = int(input("\nEnter Before Element: ")) if head.data == num: return insert_front(head) else: curr = head.next while curr.data != num: if curr == head: print("\nEntered Element Not Found in List!!") return head curr = curr.next data = int(input("\nEnter Data For New Node: ")) new_node = Node(data) new_node.next = curr new_node.prev = curr.prev curr.prev.next = new_node curr.prev = new_node return head # Function to delete the front node of doubly circular LL def delete_front(head): if head is None: print("\nList is empty!!") return None elif head.next == head: del head return None else: curr = head.next curr.prev = head.prev head.prev.next = curr del head return curr # Function to delete the end node of doubly circular LL def delete_end(head): if head is None: print("\nList is Empty!!") return None elif head.next == head: del head return None else: curr = head while curr.next != head: curr = curr.next curr.prev.next = curr.next curr.next.prev = curr.prev del curr return head # Function to delete the middle node of doubly circular LL def delete_mid(head): if head is None: print("\nList is Empty!!!") return None else: num = int(input("\nEnter Element to be deleted: ")) if head.data == num: return delete_front(head) else: curr = head.next while curr.data != num: if curr == head: print("\nEntered Element Not Found in List!!") return head curr = curr.next curr.prev.next = curr.next curr.next.prev = curr.prev del curr return head # Function to search any node in the doubly circular LL def search(head): if head is None: print("List is empty!!") return item = int(input("Enter item to be searched: ")) curr = head index = 0 count = 0 while True: if curr.data == item: print("\nItem found at position:", index + 1) count += 1 index += 1 curr = curr.next if curr == head: break if count == 0: print("Item searched not found in list") # Function to reverse the doubly circular Linked List def reverse(head): if head is None: print("List is Empty !!") return None curr = head while curr.next != head: temp = curr.next curr.next = curr.prev curr.prev = temp curr = temp temp = curr.next curr.next = curr.prev curr.prev = temp head = curr return head # Function to display the doubly circular linked list def display(head): if head is None: print("\nList is Empty!!") else: curr = head while True: print(curr.data, "->", end=" ") curr = curr.next if curr == head: break def display_menu(): print("==============================================" "======================") print("Menu:") print("1. Insert At Front") print("2. Insert At End") print("3. Insert After Element") print("4. Insert Before Element") print("5. Delete From Front") print("6. Delete From End") print("7. Delete A Node") print("8. Search for a element") print("9. Reverse the list") print("==============================================" "======================") # Driver Code if __name__ == "__main__": choice = None repeat_menu = 'y' head = None display_menu() while repeat_menu.lower() == 'y': choice = int(input("\nEnter Your Choice: ")) if choice == 1: head = insert_front(head) display(head) elif choice == 2: head = insert_end(head) display(head) elif choice == 3: head = insert_after(head) display(head) elif choice == 4: head = insert_before(head) display(head) elif choice == 5: head = delete_front(head) display(head) elif choice == 6: head = delete_end(head) display(head) elif choice == 7: head = delete_mid(head) display(head) elif choice == 8: search(head) elif choice == 9: head = reverse(head) display(head) else: print("\nWrong Choice!!!") display_menu() repeat_menu = input("\nEnter More(Y/N): ")
Output:
1. void insert_front (node **head):

2. void insert_end (node **head):

3. void insert_after (node **head):

4. void insert_before (node **head):

5. void delete_front (node **head):

6. void delete_end (node **head):

7. void delete_mid (node **head):

8. void search (node *head):

9. void reverse (node **head):

Similar Reads
C++ Program to Rotate Doubly linked list by N nodes
Given a doubly linked list, rotate the linked list counter-clockwise by N nodes. Here N is a given positive integer and is smaller than the count of nodes in linked list. N = 2Rotated List: Examples: Input : a b c d e N = 2 Output : c d e a b Input : a b c d e f g h N = 4 Output : e f g h a b c d As
4 min read
C++ Program For Inserting Node In The Middle Of The Linked List
Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input : list: 1->2->4->5 x = 3 Output : 1->2->
5 min read
C++ Program For Deleting A Node In A Doubly Linked List
Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list
4 min read
C++ Program To Delete Middle Of Linked List
Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g
4 min read
C++ Program To Delete Alternate Nodes Of A Linked List
Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm
3 min read
C++ Program For Removing Middle Points From a Linked List Of Line Segments
Given a linked list of coordinates where adjacent points either form a vertical line or a horizontal line. Delete points from the linked list which are in the middle of a horizontal or vertical line.Examples: Input: (0,10)->(1,10)->(5,10)->(7,10) | (7,5)->(20,5)->(40,5) Output: Linked
4 min read
How to Implement Iterator for a Doubly Linked List in C++?
In C++, iterators are used to access elements of a container sequentially without exposing the underlying representation. When working with data structures like a doubly linked list, implementing an iterator can greatly enhance the usability and efficiency of the list as it allows users to traverse
4 min read
C++ Program For Inserting A Node In A Linked List
Inserting a node into a linked list can be done in several ways, depending on where we want to insert the new node. Here, we'll cover four common scenarios: inserting at the front of the list, after a given node, at a specific position, and at the end of the list Table of Content Insert a Node at th
9 min read
C++ Program For Rearranging A Given Linked List In-Place
Given a singly linked list L0 -> L1 -> ⦠-> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ...You are required to do this in place without altering the nodes' values. Examples: Input: 1 -> 2 -> 3 -
7 min read
C++ Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order
Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where
8 min read