本文主要是介绍Implement a stack that pops out the mostfrequently added item. Stack supports 3 functions – push,,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、问题
Implement a stack that pops out the mostfrequently added item. Stack supports 3 functions – push, pop and top.Givecomplexity of each functions in your implementation
2、算法
实现有两种,一种使用标准库中的vector,另一种使用上节已经实现的List http://blog.csdn.net/u011476173/article/details/39025083
template <typename T>
class Stack{
private:
//std::vector<T> stack ;
List<T> stack ;
public:
void push( const T& data )
{
//stack.push_back( data ) ;
stack.insert( data ) ;
}
void pop()
{
//stack.pop_back() ;
stack.remove( 0 ) ;
}
T& top()
{
//return stack.back() ;
return stack.getHead()->content() ;
}
}
这篇关于Implement a stack that pops out the mostfrequently added item. Stack supports 3 functions – push,的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!