顺序栈的基本操作code_legend

来源:互联网 发布:微商是什么软件 编辑:程序博客网 时间:2024/06/07 06:35
#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
原创粉丝点击