Menu
CoddyTech

队尾

Coddy 队列:数据结构系列 #2 课程的第 7 课,共 12 课。

challenge icon

挑战

简单

为 Queue 添加一个名为 rear 的方法,该方法不接收输入,并返回队列中的最后一个项(最后入队的项)。

亲手试试

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

int main() {
    Queue q;
    std::string 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;
        }
    }
    return 0;
}

队列:数据结构系列 #2中的所有课

自己练习: C 在线编译器