/
Karen1C
/
sysdad
Обзор
Документация
Войти
/
Karen1C
/
sysdad
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
queue.c
92 строки
2 KB
Karen1C
upload files
30 май 2025, 09:38
30 май 2025, 09:38
8164eba
Код
Авторство
О чём код?
#include <stdio.h> #include <stdlib.h> // Структура узла очереди typedef struct QueueNode { int data; struct QueueNode* next; } QueueNode; typedef struct Queue { QueueNode* front; QueueNode* rear; } Queue; void initQueue(Queue* q) { q->front = NULL; q->rear = NULL; } void push(Queue* q, int value) { QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode)); if (newNode == NULL) { printf("Ошибка: не удалось выделить память\n"); return; } newNode->data = value; newNode->next = NULL; if (q->rear == NULL) { q->front = q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } printf("PUSH: %d\n", value); } int pop(Queue* q) { if (q->front == NULL) { printf("Очередь пуста (underflow)\n"); return -1; } QueueNode* temp = q->front; int value = temp->data; q->front = q->front->next; if (q->front == NULL) { q->rear = NULL; } free(temp); printf("POP: %d\n", value); return value; } void printQueue(Queue* q) { QueueNode* current = q->front; printf("Очередь: "); while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { Queue q; initQueue(&q); push(&q, 10); push(&q, 20); push(&q, 30); printQueue(&q); pop(&q); pop(&q); printQueue(&q); push(&q, 40); printQueue(&q); pop(&q); pop(&q); pop(&q); return 0; }