pop
الدرس 6 من 13 في دورة Stack - سلسلة هياكل البيانات #1 على Coddy.
التحدي
سهلأضف إلى Stack طريقة تسمى pop لا تستقبل أي وسائط، ستقوم هذه الدالة بإزالة العدد الصحيح الموجود في أعلى المكدس و إرجاعه.
جرّب بنفسك
#include <iostream>
#include <sstream>
#include <string>
#include "stack.h"
int main() {
Stack stack;
std::string line;
while (std::getline(std::cin, line)) {
std::istringstream iss(line);
std::string cmd;
if (!(iss >> cmd)) continue;
if (cmd == "push") {
int x; iss >> x; stack.push(x);
}
else if (cmd == "top") {
std::cout << stack.top() << std::endl;
}
else if (cmd == "pop") {
std::cout << stack.pop() << std::endl;
}
}
return 0;
}