、定义了一个类模板堆栈,怎么建立结构体堆栈啊 ?下面程序帮忙看看啊 ??

#include<iostream>
using namespace std;
const int size=10;
template<typename Type>
class Stack
{
public:
void init()
{tos=0;}
void push(Type ch);
Type pop();
private:
Type stck[size];
int tos;
};
template<typename Type>
void Stack<Type>::push(Type ob)
{
if(tos==size)
{cout<<"Stack is full";
return;
}
stck[tos]=ob;
tos++;
}
template<typename Type>
Type Stack<Type>::pop()
{
if(tos==0)
{cout<<"Stack is empty";
return 0;
}
tos--;
return stck[tos];
}
int main()
{
//定义整形堆栈
Stack<int>is;
int i;
is.init();
is.push(2);
is.push(88);
is.push(9);
is.push(4);
for(i=0;i<4;i++)
cout<<"pop is:"<<is.pop()<<endl;
struct CHAP5
{
char * pName;
bool Gender;
int Age;
struct CHAP5 * next;
};
struct Node_int
{
int i;
struct Node_int * next;
};
Stack<CHAP5>jg
jg.init();
jg.push("pan",true,20,*p);
/*CHAP5.push(88);
CHAP5.push(9);
CHAP5.push(4);*/
cout<<"pop is:"<<jg.pop()<<endl;
return 0;
}

第1个回答  2012-06-02
template<typename T>
class Stack{
public:
Satck(int a=0)
{
base=new T[size=a];
top=base;
}//构造函数
~Stack()
{
delete[] base;
}
private:
T* base;
T* top;
int size;
};
其他的函数自己添加吧
相似回答
大家正在搜