size
Lesson 8 of 12 in Coddy's Queue - Data Structures Series #2 course.
Challenge
EasyAdd to Queue a method called size that gets no input and returns the number of items in the queue.
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;
}
if (cmd == "size") {
std::cout << q.size() << std::endl;
}
}
return 0;
}