data = data; node->prev = NULL; node->next = NULL; return node; } // Add node at the end void addLast(DoublyLinkedList* list, int data) { Node* node = newNode(data); if (list->size == 0) { list->head = list->tail = node; } else { node->prev = list->tail; list->tail->next = node; list->tail = node; } list->size++; } // Add node at the beginning void "> data = data; node->prev = NULL; node->next = NULL; return node; } // Add node at the end void addLast(DoublyLinkedList* list, int data) { Node* node = newNode(data); if (list->size == 0) { list->head = list->tail = node; } else { node->prev = list->tail; list->tail->next = node; list->tail = node; } list->size++; } // Add node at the beginning void "> data = data; node->prev = NULL; node->next = NULL; return node; } // Add node at the end void addLast(DoublyLinkedList* list, int data) { Node* node = newNode(data); if (list->size == 0) { list->head = list->tail = node; } else { node->prev = list->tail; list->tail->next = node; list->tail = node; } list->size++; } // Add node at the beginning void ">
#include <stdio.h>
#include <stdlib.h>

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

// Doubly Linked List structure
typedef struct {
    Node* head;
    Node* tail;
    int size;
} DoublyLinkedList;

// Initialize the list
void init(DoublyLinkedList* list) {
    list->head = NULL;
    list->tail = NULL;
    list->size = 0;
}

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

// Add node at the end
void addLast(DoublyLinkedList* list, int data) {
    Node* node = newNode(data);
    if (list->size == 0) {
        list->head = list->tail = node;
    } else {
        node->prev = list->tail;
        list->tail->next = node;
        list->tail = node;
    }
    list->size++;
}

// Add node at the beginning
void addFirst(DoublyLinkedList* list, int data) {
    Node* node = newNode(data);
    if (list->size == 0) {
        list->head = list->tail = node;
    } else {
        node->next = list->head;
        list->head->prev = node;
        list->head = node;
    }
    list->size++;
}

// Remove first node
int removeFirst(DoublyLinkedList* list) {
    if (list->size == 0) return -1;

    Node* temp = list->head;
    int data = temp->data;

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

    free(temp);
    list->size--;
    return data;
}

// Remove last node
int removeLast(DoublyLinkedList* list) {
    if (list->size == 0) return -1;

    Node* temp = list->tail;
    int data = temp->data;

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

    free(temp);
    list->size--;
    return data;
}

// Print the list
void printList(DoublyLinkedList* list) {
    Node* trav = list->head;
    printf("[ ");
    while (trav != NULL) {
        printf("%d", trav->data);
        trav = trav->next;
        if (trav != NULL) printf(", ");
    }
    printf(" ]\\n");
}

// Free all nodes
void clearList(DoublyLinkedList* list) {
    Node* trav = list->head;
    while (trav != NULL) {
        Node* next = trav->next;
        free(trav);
        trav = next;
    }
    list->head = list->tail = NULL;
    list->size = 0;
}

// Demo
int main() {
    DoublyLinkedList list;
    init(&list);

    addLast(&list, 10);
    addLast(&list, 20);
    addFirst(&list, 5);
    addLast(&list, 30);

    printList(&list);

    removeFirst(&list);
    printList(&list);

    removeLast(&list);
    printList(&list);

    clearList(&list);
    return 0;
}