Advertisement
DaniDori

задача про синус

Oct 2nd, 2023
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // Структура для представления элемента дека
  6. struct DequeNode {
  7.     int data;
  8.     struct DequeNode* next;
  9.     struct DequeNode* prev;
  10. };
  11.  
  12. // Структура для представления дека
  13. struct Deque {
  14.     struct DequeNode* front;
  15.     struct DequeNode* rear;
  16. };
  17.  
  18. // Инициализация дека
  19. void initializeDeque(struct Deque* dq) {
  20.     dq->front = dq->rear = NULL;
  21. }
  22.  
  23. // Проверка, пуст ли дек
  24. int isEmpty(struct Deque* dq) {
  25.     return dq->front == NULL;
  26. }
  27.  
  28. // Добавление элемента в левый конец дека
  29. void pushFront(struct Deque* dq, int data) {
  30.     struct DequeNode* newNode = (struct DequeNode*)malloc(sizeof(struct DequeNode));
  31.     newNode->data = data;
  32.     newNode->next = dq->front;
  33.     newNode->prev = NULL;
  34.  
  35.     if (isEmpty(dq)) {
  36.         dq->front = dq->rear = newNode;
  37.     }
  38.     else {
  39.         dq->front->prev = newNode;
  40.         dq->front = newNode;
  41.     }
  42. }
  43.  
  44. // Добавление элемента в правый конец дека
  45. void pushRear(struct Deque* dq, int data) {
  46.     struct DequeNode* newNode = (struct DequeNode*)malloc(sizeof(struct DequeNode));
  47.     newNode->data = data;
  48.     newNode->next = NULL;
  49.     newNode->prev = dq->rear;
  50.  
  51.     if (isEmpty(dq)) {
  52.         dq->front = dq->rear = newNode;
  53.     }
  54.     else {
  55.         dq->rear->next = newNode;
  56.         dq->rear = newNode;
  57.     }
  58. }
  59.  
  60. // Удаление элемента из левого конца дека
  61. int popFront(struct Deque* dq) {
  62.     if (isEmpty(dq)) {
  63.         return 0;
  64.     }
  65.     struct DequeNode* temp = dq->front;
  66.     int data = temp->data;
  67.     dq->front = dq->front->next;
  68.     if (dq->front == NULL) {
  69.         dq->rear = NULL;
  70.     }
  71.     else {
  72.         dq->front->prev = NULL;
  73.     }
  74.     free(temp);
  75.     return data;
  76. }
  77.  
  78. // Удаление элемента из правого конеца дека
  79. int popRear(struct Deque* dq) {
  80.     if (isEmpty(dq)) {
  81.         return 0;
  82.     }
  83.     struct DequeNode* temp = dq->rear;
  84.     int data = temp->data;
  85.     dq->rear = dq->rear->prev;
  86.     if (dq->rear == NULL) {
  87.         dq->front = NULL;
  88.     }
  89.     else {
  90.         dq->rear->next = NULL;
  91.     }
  92.     free(temp);
  93.     return data;
  94. }
  95.  
  96. // Вывод элементов дека на печать
  97. void printDeque(struct Deque* dq) {
  98.     struct DequeNode* current = dq->front;
  99.     if (current == NULL) {
  100.         printf("0\n");
  101.         return;
  102.     }
  103.     while (current != NULL) {
  104.         printf("%d ", current->data);
  105.         current = current->next;
  106.     }
  107.     printf("\n");
  108. }
  109.  
  110. int main() {
  111.     struct Deque dq;
  112.     initializeDeque(&dq);
  113.  
  114.     int num;
  115.  
  116.     while (1) {
  117.  
  118.         if (!scanf("%d", &num)) {
  119.             return 0;
  120.         }
  121.         else if (num > 0) {
  122.             for (int i = 1; i <= num; i++) {
  123.                 if (num % 2 == 0) {
  124.                     pushFront(&dq, i);
  125.                 }
  126.                 else {
  127.                     pushRear(&dq, i);
  128.                 }
  129.             }
  130.             printDeque(&dq);
  131.         }
  132.         else if (num < 0) {
  133.             num = -num; // Преобразуем отрицательное число в положительное
  134.             for (int i = 0; i < num && !isEmpty(&dq); i++) {
  135.                 if (num % 2 == 0) {
  136.                     popRear(&dq);
  137.                 }
  138.                 else {
  139.                     popFront(&dq);
  140.                 }
  141.             }
  142.             printDeque(&dq);
  143.         }
  144.  
  145.         char dot;
  146.         if (scanf("%c", &dot) == 1 && dot == '.') {
  147.             break; // Завершаем выполнение при вводе точки
  148.         }
  149.     }
  150.  
  151.     return 0;
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement