又一次造轮子-C++栈(泛型编程)

栈,一种简单的数据结构,再造一次轮子

<<Stack.h>>

#if ! defined STACK_H_
#define STACK_H_

template<class Type>

class Stack
{
private:
    struct LinkStack
    {
        Type ele;
        LinkStack *next,*previous;
    };
    LinkStack *linkstack,*newnode;
    int size;

public:
    Stack();
    ~Stack();

    Type Top();
    void Push(Type e);
    void Pop(); //为空时特殊处理

    int Size();
    bool isEmpty();

};

#endif

<<Stack.cpp>>

#include<cstdlib>

#include "Stack.h"

template<class Type>
int Stack<Type>::Size()
{
    return size;
}
template<class Type>
bool Stack<Type>::isEmpty()
{
    return !size;
}

template<class Type>
Stack<Type>::Stack()
{
    size = 0;
}

template<class Type>
Type Stack<Type>::Top()
{
    return linkstack->ele;
}

template<class Type>
void Stack<Type>::Push(Type e)
{
    newnode = new LinkStack;
    newnode->ele = e;
    if(size)
    {
        newnode->previous = linkstack;
    }
    linkstack = newnode;

    ++size;
}

template<class Type>
void Stack<Type>::Pop()
{
    if(size && size != 1)
    {
        newnode = linkstack;
        linkstack = linkstack->previous;
        delete[] newnode;
    }
    else if(size==1)
    {
        delete[] linkstack;
    }

    --size;
}

template<class Type>
Stack<Type>::~Stack()
{
    while(size)
    {
        Pop();
    }
}
<<TestStack.cpp>

#include "Stack.cpp"
#include<iostream>
using namespace std;

struct A
{
    int x,y;
};

class B
{
public:
    int x,y;
};

int C;

int main()
{
    Stack<A> sta1;
    Stack<B> sta2;
    Stack<int> sta3;

    A a;
    for(int i=0;i<10;i++)
    {
        a.x = a.y = i;
        sta1.Push(a);
    }
    cout<<"A:"<<endl;
    cout<<sta1.isEmpty()<<endl;
    cout<<sta1.Size()<<endl;

    cout<<"Test A"<<endl;
    for(int i=0;i<10;i++)
    {
        A temp = sta1.Top();
        cout<<"x:"<<temp.x<<" y:"<<temp.y<<endl;
        sta1.Pop();
    }
    cout<<"A:"<<endl;
    cout<<sta1.isEmpty()<<endl;
    cout<<sta1.Size()<<endl;

    B b;
    for(int i=0;i<10;i++)
    {
        b.x = b.y = i;
        sta2.Push(b);
    }
    cout<<"B:"<<endl;
    cout<<sta2.isEmpty()<<endl;
    cout<<sta2.Size()<<endl;

    cout<<"Test B"<<endl;
    for(int i=0;i<10;i++)
    {
        B temp = sta2.Top();
        cout<<"x:"<<temp.x<<" y:"<<temp.y<<endl;
        sta2.Pop();
    }
    cout<<"B:"<<endl;
    cout<<sta2.isEmpty()<<endl;
    cout<<sta2.Size()<<endl;

    cout<<"Test C"<<endl;
    for(int i=0;i<10;i++)
    {
        sta3.Push(i);
    }
    cout<<"C:"<<endl;
    cout<<sta3.isEmpty()<<endl;
    cout<<sta3.Size()<<endl;

    for(int i=0;i<10;i++)
    {
        cout<<sta3.Top()<<endl;
        sta3.Pop();
    }
    cout<<"C:"<<endl;
    cout<<sta3.isEmpty()<<endl;
    cout<<sta3.Size()<<endl;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值