Doubly Linked List

 avatar
unknown
plain_text
10 months ago
6.0 kB
234
Indexable
#include <stdio.h>
#include <stdlib.h>

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

// Head and tail pointers
struct Node *head = NULL;
struct Node *tail = NULL;

// Function to create a new node
struct Node *createNode(int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    if (!newNode)
    {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

// Insert at the beginning
void insertAtBeginning(int data)
{
    struct Node *newNode = createNode(data);
    if (head == NULL)
    {
        head = tail = newNode;
    }
    else
    {
        newNode->next = head;
        head->prev = newNode;
        head = newNode;
    }
    printf("Inserted %d at the beginning.\n", data);
}

// Insert at the end
void insertAtEnd(int data)
{
    struct Node *newNode = createNode(data);
    if (tail == NULL)
    {
        head = tail = newNode;
    }
    else
    {
        tail->next = newNode;
        newNode->prev = tail;
        tail = newNode;
    }
    printf("Inserted %d at the end.\n", data);
}

// Insert to the left of a given position (1-based)
void insertLeftOfPosition(int data, int pos)
{
    if (pos <= 1 || head == NULL)
    {
        insertAtBeginning(data);
        return;
    }

    struct Node *temp = head;
    int count = 1;

    while (temp != NULL && count < pos)
    {
        temp = temp->next;
        count++;
    }

    if (temp == NULL)
    {
        printf("Position %d is out of range.\n", pos);
        return;
    }

    struct Node *newNode = createNode(data);
    newNode->next = temp;
    newNode->prev = temp->prev;

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

    if (temp == head)
        head = newNode;

    printf("Inserted %d to the left of position %d.\n", data, pos);
}

// Insert to the right of a given position (1-based)
void insertRightOfPosition(int data, int pos)
{
    struct Node *temp = head;
    int count = 1;

    while (temp != NULL && count < pos)
    {
        temp = temp->next;
        count++;
    }

    if (temp == NULL)
    {
        printf("Position %d is out of range.\n", pos);
        return;
    }

    struct Node *newNode = createNode(data);
    newNode->next = temp->next;
    newNode->prev = temp;

    if (temp->next != NULL)
        temp->next->prev = newNode;
    else
        tail = newNode; // if inserting after the last node, update tail

    temp->next = newNode;

    printf("Inserted %d to the right of position %d.\n", data, pos);
}

// Delete from the beginning
void deleteFromBeginning()
{
    if (head == NULL)
    {
        printf("List is empty.\n");
        return;
    }

    struct Node *temp = head;
    head = head->next;

    if (head != NULL)
        head->prev = NULL;
    else
        tail = NULL; // list became empty

    printf("Deleted %d from the beginning.\n", temp->data);
    free(temp);
}

// Delete from the end
void deleteFromEnd()
{
    if (tail == NULL)
    {
        printf("List is empty.\n");
        return;
    }

    struct Node *temp = tail;
    tail = tail->prev;

    if (tail != NULL)
        tail->next = NULL;
    else
        head = NULL; // list became empty

    printf("Deleted %d from the end.\n", temp->data);
    free(temp);
}

// Display forward
void displayForward()
{
    struct Node *temp = head;
    printf("List (forward): ");
    while (temp != NULL)
    {
        printf("%d <-> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

// Display backward
void displayBackward()
{
    struct Node *temp = tail;
    printf("List (backward): ");
    while (temp != NULL)
    {
        printf("%d <-> ", temp->data);
        temp = temp->prev;
    }
    printf("NULL\n");
}

// Main menu
int main()
{
    int choice, value, position;

    while (1)
    {
        printf("\n--- Doubly Linked List Menu ---\n");
        printf("1. Insert at Beginning\n");
        printf("2. Insert at End\n");
        printf("3. Insert at Left of Position\n");
        printf("4. Insert at Right of Position\n");
        printf("5. Delete from Beginning\n");
        printf("6. Delete from End\n");
        printf("7. Display Forward\n");
        printf("8. Display Backward\n");
        printf("0. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            printf("Enter value to insert: ");
            scanf("%d", &value);
            insertAtBeginning(value);
            break;
        case 2:
            printf("Enter value to insert: ");
            scanf("%d", &value);
            insertAtEnd(value);
            break;
        case 3:
            printf("Enter value to insert: ");
            scanf("%d", &value);
            printf("Enter position (1-based): ");
            scanf("%d", &position);
            insertLeftOfPosition(value, position);
            break;
        case 4:
            printf("Enter value to insert: ");
            scanf("%d", &value);
            printf("Enter position (1-based): ");
            scanf("%d", &position);
            insertRightOfPosition(value, position);
            break;
        case 5:
            deleteFromBeginning();
            break;
        case 6:
            deleteFromEnd();
            break;
        case 7:
            displayForward();
            break;
        case 8:
            displayBackward();
            break;
        case 0:
            printf("Exiting...\n");
            exit(0);
        default:
            printf("Invalid choice. Try again.\n");
        }
    }

    return 0;
}
Editor is loading...
Leave a Comment