stack.h   slist.h(见队列)

#ifndef _STACK_H_INCLUDED
#define _STACK_H_INCLUDED

#include "slist.h"

template<typename T>
class Stack {
public:
	Stack() : hb_slist() {}
	~Stack() {}

	bool empty() const { return hb_slist.empty(); }
	void clear() { hb_slist.clear(); }
	int size() const { return hb_slist.size(); }

	T top() const { return hb_slist.value(hb_slist.size()); }
	void push(const T& elem) { hb_slist.push_back(elem); }
	T pop() { return hb_slist.pop_back(); }

private:
	Slist<T> hb_slist;
};

#endif

测试代码:

#include "stdafx.h"

#include <iostream>
#include "stack.h"

int main()
{
	Stack<int> stack;
	stack.push(2);
	stack.push(6);
	stack.push(9);
	int num=stack.size();
	for (int i=0; i!=num; ++i)
		std::cout<<stack.pop()<<"-";
	std::cout<<std::endl;
	
	return 0;
}


你可能感兴趣的:(C++,算法,栈)