// generic queue implemented with doubly linked list #include<iostream> #include<string> #include <list> using std::cout; using std::endl; using std::string; template<class T> class Queue { public: Queue() { } void clear() { lst.clear(); } bool isEmpty() const { return lst.empty(); } T& front() { return lst.front(); } void enqueue(const T& el) { lst.push_back(el); } T dequeue() { T el = lst.front(); lst.pop_front(); return el; } private: std::list<T> lst; }; int main(){ Queue<string> tque; tque.enqueue("you "); tque.enqueue("are "); tque.enqueue("a "); tque.enqueue("shining "); tque.enqueue("star."); while(!tque.isEmpty()){ cout << tque.dequeue(); } cout << endl; // A segmentation fault will happen. //cout << tque.dequeue(); }
时间: 2024-10-17 04:41:17