类模板实现堆栈-VC编程

类模板实现堆栈-VC编程
2008-05-29 08:16:40

采用了三种实现方法:

1、自己定义数组(Stack 堆栈数组大小不能动态增加)

2、使用STL的vector(Vector_Stack 堆栈数组可动态变化)

3、使用STL的list(List_Stack 堆栈数组可动态变化)


以下是stack.h的头文件,所有接口函数都写为内联,因此没有实现文件。
正在装载数据……


#ifndef ___STACK_H___
#define ___STACK_H___

#include <windows.h>
#include <vector>

using namespace std;

template<class T,int size = 100>
class Stack
{
public:
Stack():top(0){}
~Stack(){};

int Push(T& data)
{
if (top >= size)
{
return top;
}
stack[top ] = data;
return top;
}
int Pop(T* pData)
{
if (top <= 0)
{
return -1;
}
*pData = stack[--top];
return 0;
}
int Size()
{
return top;
}
int Clear()
{
top = 0;
return 0;
}
int Peek(T* pData)
{
*pData = stack[top];
return 0;
}
BOOL IsEmpty()
{
return (top > 0 ? FALSE : TRUE);
}
protected:
private:
T stack[size];
int top;
};


template<class T>
class Vector_Stack
{
public:
Vector_Stack(){}
~Vector_Stack(){};

int Push(T& data)
{
vstack.push_back(data);
return vstack.size();
}
int Pop(T* pData)
{
if (vstack.empty())
{
*pData = 0;
return -1;
}
else
{
*pData = vstack.back();
vstack.pop_back();
return 0;
}
}
int Size()
{
return vstack.size();
}
int Clear()
{
vstack.clear();
return 0;
}
int Peek(T* pData)
{
*pData = vstack.back();
return 0;
}
BOOL IsEmpty()
{
return (vstack.empty() ? TRUE : FALSE);
}
protected:
private:
vector<T> vstack;
};

template<class T>
class List_Stack
{
public:
List_Stack(){}
~List_Stack(){};

int Push(T& data)
{
lstack.push_back(data);
return lstack.size();
}
int Pop(T* pData)
{
if (lstack.empty())
{
*pData = 0;
return -1;
}
else
{
*pData = lstack.back();
lstack.pop_back();
return 0;
}
}
int Size()
{
return lstack.size();
}
int Clear()
{
lstack.clear();
return 0;
}
int Peek(T* pData)
{
*pData = lstack.back();
return 0;
}
BOOL IsEmpty()
{
return (lstack.empty() ? TRUE : FALSE);
}
protected:
private:
vector<T> lstack;
};

#endif

检验代码:

// 模板(栈).cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stack.h"

#include <vector>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
/************************************************************************/
/* Stack */
/************************************************************************/
cout<<"********** Stack **********"<<endl;

int i,nSize;
int data;
const int size = 3;
Stack<int,size> intStack;

cout<<"set stack size=3"<<endl;

for (i=0;i<3;i )
{
data = (i 1)*100;
intStack.Push(data);
cout<<"push "<<data<<endl;
}
cout<<"size="<<intStack.Size()<<endl;

for (i=0;i<size 1;i )
{
if (!intStack.Pop(&data))
{
cout<<"pop data:"<<data<<endl;
}
}
cout<<"size="<<intStack.Size()<<endl<<endl;

/************************************************************************/
/* Vector_Stack */
/************************************************************************/
cout<<"********** Vector_Stack **********"<<endl;

Vector_Stack<int> vIntStack;

for (i=0;i<5;i )
{
data = (i 1)*10;
vIntStack.Push(data);
cout<<"push "<<data<<endl;
}
nSize = vIntStack.Size();
cout<<"size="<<nSize<<endl;

for (i=0;i<nSize 1;i )
{
vIntStack.Pop(&data);
cout<<"pop data:"<<data<<endl;
}
nSize = vIntStack.Size();
cout<<"size="<<nSize<<endl;

cout<<"IsEmpty="<<vIntStack.IsEmpty()<<endl<<endl;

/************************************************************************/
/* List_Stack */
/************************************************************************/
cout<<"********** List_Stack **********"<<endl;

List_Stack<int> lIntStack;

for (i=0;i<5;i )
{
data = (i 1)*10;
lIntStack.Push(data);
cout<<"push "<<data<<endl;
}
nSize = lIntStack.Size();
cout<<"size="<<nSize<<endl;

for (i=0;i<nSize 1;i )
{
lIntStack.Pop(&data);
cout<<"pop data:"<<data<<endl;
}
nSize = lIntStack.Size();
cout<<"size="<<nSize<<endl;

cout<<"IsEmpty="<<lIntStack.IsEmpty()<<endl;

return 0;
}


运行结果为:

********** Stack **********
set stack size=3
push 100
push 200
push 300
size=3
pop data:300
pop data:200
pop data:100
size=0

********** Vector_Stack **********
push 10
push 20
push 30
push 40
push 50
size=5
pop data:50
pop data:40
pop data:30
pop data:20
pop data:10
pop data:0
size=0
IsEmpty=1

********** List_Stack **********
push 10
push 20
push 30
push 40
push 50
size=5
pop data:50
pop data:40
pop data:30
pop data:20
pop data:10
pop data:0
size=0
IsEmpty=1
Press any key to continue


文章转载自『幽月's Blog』

你可能感兴趣的:(类模板实现堆栈-VC编程)