Menu
Coddy logo textTech

rear

Lesson 7 of 12 in Coddy's Queue - Data Structures Series #2 course.

challenge icon

Challenge

Easy

Add to Queue a method called rear that gets no input and returns the last item from the queue (the item that was last enqueued).

Try it yourself

#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;
}

All lessons in Queue - Data Structures Series #2