DOUBLE LINKED LIST by smd

 program:

#include <stdio.h>
#include <stdlib.h>

int count = 0;

struct node {
    int data;
    struct node *next;
    struct node *prev;
};
struct node *head = NULL, *newnode = NULL, *trav = NULL;

void create_list() {
    int value;
    struct node *temp;

    newnode = (struct node *)malloc(sizeof(struct node));
    printf("Enter the value to be inserted: ");
    scanf("%d", &value);

    newnode->data = value;
    newnode->next = NULL;
    newnode->prev = NULL;

    if (head == NULL) {
        head = newnode;
    } else {
        temp = head;
        while (temp->next != NULL)
            temp = temp->next;
        temp->next = newnode;
        newnode->prev = temp;
    }
    count++;
}

void insert_at_begin() {
    int value;
    newnode = (struct node *)malloc(sizeof(struct node));

    printf("Enter the value to be inserted: ");
    scanf("%d", &value);

    newnode->data = value;
    newnode->next = head;
    newnode->prev = NULL;

    if (head != NULL) {
        head->prev = newnode;
    }
    head = newnode;
    count++;
}

void insert_at_end() {
    int value;
    struct node *temp;

    newnode = (struct node *)malloc(sizeof(struct node));
    printf("Enter the value to be inserted: ");
    scanf("%d", &value);

    newnode->data = value;
    newnode->next = NULL;
    newnode->prev = NULL;

    if (head == NULL) {
        head = newnode;
    } else {
        temp = head;
        while (temp->next != NULL)
            temp = temp->next;
        temp->next = newnode;
        newnode->prev = temp;
    }
    count++;
}

void insert_at_specific_point() {
    int loc, value, i = 1;
    struct node *temp;

    if (count == 0) {
        printf("List is empty. Use insert at beginning or end.\n");
        return;
    }

    printf("Enter the position (1 to %d): ", count + 1);
    scanf("%d", &loc);

    if (loc < 1 || loc > count + 1) {
        printf("Invalid position\n");
        return;
    }

    printf("Enter the value to be inserted: ");
    scanf("%d", &value);

    newnode = (struct node *)malloc(sizeof(struct node));
    newnode->data = value;

    if (loc == 1) {
        newnode->next = head;
        newnode->prev = NULL;
        if (head != NULL)
            head->prev = newnode;
        head = newnode;
    } else {
        temp = head;
        while (i < loc - 1) {
            temp = temp->next;
            i++;
        }
        newnode->next = temp->next;
        newnode->prev = temp;
        if (temp->next != NULL)
            temp->next->prev = newnode;
        temp->next = newnode;
    }
    count++;
}

void delete_at_begin() {
    struct node *temp;

    if (head == NULL) {
        printf("There are no elements to delete\n");
        return;
    }

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

    printf("%d is deleted\n", temp->data);
    free(temp);
    count--;
}

void delete_at_end() {
    struct node *temp;

    if (head == NULL) {
        printf("There are no elements to delete\n");
        return;
    }

    temp = head;
    if (temp->next == NULL) {
        printf("%d is deleted\n", temp->data);
        free(temp);
        head = NULL;
    } else {
        while (temp->next != NULL)
            temp = temp->next;
        printf("%d is deleted\n", temp->data);
        temp->prev->next = NULL;
        free(temp);
    }
    count--;
}

void delete_at_specific_point() {
    struct node *temp;
    int pos, i = 1;

    if (head == NULL) {
        printf("There are no elements to delete\n");
        return;
    }

    printf("Enter the position to delete (1 to %d): ", count);
    scanf("%d", &pos);

    if (pos < 1 || pos > count) {
        printf("Invalid position\n");
        return;
    }

    if (pos == 1) {
        delete_at_begin();
        return;
    }
    if (pos == count) {
        delete_at_end();
        return;
    }

    temp = head;
    while (i < pos) {
        temp = temp->next;
        i++;
    }

    temp->prev->next = temp->next;
    temp->next->prev = temp->prev;
    printf("%d is deleted\n", temp->data);
    free(temp);
    count--;
}

void display() {
    trav = head;
    if (trav == NULL) {
        printf("List is empty\n");
        return;
    }

    printf("Elements in the list: ");
    while (trav != NULL) {
        printf("%d <=> ", trav->data);
        trav = trav->next;
    }
    printf("NULL\n");
}

int main() {
    int choice = 0;

    while (choice != 9) {
        printf("\nChoose the options below\n");
        printf("1. Create list\n");
        printf("2. Insert at beginning\n");
        printf("3. Insert at end\n");
        printf("4. Insert at specific point\n");
        printf("5. Delete at beginning\n");
        printf("6. Delete at end\n");
        printf("7. Delete at specific point\n");
        printf("8. Display\n");
        printf("9. Exit\n");
        scanf("%d", &choice);

        switch (choice) {
            case 1: create_list(); break;
            case 2: insert_at_begin(); break;
            case 3: insert_at_end(); break;
            case 4: insert_at_specific_point(); break;
            case 5: delete_at_begin(); break;
            case 6: delete_at_end(); break;
            case 7: delete_at_specific_point(); break;
            case 8: display(); break;
            case 9: printf("Exiting... Bye.\n"); break;
            default: printf("Invalid entry. Choose correct option\n"); break;
        }
    }
    return 0;
}

Execute the code and review the output to better understand how it works.




output:



Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 1 Enter the value to be inserted: 10 Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 1 Enter the value to be inserted: 20 Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 2 Enter the value to be inserted: 30 Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 3 Enter the value to be inserted: 50 Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 4 Enter the position (1 to 5): 3 Enter the value to be inserted: 40 Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 8 Elements in the list: 30 <=> 10 <=> 40 <=> 20 <=> 50 <=> NULL Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 6 50 is deleted Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 7 Enter the position to delete (1 to 4): 2 10 is deleted Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 5 30 is deleted Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 8 Elements in the list: 40 <=> 20 <=> NULL Choose the options below 1. Create list 2. Insert at beginning 3. Insert at end 4. Insert at specific point 5. Delete at beginning 6. Delete at end 7. Delete at specific point 8. Display 9. Exit 9 Exiting... Bye.

Comments

Popular posts from this blog

SINGLE LINKED LIST by smd

CLL by smd

QUEUE USING ARRAYS by smd