请高手推荐本c++数据结构的经典书!
- 提问者网友:野性
- 2021-01-03 18:49
。
- 二级知识专家网友:丢不掉的轻狂
- 2021-01-03 19:55
再可以看看《Data Structures and Algorithms with Object-Oriented Design Patterns in C++》
如果再有余力就看《STL源码剖析》(侯捷)——里面是STL的实现分析,绝对值得看。
- 1楼网友:瘾与深巷
- 2021-01-03 22:23
- 2楼网友:偏爱自由
- 2021-01-03 21:23
n=2的例子:
const unsigned int stacksize = 100;
#ifndef bothstack_h #define bothstack_h template<typename t> class cbothstack { t data[ stacksize ]; int top1; int top2;
public: cbothstack(); ~cbothstack(); void push(unsigned short int i , t x); t pop(unsigned short int i); t gettop(unsigned short int i) const; bool empty() const; }; #endif
template<typename t> cbothstack<t>::cbothstack() { top1 =- 1; top2 = stacksize; }
template<typename t> cbothstack<t>::~cbothstack() {}
template<typename t> void cbothstack<t>::push(unsigned short int i , t x) { if ( top1 + 1 == top2 ) throw "上溢"; if ( i == 1) data[ ++top1 ] = x; if ( i == 2 ) data[ --top2 ] = x; }
template<typename t> t cbothstack<t>::pop(unsigned short int i) { if ( top1 == -1 || top2 == stacksize) throw "下溢"; else if ( i == 1 ) return data[ top1-- ]; else if ( i == 2 ) return data[ top2++ ]; }
template<typename t> t cbothstack<t>::gettop(unsigned short int i) const { if ( top1 == -1 || top2 == stacksize ) throw "下溢"; if ( i == 1 ) return data[ top1 ]; if ( i == 2 ) return data[ top2 ]; }
template<typename t> bool cbothstack<t>::empty() const { return ( top1 == -1 && top2 == stacksize); }