Write a c program to Create a Circular DoubleLinked list and perform Following Operations A. Insertion After a particular node B. Insertion Before a particular node C. Search a particular node D. Return a particular node E. Deletion before a particular node F. Delete a particular node

 #include <stdio.h>

#include <stdlib.h>


// Define the structure for a node in the circular doubly linked list

struct Node {

    int data;

    struct Node* next;

    struct Node* prev;

};


// Function to create a new node with given data

struct Node* createNode(int data) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = data;

    newNode->next = newNode;

    newNode->prev = newNode;

    return newNode;

}


// Function to insert a node at the end of the circular doubly linked list

void insertAtEnd(struct Node** head, int data) {

    struct Node* newNode = createNode(data);

    if (*head == NULL) {

        *head = newNode;

    } else {

        struct Node* last = (*head)->prev;

        newNode->next = *head;

        (*head)->prev = newNode;

        newNode->prev = last;

        last->next = newNode;

    }

}


// Function to print the circular doubly linked list

void printList(struct Node* head) {

    if (head == NULL) {

        printf("List is empty\n");

        return;

    }

    struct Node* temp = head;

    do {

        printf("%d <-> ", temp->data);

        temp = temp->next;

    } while (temp != head);

    printf("(head)\n");

}


// Function to insert a node after a particular node

void insertAfterNode(struct Node* prevNode, int data) {

    if (prevNode == NULL) {

        printf("The given previous node cannot be NULL\n");

        return;

    }

    struct Node* newNode = createNode(data);

    newNode->next = prevNode->next;

    newNode->prev = prevNode;

    prevNode->next->prev = newNode;

    prevNode->next = newNode;

}


// Function to insert a node before a particular node

void insertBeforeNode(struct Node** head, int beforeData, int data) {

    struct Node* newNode = createNode(data);

    if (*head == NULL) {

        printf("List is empty\n");

        return;

    }

    struct Node* temp = *head;

    do {

        if (temp->data == beforeData) {

            newNode->next = temp;

            newNode->prev = temp->prev;

            temp->prev->next = newNode;

            temp->prev = newNode;

            if (temp == *head) {

                *head = newNode;

            }

            return;

        }

        temp = temp->next;

    } while (temp != *head);

    printf("Node with data %d not found\n", beforeData);

}


// Function to search for a particular node

int searchNode(struct Node* head, int data) {

    struct Node* temp = head;

    int position = 1;

    if (head == NULL) return -1;

    do {

        if (temp->data == data) {

            return position;

        }

        temp = temp->next;

        position++;

    } while (temp != head);

    return -1; // Not found

}


// Function to return a particular node by its position

struct Node* getNodeByPosition(struct Node* head, int position) {

    if (position < 1) {

        printf("Position should be >= 1\n");

        return NULL;

    }

    struct Node* temp = head;

    int currentPos = 1;

    if (head == NULL) return NULL;

    do {

        if (currentPos == position) {

            return temp;

        }

        temp = temp->next;

        currentPos++;

    } while (temp != head);

    printf("Position out of range\n");

    return NULL;

}


// Function to delete a node before a particular node

void deleteBeforeNode(struct Node** head, int beforeData) {

    if (*head == NULL || (*head)->next == *head) {

        printf("List is empty or has only one node\n");

        return;

    }

    struct Node* temp = *head;

    do {

        if (temp->data == beforeData) {

            struct Node* toDelete = temp->prev;

            if (toDelete == *head) {

                *head = toDelete->next;

            }

            toDelete->prev->next = toDelete->next;

            toDelete->next->prev = toDelete->prev;

            free(toDelete);

            return;

        }

        temp = temp->next;

    } while (temp != *head);

    printf("Node with data %d not found\n", beforeData);

}


// Function to delete a particular node by its data

void deleteNode(struct Node** head, int data) {

    if (*head == NULL) {

        printf("List is empty\n");

        return;

    }

    struct Node* temp = *head;

    do {

        if (temp->data == data) {

            if (temp->next == temp && temp->prev == temp) { // Only one node

                *head = NULL;

            } else {

                temp->prev->next = temp->next;

                temp->next->prev = temp->prev;

                if (temp == *head) {

                    *head = temp->next;

                }

            }

            free(temp);

            return;

        }

        temp = temp->next;

    } while (temp != *head);

    printf("Node with data %d not found\n", data);

}


int main() {

    struct Node* head = NULL;


    // Sample operations

    insertAtEnd(&head, 1);

    insertAtEnd(&head, 2);

    insertAtEnd(&head, 4);

    insertAtEnd(&head, 5);


    printf("Initial list: ");

    printList(head);


    printf("After inserting 3 after node with data 2: ");

    struct Node* node2 = getNodeByPosition(head, 2);

    insertAfterNode(node2, 3);

    printList(head);


    printf("After inserting 0 before node with data 1: ");

    insertBeforeNode(&head, 1, 0);

    printList(head);


    printf("Searching for node with data 4: ");

    int position = searchNode(head, 4);

    if (position != -1) {

        printf("Node found at position %d\n", position);

    } else {

        printf("Node not found\n");

    }


    printf("Node at position 3: ");

    struct Node* node = getNodeByPosition(head, 3);

    if (node != NULL) {

        printf("Node at position 3 has data: %d\n", node->data);

    }


    printf("After deleting node before node with data 2: ");

    deleteBeforeNode(&head, 2);

    printList(head);


    printf("After deleting node with data 4: ");

    deleteNode(&head, 4);

    printList(head);


    return 0;

}


Comments

Popular posts from this blog

Write a c program to Create a Circular Linked list and perform Following Operations A. Insertion At Beginning B. Insertion At End C. Insertion After a particular node Insertion Before a particular node E. Insertion at specific position F. Search a particular node G. Return a particular node H. Deletion at the beginning I. Deletion at the end J. Deletion after a particular node K. Deletion before a particular node L. Delete a particular node M. Deletion at a specific position

Write a c program to check whether the created linked list is palindrome or not

Write a c program to Create a Circular single Linked list and perform Following Operations A. Insertion After a particular node B. Insertion Before a particular node C. Search a particular node D. Return a particular node E. Deletion before a particular node F. Delete a particular node