Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define STATIC_CAPACITY 100
- // Static Deque
- typedef struct {
- int deque[STATIC_CAPACITY];
- int front, rear;
- int size;
- } StaticDeque;
- void initStaticDeque(StaticDeque *deque) {
- deque->front = 0;
- deque->rear = -1;
- deque->size = 0;
- }
- int isStaticDequeEmpty(StaticDeque *deque) {
- return deque->size == 0;
- }
- int isStaticDequeFull(StaticDeque *deque) {
- return deque->size == STATIC_CAPACITY;
- }
- void addFrontStatic(StaticDeque *deque, int item) {
- if (isStaticDequeFull(deque)) {
- printf("Static Deque is full.\n");
- return;
- }
- deque->front = (deque->front - 1 + STATIC_CAPACITY) % STATIC_CAPACITY;
- deque->deque[deque->front] = item;
- deque->size++;
- }
- void addRearStatic(StaticDeque *deque, int item) {
- if (isStaticDequeFull(deque)) {
- printf("Static Deque is full.\n");
- return;
- }
- deque->rear = (deque->rear + 1) % STATIC_CAPACITY;
- deque->deque[deque->rear] = item;
- deque->size++;
- }
- int removeFrontStatic(StaticDeque *deque) {
- if (isStaticDequeEmpty(deque)) {
- printf("Static Deque is empty.\n");
- return -1;
- }
- int removed = deque->deque[deque->front];
- deque->front = (deque->front + 1) % STATIC_CAPACITY;
- deque->size--;
- return removed;
- }
- int removeRearStatic(StaticDeque *deque) {
- if (isStaticDequeEmpty(deque)) {
- printf("Static Deque is empty.\n");
- return -1;
- }
- int removed = deque->deque[deque->rear];
- deque->rear = (deque->rear - 1 + STATIC_CAPACITY) % STATIC_CAPACITY;
- deque->size--;
- return removed;
- }
- int getFrontStatic(StaticDeque *deque) {
- if (isStaticDequeEmpty(deque)) {
- printf("Static Deque is empty.\n");
- return -1;
- }
- return deque->deque[deque->front];
- }
- int getRearStatic(StaticDeque *deque) {
- if (isStaticDequeEmpty(deque)) {
- printf("Static Deque is empty.\n");
- return -1;
- }
- return deque->deque[deque->rear];
- }
- // Dynamic Deque
- typedef struct Node {
- int data;
- struct Node* prev;
- struct Node* next;
- } Node;
- typedef struct {
- Node* front;
- Node* rear;
- int size;
- } DynamicDeque;
- Node* createNode(int item) {
- Node* newNode = (Node*)malloc(sizeof(Node));
- if (newNode == NULL) {
- printf("Memory allocation failed.\n");
- exit(EXIT_FAILURE);
- }
- newNode->data = item;
- newNode->prev = NULL;
- newNode->next = NULL;
- return newNode;
- }
- void initDynamicDeque(DynamicDeque *deque) {
- deque->front = NULL;
- deque->rear = NULL;
- deque->size = 0;
- }
- int isDynamicDequeEmpty(DynamicDeque *deque) {
- return deque->size == 0;
- }
- void addFrontDynamic(DynamicDeque *deque, int item) {
- Node* newNode = createNode(item);
- if (isDynamicDequeEmpty(deque)) {
- deque->front = deque->rear = newNode;
- } else {
- newNode->next = deque->front;
- deque->front->prev = newNode;
- deque->front = newNode;
- }
- deque->size++;
- }
- void addRearDynamic(DynamicDeque *deque, int item) {
- Node* newNode = createNode(item);
- if (isDynamicDequeEmpty(deque)) {
- deque->front = deque->rear = newNode;
- } else {
- newNode->prev = deque->rear;
- deque->rear->next = newNode;
- deque->rear = newNode;
- }
- deque->size++;
- }
- int removeFrontDynamic(DynamicDeque *deque) {
- if (isDynamicDequeEmpty(deque)) {
- printf("Dynamic Deque is empty.\n");
- return -1;
- }
- int removed = deque->front->data;
- Node* temp = deque->front;
- if (deque->front == deque->rear) {
- deque->front = deque->rear = NULL;
- } else {
- deque->front = deque->front->next;
- deque->front->prev = NULL;
- }
- free(temp);
- deque->size--;
- return removed;
- }
- int removeRearDynamic(DynamicDeque *deque) {
- if (isDynamicDequeEmpty(deque)) {
- printf("Dynamic Deque is empty.\n");
- return -1;
- }
- int removed = deque->rear->data;
- Node* temp = deque->rear;
- if (deque->front == deque->rear) {
- deque->front = deque->rear = NULL;
- } else {
- deque->rear = deque->rear->prev;
- deque->rear->next = NULL;
- }
- free(temp);
- deque->size--;
- return removed;
- }
- int getFrontDynamic(DynamicDeque *deque) {
- if (isDynamicDequeEmpty(deque)) {
- printf("Dynamic Deque is empty.\n");
- return -1;
- }
- return deque->front->data;
- }
- int getRearDynamic(DynamicDeque *deque) {
- if (isDynamicDequeEmpty(deque)) {
- printf("Dynamic Deque is empty.\n");
- return -1;
- }
- return deque->rear->data;
- }
- void freeDynamicDeque(DynamicDeque *deque) {
- while (!isDynamicDequeEmpty(deque)) {
- removeFrontDynamic(deque);
- }
- }
- int main() {
- // Static Deque
- StaticDeque staticDeque;
- initStaticDeque(&staticDeque);
- addFrontStatic(&staticDeque, 5);
- addRearStatic(&staticDeque, 10);
- printf("Static Deque Front element: %d\n", getFrontStatic(&staticDeque));
- printf("Static Deque Rear element: %d\n", getRearStatic(&staticDeque));
- removeFrontStatic(&staticDeque);
- removeRearStatic(&staticDeque);
- // Dynamic Deque
- DynamicDeque dynamicDeque;
- initDynamicDeque(&dynamicDeque);
- addFrontDynamic(&dynamicDeque, 15);
- addFrontDynamic(&dynamicDeque, 16);
- addFrontDynamic(&dynamicDeque, 17);
- addRearDynamic(&dynamicDeque, 20);
- printf("Dynamic Deque Front element: %d\n", getFrontDynamic(&dynamicDeque));
- printf("Dynamic Deque Rear element: %d\n", getRearDynamic(&dynamicDeque));
- removeFrontDynamic(&dynamicDeque);
- removeRearDynamic(&dynamicDeque);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement