Created
April 25, 2019 16:40
-
-
Save necusjz/8541b926e44be4fbe2598d844e70c1ad to your computer and use it in GitHub Desktop.
面试题 9:用两个栈实现队列
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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