栈模板类
原创
©著作权归作者所有:来自51CTO博客作者wx64015c4b4bc07的原创作品,请联系作者获取转载授权,否则将追究法律责任
// 1233.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <list>
using namespace std;
//栈模板类
template<typename T>
class Stack
{
T c[20];
int size;
public:
Stack();
void push(T item);
T pop();
int len()
{
return size;
}
};
template<typename T>
Stack<T>::Stack()
{
size=0;
}
template<typename T>
void Stack<T>::push(T item)
{
if(size<20)
{
c[size]=item;
size++;
}
else
{
printf("stack full!\n");
}
}
template<typename T>
T Stack<T>::pop()
{
if(size>0)
{
size--;
return c[size];
}
else
{
printf("no data!\n");
return NULL;
}
}
int main(int argc, char* argv[])
{
Stack<int> mystack;
printf("length:%d\n",mystack.len());
//压栈
mystack.push(10);
mystack.push(8);
mystack.push(1);
printf("length:%d\n",mystack.len());
//出栈
int value=mystack.pop();
printf("%d\n",value);
printf("length:%d\n",mystack.len());
return 0;
}