注册 登录
编程论坛 VC++/MFC

关于类模板(与栈相关~)

小小小小小黄 发布于 2013-05-08 21:23, 513 次点击
#include<iostream.h>
template<class Type>
class Stack
{
    private:
        int top,length;
        Type* s;
    public:
        Stack(int n)
        {
            s=new Type[n];
            length=n;
            top=0;
        }
        ~Stack()
        {
            delete[]s;
        }
        void Push(Type);
        Type Pop();
};
template<class Type>
void Stack<Type>::Push(Type)
{
    if(top==length)
    {
        cout<<"Stack is full\n";
        return;
    }
    s[top]=d;//这一步也有问题,不是应该写输入的数据么,不知道改写什么,应该不是d~
    top++;
}
template<class Type>
Type Stack<Type>::Pop()
{
    if(top==0)
    {
        cout<<"Stack is empty\n";
        return 0;
    }
    top--;
    return s[top];
}
void mian()
{
    int a,n;
    double b;
    char c;
    cout<<"输入栈顶:"<<endl;
    cin>>n;
    Stack<int>s1(n);
    //这一段将数据输入,压入,弹出不知道该怎么写?求教~



    Stack<double>s2(5);
    Stack<char>s3(5);
}
2 回复
#2
yuccn2013-05-09 12:14
void mian() -》void main()
#3
yuccn2013-05-09 12:18

void Stack<Type>::Push(Type d)
 {
     if(top==length)
     {
         cout<<"Stack is full\n";
         return;
     }
     s[top]=d;//这一步也有问题,不是应该写输入的数据么,不知道改写什么,应该不是d~
     top++;
 }

//这一段将数据输入,压入,弹出不知道该怎么写?求教~
    s1.Push(12);
    a = s1.Pop();
1