C++进阶与拔高(三)(C++数据结构的具体实现:栈与队列)

1.3 栈Stack的具体实现

#ifndef STACK_H
#define STACK_H
#include "Vector\vector.h"
#include"list.h"
//基于向量派生的栈模板类
template<typename T>
class Stack :public Vector<T>{
public:
    void push(T const&e){ insert(size(), e); }
    T pop(){ return remove(size() - 1); }
    T& top(){ return (*this)[size() - 1]; }
};
//基于列表派生的模板类
template<typename T>
class ListStack :public List<T>{
public://size() empty()以及其它开放接口均可以直接沿用
    void push(T const& e){ insertAsFirst(e); }
    T pop(){ return remove(header->succ); }
    T& top(){ return first()->data; }
};
#endif // !STACK_H

1.4 队列Queue的具体实现

#ifndef QUEUE_H
#define QUEUE_H
#include"../list.h"
template <typename T>
class Queue : public List<T>{
public:
    void enqueue(T const& e){ insertAsFirst(e); }
    T& real(){ return first()->data; }
    T dequeue(){ return remove(last()); }
    T& front(){ return last()->data; }
};
#endif // !QUEUE_H

猜你喜欢

转载自blog.csdn.net/Lao_tan/article/details/81105640