Untitled

 avatar
unknown
plain_text
a month ago
17 kB
3
Indexable
#include <stdio.h>
#include <stdlib.h>

// Define a Node structure
struct Node {
        int data;
            struct Node* next;
                struct Node* prev;
};

// Function to insert a node at the beginning
void insertAtBeginning(struct Node** head, int data) {
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
            newNode->data = data;
                newNode->next = *head;
                    newNode->prev = NULL;

                        if (*head != NULL)
                                (*head)->prev = newNode;

                                    *head = newNode;
}

// Function to insert a node at the end
void insertAtEnd(struct Node** head, int data) {
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
            struct Node* temp = *head;
                newNode->data = data;
                    newNode->next = NULL;

                        if (*head == NULL) {
                                    newNode->prev = NULL;
                                            *head = newNode;
                                                    return;
                        }

                            while (temp->next != NULL)
                                    temp = temp->next;

                                        temp->next = newNode;
                                            newNode->prev = temp;
}

// Function to delete a node from the list
void deleteNode(struct Node** head, int key) {
        struct Node* temp = *head;

            if (*head == NULL) {
                        printf("List is empty.\n");
                                return;
            }

                while (temp != NULL && temp->data != key)
                        temp = temp->next;

                            if (temp == NULL) {
                                        printf("Element not found.\n");
                                                return;
                            }

                                if (temp->prev != NULL)
                                        temp->prev->next = temp->next;
                                            else
                                                    *head = temp->next;

                                                        if (temp->next != NULL)
                                                                temp->next->prev = temp->prev;

                                                                    free(temp);
                                                                        printf("Element %d deleted.\n", key);
}

// Function to display the list in forward direction
void displayForward(struct Node* head) {
        struct Node* temp = head;
            printf("Forward List: ");
                while (temp != NULL) {
                            printf("%d -> ", temp->data);
                                    temp = temp->next;
                }
                    printf("NULL\n");
}

// Function to display the list in reverse direction
void displayReverse(struct Node* head) {
        struct Node* temp = head;
            if (temp == NULL) {
                        printf("List is empty.\n");
                                return;
            }

                while (temp->next != NULL)
                        temp = temp->next;

                            printf("Reverse List: ");
                                while (temp != NULL) {
                                            printf("%d -> ", temp->data);
                                                    temp = temp->prev;
                                }
                                    printf("NULL\n");
}

// Function to search for an element in the list
void search(struct Node* head, int key) {
        struct Node* temp = head;
            int pos = 1;

                while (temp != NULL) {
                            if (temp->data == key) {
                                            printf("Element %d found at position %d\n", key, pos);
                                                        return;
                            }
                                    temp = temp->next;
                                            pos++;
                }

                    printf("Element %d not found in the list.\n", key);
}

// Main function
int main() {
        struct Node* head = NULL;
            int choice, value;

                while (1) {
                            printf("\nDoubly Linked List Operations:\n");
                                    printf("1. Insert at Beginning\n");
                                            printf("2. Insert at End\n");
                                                    printf("3. Delete Node\n");
                                                            printf("4. Display Forward\n");
                                                                    printf("5. Display Reverse\n");
                                                                            printf("6. Search\n");
                                                                                    printf("7. Exit\n");
                                                                                            printf("Enter your choice: ");
                                                                                                    scanf("%d", &choice);

                                                                                                            switch (choice) {
                                                                                                                            case 1:
                                                                                                                                            printf("Enter value: ");
                                                                                                                                                            scanf("%d", &value);
                                                                                                                                                                            insertAtBeginning(&head, value);
                                                                                                                                                                                            break;
                                                                                                                                                                                                        case 2:
                                                                                                                                                                                                                        printf("Enter value: ");
                                                                                                                                                                                                                                        scanf("%d", &value);
                                                                                                                                                                                                                                                        insertAtEnd(&head, value);
                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                    case 3:
                                                                                                                                                                                                                                                                                                    printf("Enter value to delete: ");
                                                                                                                                                                                                                                                                                                                    scanf("%d", &value);
                                                                                                                                                                                                                                                                                                                                    deleteNode(&head, value);
                                                                                                                                                                                                                                                                                                                                                    break;
                                                                                                                                                                                                                                                                                                                                                                case 4:
                                                                                                                                                                                                                                                                                                                                                                                displayForward(head);
                                                                                                                                                                                                                                                                                                                                                                                                break;
                                                                                                                                                                                                                                                                                                                                                                                                            case 5:
                                                                                                                                                                                                                                                                                                                                                                                                                            displayReverse(head);
                                                                                                                                                                                                                                                                                                                                                                                                                                            break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                        case 6:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        printf("Enter value to search: ");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        scanf("%d", &value);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        search(head, value);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    case 7:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    exit(0);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                default:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                printf("Invalid choice! Please try again.\n");
                                                                                                            }
                }
                    return 0;
}
                                                                                                            }
                }
}
                            }
                }
}
                                }
            }
}
                }
}
                            }
            }
}
                        }
}
}
}
Editor is loading...
Leave a Comment