Untitled

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

#define MAX_SIZE 100 

int deque[MAX_SIZE]; 
int front = -1; 
int rear = -1;
char x;
// Function to add an element at the rear end of the deque
void addRear(int x) {
    if (rear == MAX_SIZE - 1) {
        printf("Deque is full. Cannot add  at the rear.\n");
        return;
    } 
    else {
        rear++;
        deque[rear]=x;
        return;
    }
    
}

// Function to add an element at the front end of the deque
void addFront(int x) {
    if (front == -1) {
        printf("Deque is full at the front. Cannot add .\n");
        return;
    }
    else {
        deque[front]=x;
        front--;
        return;
    }
}

// Function to delete an element from the rear end of the deque
char deleteRear() {
    if (front == rear) {
        printf("Deque is empty. Cannot delete from the rear.\n");
        
    }
    else {
        x = deque[rear];
        rear--;
        return x; 
    }
}

// Function to delete an element from the front end of the deque
char deleteFront() {
    if (front == rear) {
        printf("Deque is empty. Cannot delete from the front.\n");
        }
    else {
        front++;
        x=deque[front];
        return x; 
    }
}

// Function to print the deque (for testing purposes)
void printDeque() {
    if (front == rear) {
        printf("Deque is empty.\n");
        return;
    }
    printf("Deque elements: ");
    for (int i = front+1; i <= rear; i++) {
        printf("%d ", deque[i]);
    }
    printf("\n");
}

int main() {
    addRear(10);
    addRear(20);
    printDeque();
    deleteRear();
    printDeque();
    addRear(30);
    printDeque();
    deleteFront();
    addFront(2);
    printDeque();
    return 0;
}
Leave a Comment