Menu
Coddy logo textTech

Ćwiczenie nr 3

Lekcja 11 z 12 w kursie Kolejka — struktury danych, seria nr 2 w Coddy.

Ulepszmy kolejkę i przekształćmy ją w kolejkę cykliczną!

challenge icon

Wyzwanie

Łatwy

Ulepsz swoją klasę Queue, aby obsługiwała kolejkę cykliczną, i zmień jej nazwę na CircularQueue.

Konstruktor przyjmuje liczbę całkowitą — maksymalny rozmiar kolejki cyklicznej. Gdy kolejka jest pełna i dodawany jest nowy element, najstarszy element (z początku kolejki) zostaje usunięty, aby zrobić miejsce. Zaktualizuj pozostałe metody, aby to obsługiwały.

Spróbuj swoich sił

#include <iostream>
#include <sstream>
#include <string>
#include "circular_queue.h"

int main() {
    std::string line;
    std::getline(std::cin, line);
    CircularQueue q(std::stoi(line));
    while (std::getline(std::cin, line)) {
        std::istringstream iss(line);
        std::string cmd;
        if (!(iss >> cmd)) continue;
        if (cmd == "enqueue") {
            int x; iss >> x; q.enqueue(x);
        }
        if (cmd == "dequeue") {
            q.dequeue();
        }
        if (cmd == "front") {
            std::cout << q.front() << std::endl;
        }
        if (cmd == "rear") {
            std::cout << q.rear() << std::endl;
        }
        if (cmd == "size") {
            std::cout << q.size() << std::endl;
        }
    }
    return 0;
}

Wszystkie lekcje w sekcji Kolejka — struktury danych, seria nr 2

Poćwicz samodzielnie: Kompilator C online