Advertisement
vallec

Zadacha 2

Dec 17th, 2023
996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define STATIC_CAPACITY 100
  4.  
  5. // Static Deque
  6.  
  7. typedef struct {
  8.     int deque[STATIC_CAPACITY];
  9.     int front, rear;
  10.     int size;
  11. } StaticDeque;
  12.  
  13. void initStaticDeque(StaticDeque *deque) {
  14.     deque->front = 0;
  15.     deque->rear = -1;
  16.     deque->size = 0;
  17. }
  18.  
  19. int isStaticDequeEmpty(StaticDeque *deque) {
  20.     return deque->size == 0;
  21. }
  22.  
  23. int isStaticDequeFull(StaticDeque *deque) {
  24.     return deque->size == STATIC_CAPACITY;
  25. }
  26.  
  27. void addFrontStatic(StaticDeque *deque, int item) {
  28.     if (isStaticDequeFull(deque)) {
  29.         printf("Static Deque is full.\n");
  30.         return;
  31.     }
  32.     deque->front = (deque->front - 1 + STATIC_CAPACITY) % STATIC_CAPACITY;
  33.     deque->deque[deque->front] = item;
  34.     deque->size++;
  35. }
  36.  
  37. void addRearStatic(StaticDeque *deque, int item) {
  38.     if (isStaticDequeFull(deque)) {
  39.         printf("Static Deque is full.\n");
  40.         return;
  41.     }
  42.     deque->rear = (deque->rear + 1) % STATIC_CAPACITY;
  43.     deque->deque[deque->rear] = item;
  44.     deque->size++;
  45. }
  46.  
  47. int removeFrontStatic(StaticDeque *deque) {
  48.     if (isStaticDequeEmpty(deque)) {
  49.         printf("Static Deque is empty.\n");
  50.         return -1;
  51.     }
  52.     int removed = deque->deque[deque->front];
  53.     deque->front = (deque->front + 1) % STATIC_CAPACITY;
  54.     deque->size--;
  55.     return removed;
  56. }
  57.  
  58. int removeRearStatic(StaticDeque *deque) {
  59.     if (isStaticDequeEmpty(deque)) {
  60.         printf("Static Deque is empty.\n");
  61.         return -1;
  62.     }
  63.     int removed = deque->deque[deque->rear];
  64.     deque->rear = (deque->rear - 1 + STATIC_CAPACITY) % STATIC_CAPACITY;
  65.     deque->size--;
  66.     return removed;
  67. }
  68.  
  69. int getFrontStatic(StaticDeque *deque) {
  70.     if (isStaticDequeEmpty(deque)) {
  71.         printf("Static Deque is empty.\n");
  72.         return -1;
  73.     }
  74.     return deque->deque[deque->front];
  75. }
  76.  
  77. int getRearStatic(StaticDeque *deque) {
  78.     if (isStaticDequeEmpty(deque)) {
  79.         printf("Static Deque is empty.\n");
  80.         return -1;
  81.     }
  82.     return deque->deque[deque->rear];
  83. }
  84.  
  85. // Dynamic Deque
  86.  
  87. typedef struct Node {
  88.     int data;
  89.     struct Node* prev;
  90.     struct Node* next;
  91. } Node;
  92.  
  93. typedef struct {
  94.     Node* front;
  95.     Node* rear;
  96.     int size;
  97. } DynamicDeque;
  98.  
  99. Node* createNode(int item) {
  100.     Node* newNode = (Node*)malloc(sizeof(Node));
  101.     if (newNode == NULL) {
  102.         printf("Memory allocation failed.\n");
  103.         exit(EXIT_FAILURE);
  104.     }
  105.     newNode->data = item;
  106.     newNode->prev = NULL;
  107.     newNode->next = NULL;
  108.     return newNode;
  109. }
  110.  
  111. void initDynamicDeque(DynamicDeque *deque) {
  112.     deque->front = NULL;
  113.     deque->rear = NULL;
  114.     deque->size = 0;
  115. }
  116.  
  117. int isDynamicDequeEmpty(DynamicDeque *deque) {
  118.     return deque->size == 0;
  119. }
  120.  
  121. void addFrontDynamic(DynamicDeque *deque, int item) {
  122.     Node* newNode = createNode(item);
  123.     if (isDynamicDequeEmpty(deque)) {
  124.         deque->front = deque->rear = newNode;
  125.     } else {
  126.         newNode->next = deque->front;
  127.         deque->front->prev = newNode;
  128.         deque->front = newNode;
  129.     }
  130.     deque->size++;
  131. }
  132.  
  133. void addRearDynamic(DynamicDeque *deque, int item) {
  134.     Node* newNode = createNode(item);
  135.     if (isDynamicDequeEmpty(deque)) {
  136.         deque->front = deque->rear = newNode;
  137.     } else {
  138.         newNode->prev = deque->rear;
  139.         deque->rear->next = newNode;
  140.         deque->rear = newNode;
  141.     }
  142.     deque->size++;
  143. }
  144.  
  145. int removeFrontDynamic(DynamicDeque *deque) {
  146.     if (isDynamicDequeEmpty(deque)) {
  147.         printf("Dynamic Deque is empty.\n");
  148.         return -1;
  149.     }
  150.     int removed = deque->front->data;
  151.     Node* temp = deque->front;
  152.     if (deque->front == deque->rear) {
  153.         deque->front = deque->rear = NULL;
  154.     } else {
  155.         deque->front = deque->front->next;
  156.         deque->front->prev = NULL;
  157.     }
  158.     free(temp);
  159.     deque->size--;
  160.     return removed;
  161. }
  162.  
  163. int removeRearDynamic(DynamicDeque *deque) {
  164.     if (isDynamicDequeEmpty(deque)) {
  165.         printf("Dynamic Deque is empty.\n");
  166.         return -1;
  167.     }
  168.     int removed = deque->rear->data;
  169.     Node* temp = deque->rear;
  170.     if (deque->front == deque->rear) {
  171.         deque->front = deque->rear = NULL;
  172.     } else {
  173.         deque->rear = deque->rear->prev;
  174.         deque->rear->next = NULL;
  175.     }
  176.     free(temp);
  177.     deque->size--;
  178.     return removed;
  179. }
  180.  
  181. int getFrontDynamic(DynamicDeque *deque) {
  182.     if (isDynamicDequeEmpty(deque)) {
  183.         printf("Dynamic Deque is empty.\n");
  184.         return -1;
  185.     }
  186.     return deque->front->data;
  187. }
  188.  
  189. int getRearDynamic(DynamicDeque *deque) {
  190.     if (isDynamicDequeEmpty(deque)) {
  191.         printf("Dynamic Deque is empty.\n");
  192.         return -1;
  193.     }
  194.     return deque->rear->data;
  195. }
  196.  
  197. void freeDynamicDeque(DynamicDeque *deque) {
  198.     while (!isDynamicDequeEmpty(deque)) {
  199.         removeFrontDynamic(deque);
  200.     }
  201. }
  202.  
  203. int main() {
  204.     // Static Deque
  205.     StaticDeque staticDeque;
  206.     initStaticDeque(&staticDeque);
  207.  
  208.     addFrontStatic(&staticDeque, 5);
  209.     addRearStatic(&staticDeque, 10);
  210.  
  211.     printf("Static Deque Front element: %d\n", getFrontStatic(&staticDeque));
  212.     printf("Static Deque Rear element: %d\n", getRearStatic(&staticDeque));
  213.  
  214.     removeFrontStatic(&staticDeque);
  215.     removeRearStatic(&staticDeque);
  216.  
  217.     // Dynamic Deque
  218.     DynamicDeque dynamicDeque;
  219.     initDynamicDeque(&dynamicDeque);
  220.  
  221.     addFrontDynamic(&dynamicDeque, 15);
  222.     addFrontDynamic(&dynamicDeque, 16);
  223.     addFrontDynamic(&dynamicDeque, 17);
  224.     addRearDynamic(&dynamicDeque, 20);
  225.  
  226.     printf("Dynamic Deque Front element: %d\n", getFrontDynamic(&dynamicDeque));
  227.     printf("Dynamic Deque Rear element: %d\n", getRearDynamic(&dynamicDeque));
  228.     removeFrontDynamic(&dynamicDeque);
  229.     removeRearDynamic(&dynamicDeque);
  230.  
  231.     return 0;
  232. }
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement