栈的c++实现(类模板,包含顺序实现和链表实现)

头文件:

#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED

class OutOfBound{};

template <class elemType>
class Stack
{
public:
    virtual bool isEmpty() const=0;
    virtual void push(const elemType &x)=0;
    virtual elemType pop()=0;
    virtual elemType top() const=0;
    virtual ~Stack(){}
};

template <class elemType>
class seqStack:public Stack<elemType>
{
private:
    elemType *elem;
    int Top;
    int maxSize;
    void doubleSpace();
public:
    seqStack(int initSize=10);
    ~seqStack();
    bool isEmpty() const;
    elemType top() const;
    elemType pop();
    void push(const elemType &x);
};

template <class elemType>
class linkStack:public Stack<elemType>{
   private:
      struct node{
          elemType data;
          node *next;
          node(const elemType &x,node *N=NULL){data=x; next=N;}
          node():next(NULL){}
          ~node(){}
      };
      node *Top;
   public:
    linkStack();
    ~linkStack();
    bool isEmpty() const;
    elemType top() const;
    elemType pop();
    void push(const elemType &x);
};


#include "seqStack.cpp"
#include "linkStack.cpp"
#endif // STACK_H_INCLUDED

顺序实现:

#ifndef cpp1_h
#define cpp1_h

#include <iostream>
#include "stack.h"
using namespace std;

template <class elemType>
seqStack<elemType>::seqStack(int initSize){
    //declaration could have default value
    elem=new elemType[initSize];
    maxSize=initSize;
    Top=-1;
}

template <class elemType>
seqStack<elemType>::~seqStack(){
    delete [] elem;
}
template <class elemType>
bool seqStack<elemType>::isEmpty() const{
    return Top==-1;
}

template <class elemType>
elemType seqStack<elemType>::top() const{
    if (Top==-1) throw OutOfBound();
    return elem[Top];
}

template <class elemType>
elemType seqStack<elemType>::pop(){
    if (Top==-1) throw OutOfBound();
    return elem[Top--];
}

template <class elemType>
void seqStack<elemType>::push(const elemType &x){
    if(Top==maxSize-1) doubleSpace();
    elem[++Top]=x;
}

template <class elemType>
void seqStack<elemType>::doubleSpace(){
    elemType *tmp=elem;
    elem=new elemType[2*maxSize];
    for(int i=0;i<maxSize;i++) elem[i]=tmp[i];
    maxSize*=2;
    delete [] tmp;
}
#endif // cpp1_h

链表实现:

#ifndef cpp2_h
#define cpp2_h

#include <iostream>
#include "stack.h"
using namespace std;
template <class elemType>
linkStack<elemType>::linkStack(){Top=NULL;}

template <class elemType>
linkStack<elemType>::~linkStack(){
    node *tmp;
    while(Top!=NULL){
        tmp=Top;
        Top=Top->next;
        delete tmp;
    }
}

template <class elemType>
bool linkStack<elemType>::isEmpty() const{
    return Top==NULL;
}

template <class elemType>
elemType linkStack<elemType>::pop(){
    node *tmp=Top;
    elemType x=Top->data;
    Top=Top->next;
    delete tmp;
    return x;
}

template <class elemType>
elemType linkStack<elemType>::top() const{
    if (Top==NULL) throw OutOfBound();
    return Top->data;
}

template <class elemType>
void linkStack<elemType>::push(const elemType &x){
    node *tmp=new node(x,Top);
    Top=tmp;
}
#endif // cpp2_h

主函数(测试用):

#include <iostream>
#include "stack.h"
using namespace std;

int main()
{
    seqStack<int> st1(5);
    st1.push(1);
    st1.push(2);
    st1.push(3);
    cout<<st1.top();
    st1.pop();
    st1.pop();
    st1.pop();
    if (st1.isEmpty()) cout<<"yes";

    cout<<endl;
    linkStack<int> st2;
    st2.push(1);
    st2.push(2);
    cout<<st2.pop();
    cout<<st2.pop();
    cout<<st2.isEmpty();

    return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值