我的Stack

A.第一版
这个小程序是第一版。(会有第二版吗?)
1。 程序基本说明:用数组结合模板实现的极其简单的数据结构---stack。
2。 程序思路:栈底在数组的0位置,数组的上限就是栈的上限,用一个index来标定当前的栈顶,很土吧?	
3。 主要函数介绍:
     A.  T pop();
	   void push(T element);
	stack的基本操作,简单判断empty和溢出的情况。没什么好说的。
4。 不足之处:
	A. 随便写的,计划再用链表实现一遍。
		
#include <iostream>
using namespace std;
const int MaxLen = 100;
template <class T>
class Stack
{
private:
	T lst[MaxLen];
	int domino;
public:
	T pop();
	void push(T element);
	Stack();
	bool empty();
	bool full();
};
int main()
{
	Stack<int> intStack;
	for (int i=0; i<10; i++)
	{
		intStack.push(i*2);
	}
	for (i =0; i< 12; i++)
	{
		cout<<i<<" is "<<intStack.pop()<<"\n";
	}
	return 0;
}
template<class T>
bool Stack<T>::full()
{
	return (domino==MaxLen);
}
template<class T>
Stack<T>::Stack()
{
	domino =0;
}
template<class T>
bool Stack<T>::empty()
{
	return (domino==0);
}
template<class T>
T Stack<T>::pop()
{
	if (!empty())
	{
		domino--;
		return lst[domino];	
	}
	else
	{
		cout<<"\nEmpty!\n";
		return NULL;
	}
}
template<class T>
void Stack<T>::push(T element)
{
	if (!full())
	{
		lst[domino]= element;
		domino++;
	}
	else
	{
		cout<<"\noverflow!\n";
	}
}

                                                         back.gif (341 bytes)       up.gif (335 bytes)         next.gif (337 bytes)

Hosted by www.Geocities.ws

1