顺序栈的基本操作code_legend

#include <iostream>


using namespace std;
typedef int elemType;
//#definde Maxsize 10
class seqStack{
public :
      int maxSize;
      int top;
      elemType * array;
public :


      /*init the stack*/


      void initStack(int maxsize){
      this->maxSize=maxsize;
      this->array=new elemType[maxSize];
      this->top=-1;
      }


      /*constructor*/
      seqStack(int maxsize){


      this->initStack(maxsize);


      }


      /*destrucotr*/
      ~seqStack(){
      delete []array;
      }




      /*is full?*/
      bool isStackFull(){
      if(this->top==this->maxSize-1)
      return true;
      else
      return false;
      }
      
      /*is empty?*/
      bool isStackEmpty(){


      if(this->top==-1)
      return true;
      else
       return false;


      }




      /*push the element into the stack,
      if the stack is full ,return fasle;
      else return true;
      */
      bool push(elemType element){
      if(this->isStackFull())
      return false;
      this->top++;
      this->array[this->top]=element;
      return true;
      }


      /*
      pop the top element from the stack;
      if the stack is empty ,return false;
      else return true.
      */


      bool pop(elemType& topElement){
      if(this->isStackEmpty())
      return false;


      topElement=this->array[this->top];
      this->top--;
      return true;
      }


      /*get the current size of the stack */


      int getSize(){


      return this->top+1;
      }




      /*get the top element
      if the stack is empty ,return false ;
      else true;
      */


      bool getTop(elemType& topElement){
      if(this->isStackEmpty())
      return false;


      topElement=this->array[this->top];
      return true;
      }




};


int main()
{
      seqStack seqstack(10);
      seqstack.push(2);
      seqstack.push(3);
      seqstack.push(4);


      elemType element;
      int size;
      cout<<"push 2 , 3, 4"<<endl;
      size=seqstack.getSize();
      cout<<"size is : "<<size<<endl;


      seqstack.pop(element);


      cout<<"pop the top , top is : "<<element<<endl;


      size=seqstack.getSize();
      cout<<"size is : "<<size<<endl;


      seqstack.getTop(element);
      cout<<"get the top is :"<<element<<endl;
    cout << "Hello world!" << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值