Menu Driven Program to implement all the operations of Doubly Circular Linked List

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 LLvoid 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 LLvoid 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 LLvoid 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 LLvoid 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 LLvoid 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 LLvoid 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 LLvoid 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 LLvoid 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 Listvoid 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 listvoid 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 Codeint 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;} |
Python3
# Python program for the above approachÂ
class Node:    def __init__(self,data):        self.data = data        self.prev = None        self.next = None         # Function to add the node at the front# of the doubly circular LLdef insert_front(head):    # Function to insert node in front of list    print("Enter Data for New Node: ")Â
    # Create a new node named new_node    new_node = Node(int(input()))         if head == None:        # 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        new_node.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 LLdef insert_end(head):    # Function to insert node at    # last of list    print("Enter Data for New Node: ")Â
    # Create new node    new_node = None(int(input()))Â
    if (head == None):        # 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        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 LLdef insert_after(head):    # Function to enter a node after    # the element entered by userÂ
    # Create new node    new_node = Node(int(input("Enter Data for New Node: ")))         if (head == None):        # If there is no element in        # the list then create a node        # pointing to itself and make        # it head        print("There is No element in the List")        print("Creating a new node")        new_node.prev = new_node         new_node.next = new_node         head = new_node     else:        # Ask user after which node new        # node is to be inserted        num = int(input("Enter After Element:")) Â
        # temp node to traverse list        # and point to current element        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):                print("Entered Element Not Found in List")                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 LLdef insert_before(head):    # Function to enter node before    # a node entered by the userÂ
    if (head == None) :Â
        # If there is no element in the        # list create new node and make        # it head        print("List is Empty!! Creating New node...")        new_node = Node(int(input("Enter Data for New Node:"))) Â
        new_node.prev = new_node         new_node.next = new_node         head = new_node     else :        # Ask user before which node        # new node is to be inserted        num = int(input("Enter Before Element: "))Â
        # 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            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                    print("Entered Element Not Found in List!!")                     return                                curr = curr.next                          new_node = Node(int(input("Enter Data For New Node: ")))Â
            # 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 LLdef delete_front(head):    # Function to delete a node    # from front of list    if (head == None):        # If list is already empty        # print a message        print("List in empty!!")          elif (head.next == head):        # If head is the only element        # in the list delete head and        # assign it to None        head = None          else :        # 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        head = NoneÂ
        # assign head to temp        head = curr       Â
# Function to delete the end node# of doubly circular LLdef delete_end(head):    # Function to delete a node    # from end of list    if (head == None):Â
        # If list is already empty        # print a message        print("List is Empty!!")          elif (head.next == head) :Â
        # If head is the only element        # in the list delete head and        # assign it to None        head = None          else :Â
        # Create temporary node curr        # to traverse list and point        # to current elementÂ
        # 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        curr = None        Â
# Function to delete the middle node# of doubly circular LLdef delete_mid(head):    # Function to delete a node    # entered by user         if (head == None):        # If list is already empty        # print a message        print("List is Empty!!!")    else:        num = int(input("Enter Element to be deleted: "))Â
        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            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                    print("Entered Element Not Found in List!!")                    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             curr = None                # Function to search any node in the# doubly circular LLdef search(head):    if (head == None) :        # If head is None list is empty        print("List is empty!!")        return    Â
Â
    # Ask user to enter item to    # be searched    item = int(input("Enter item to be searched: "))Â
    # curr pointer is used to    # traverse list it will point    # to the current element    curr = head.nextÂ
    index = 0    count = 0    if item == head.data:        print("Item found at position:",1)             while (curr != head):        # If data in curr is equal to item        # to be searched print its position        # index+1        if (curr.data == item) :            print("Item found at position:",index + 1)            # increment count            count += 1Â
        # Index will increment by 1 in        # each iteration        index += 1        curr = curr.nextÂ
Â
    # If count is still 0 that means    # item is not found    if (count == 0):        print("Item searched not found in list")  Â
# Function to reverse the doubly# circular Linked Listdef reverse(head):    if (head == None):        # If head is None list is empty        print("List is Empty !!")        return     Â
    # curr is used to traverse list    curr = head     while (curr.next != head):Â
        # use a temp node to store        # address of next of curr        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    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 listdef display(head):    curr = head.next    if (curr == None):        print("List is Empty!!")    else :        print(head.data,'.')        while (curr != head):             print(curr.data,".")            curr = curr.next      Â
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 a the list") Â Â Â Â print("====================================================================")Â
# Driver Codechoice = 0repeat_menu = 'y'Â
# Declaration of head nodehead = Nonedisplay_menu() while (repeat_menu == 'y' or repeat_menu == 'Y'):    choice = int(input("Enter Your Choice: "))    if choice == 1:        insert_front(head)         display(head)     elif choice == 2:        insert_end(head)         display(head)    elif choice == 3:        insert_after(head)         display(head)     elif choice == 4:        insert_before(head)         display(head)     elif choice == 5:        delete_front(head)         display(head)     elif choice == 6:        delete_end(head)         display(head)     elif choice == 7:        delete_mid(head)         display(head)     elif choice == 8:        search(head)     elif choice == 9:        reverse(head)         display(head)     else:        print("Wrong Choice!!!")        display_menu()     repeat_menu = input("Enter 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):
Â
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




