Skip to content

Instantly share code, notes, and snippets.

@necusjz
Created April 25, 2019 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save necusjz/8541b926e44be4fbe2598d844e70c1ad to your computer and use it in GitHub Desktop.
Save necusjz/8541b926e44be4fbe2598d844e70c1ad to your computer and use it in GitHub Desktop.
面试题 9:用两个栈实现队列
template<typename T> void CQueue<T>::appendTail(const T& element) {
stack1.push(element);
}
template<typename T> T CQueue<T>::deleteHead() {
if(stack2.size() <= 0) {
while(stack1.size() > 0) {
T& data = stack1.top();
stack1.pop();
stack2.push(data);
}
}
if(stack2.size() == 0) {
throw new exception("queue is empty");
}
T head = stack2.top();
stack2.pop();
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment