// Циклична опашка #include #include struct Queue { int size; int front; int rear; int* Q; }; void create(struct Queue* q, int size) { q->size = size; q->front = q->rear = 0; q->Q = (int*)malloc(q->size * sizeof(int)); } void enqueue(struct Queue* q, int x) { if ((q->rear + 1) % q->size == q->front) return; // опашката е пълна else { q->rear = (q->rear + 1) % q->size; q->Q[q->rear] = x; } } int dequeue(struct Queue* q, int* number) { int x = -1; if (q->front == q->rear) return 0; // опашката е празна else { q->front = (q->front + 1) % q->size; x = q->Q[q->front]; *number = x; } return 1; } void Display(struct Queue q) { int i = q.front + 1; do { printf("%d ", q.Q[i]); i = (i + 1) % q.size; } while (i != (q.rear + 1) % q.size); printf("\n"); } int main() { struct Queue q; int size = 7; create(&q, size); // Създаване на опашка // елементите на опашката = size -1 // Добавяне на елементи enqueue(&q, 10); enqueue(&q, 11); enqueue(&q, 12); enqueue(&q, 13); enqueue(&q, 14); enqueue(&q, 15); Display(q); // принтиране на всички елементи в опашката int number = 0; dequeue(&q, &number); printf("%d \n", number); // принтиране на извлеченото число Display(q); // принтиране на всички елементи в опашката }