Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- // Структура для представления элемента дека
- struct DequeNode {
- int data;
- struct DequeNode* next;
- struct DequeNode* prev;
- };
- // Структура для представления дека
- struct Deque {
- struct DequeNode* front;
- struct DequeNode* rear;
- };
- // Инициализация дека
- void initializeDeque(struct Deque* dq) {
- dq->front = dq->rear = NULL;
- }
- // Проверка, пуст ли дек
- int isEmpty(struct Deque* dq) {
- return dq->front == NULL;
- }
- // Добавление элемента в левый конец дека
- void pushFront(struct Deque* dq, int data) {
- struct DequeNode* newNode = (struct DequeNode*)malloc(sizeof(struct DequeNode));
- newNode->data = data;
- newNode->next = dq->front;
- newNode->prev = NULL;
- if (isEmpty(dq)) {
- dq->front = dq->rear = newNode;
- }
- else {
- dq->front->prev = newNode;
- dq->front = newNode;
- }
- }
- // Добавление элемента в правый конец дека
- void pushRear(struct Deque* dq, int data) {
- struct DequeNode* newNode = (struct DequeNode*)malloc(sizeof(struct DequeNode));
- newNode->data = data;
- newNode->next = NULL;
- newNode->prev = dq->rear;
- if (isEmpty(dq)) {
- dq->front = dq->rear = newNode;
- }
- else {
- dq->rear->next = newNode;
- dq->rear = newNode;
- }
- }
- // Удаление элемента из левого конца дека
- int popFront(struct Deque* dq) {
- if (isEmpty(dq)) {
- return 0;
- }
- struct DequeNode* temp = dq->front;
- int data = temp->data;
- dq->front = dq->front->next;
- if (dq->front == NULL) {
- dq->rear = NULL;
- }
- else {
- dq->front->prev = NULL;
- }
- free(temp);
- return data;
- }
- // Удаление элемента из правого конеца дека
- int popRear(struct Deque* dq) {
- if (isEmpty(dq)) {
- return 0;
- }
- struct DequeNode* temp = dq->rear;
- int data = temp->data;
- dq->rear = dq->rear->prev;
- if (dq->rear == NULL) {
- dq->front = NULL;
- }
- else {
- dq->rear->next = NULL;
- }
- free(temp);
- return data;
- }
- // Вывод элементов дека на печать
- void printDeque(struct Deque* dq) {
- struct DequeNode* current = dq->front;
- if (current == NULL) {
- printf("0\n");
- return;
- }
- while (current != NULL) {
- printf("%d ", current->data);
- current = current->next;
- }
- printf("\n");
- }
- int main() {
- struct Deque dq;
- initializeDeque(&dq);
- int num;
- while (1) {
- if (!scanf("%d", &num)) {
- return 0;
- }
- else if (num > 0) {
- for (int i = 1; i <= num; i++) {
- if (num % 2 == 0) {
- pushFront(&dq, i);
- }
- else {
- pushRear(&dq, i);
- }
- }
- printDeque(&dq);
- }
- else if (num < 0) {
- num = -num; // Преобразуем отрицательное число в положительное
- for (int i = 0; i < num && !isEmpty(&dq); i++) {
- if (num % 2 == 0) {
- popRear(&dq);
- }
- else {
- popFront(&dq);
- }
- }
- printDeque(&dq);
- }
- char dot;
- if (scanf("%c", &dot) == 1 && dot == '.') {
- break; // Завершаем выполнение при вводе точки
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement