Реализация двусторонней очереди (Deque) в C с использованием массива и двусвязного списка

Ниже приведены программы на языке C, реализующие двустороннюю очередь (абстрактный тип данных) с использованием массива и двусвязного списка.

  1. Двусторонняя очередь с использованием массива:

    #include <stdio.h>
    #define MAX_SIZE 100
    int dequeue[MAX_SIZE];
    int front = -1;
    int rear = -1;
    void insertFront(int item) {
    if ((front == 0 && rear == MAX_SIZE - 1) || (front == rear + 1)) {
        printf("Queue is full.\n");
        return;
    } else if (front == -1 && rear == -1) {
        front = rear = 0;
    } else if (front == 0) {
        front = MAX_SIZE - 1;
    } else {
        front--;
    }
    dequeue[front] = item;
    printf("Item %d inserted at the front.\n", item);
    }
    void insertRear(int item) {
    if ((front == 0 && rear == MAX_SIZE - 1) || (front == rear + 1)) {
        printf("Queue is full.\n");
        return;
    } else if (front == -1 && rear == -1) {
        front = rear = 0;
    } else if (rear == MAX_SIZE - 1) {
        rear = 0;
    } else {
        rear++;
    }
    dequeue[rear] = item;
    printf("Item %d inserted at the rear.\n", item);
    }
    void deleteFront() {
    if (front == -1 && rear == -1) {
        printf("Queue is empty.\n");
        return;
    } else if (front == rear) {
        printf("Item %d deleted from the front.\n", dequeue[front]);
        front = rear = -1;
    } else if (front == MAX_SIZE - 1) {
        printf("Item %d deleted from the front.\n", dequeue[front]);
        front = 0;
    } else {
        printf("Item %d deleted from the front.\n", dequeue[front]);
        front++;
    }
    }
    void deleteRear() {
    if (front == -1 && rear == -1) {
        printf("Queue is empty.\n");
        return;
    } else if (front == rear) {
        printf("Item %d deleted from the rear.\n", dequeue[rear]);
        front = rear = -1;
    } else if (rear == 0) {
        printf("Item %d deleted from the rear.\n", dequeue[rear]);
        rear = MAX_SIZE - 1;
    } else {
        printf("Item %d deleted from the rear.\n", dequeue[rear]);
        rear--;
    }
    }
    void display() {
    if (front == -1 && rear == -1) {
        printf("Queue is empty.\n");
        return;
    }
    printf("Queue elements: ");
    int i = front;
    while (i != rear) {
        printf("%d ", dequeue[i]);
        if (i == MAX_SIZE - 1) {
            i = 0;
        } else {
            i++;
        }
    }
    printf("%d\n", dequeue[i]);
    }
    int main() {
    insertFront(5);
    insertRear(10);
    insertFront(3);
    display();
    deleteFront();
    display();
    deleteRear();
    display();
    
    return 0;
    }
  2. Двусторонняя очередь с использованием двусвязного списка:

    #include <stdio.h>
    #include <stdlib.h>
    struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
    };
    struct Node* front = NULL;
    struct Node* rear = NULL;
    void insertFront(int item) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = item;
    newNode->prev = NULL;
    newNode->next = front;
    if (front == NULL) {
        front = rear = newNode;
    } else {
        front->prev = newNode;
        front = newNode;
    }
    printf("Item %d inserted at the front.\n", item);
    }
    void insertRear(int item) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = item;
    newNode->prev = rear;
    newNode->next = NULL;
    if (rear == NULL) {
        front = rear = newNode;
    } else {
        rear->next = newNode;
        rear = newNode;
    }
    printf("Item %d inserted at the rear.\n", item);
    }
    void deleteFront() {
    if (front == NULL && rear == NULL) {
        printf("Queue is empty.\n");
        return;
    } else if (front == rear) {
        printf("Item %d deleted from the front.\n", front->data);
        free(front);
        front = rear = NULL;
    } else {
        struct Node* temp = front;
        printf("Item %d deleted from the front.\n", temp->data);
        front = front->next;
        front->prev = NULL;
        free(temp);
    }
    }
    void deleteRear() {
    if (front == NULL && rear == NULL) {
        printf("Queue is empty.\n");
        return;
    } else if (front == rear) {
        printf("Item %d deleted from the rear.\n", rear->data);
        free(rear);
        front = rear = NULL;
    } else {
        struct Node* temp = rear;
        printf("Item %d deleted from the rear.\n", temp->data);
        rear = rear->prev;
        rear->next = NULL;
        free(temp);
    }
    }
    void display() {
    if (front == NULL && rear == NULL) {
        printf("Queue is empty.\n");
        return;
    }
    printf("Queue elements: ");
    struct Node* current = front;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
    }
    int main() {
    insertFront(5);
    insertRear(10);
    insertFront(3);
    display();
    deleteFront();
    display();
    deleteRear();
    display();
    
    return 0;
    }