共享栈的基本操作code_legend

/*
一个数组有两个栈来共享所以为共享栈。
所以有两个top,一个数组。
*/
#include <iostream>
using namespace std;
#define Maxsize 20
typedef int elemType;
class shareStack{
      public:
      elemType shareArray[Maxsize];
      int top1;
      int top2;


      public:
      /*init the shareStack*/
      void initShareStack(){
      top1=-1;
      top2=Maxsize;
      }


      /*constructor*/
      shareStack(){
      initShareStack();
      }


      /*stack1 is full and stack2 is full .
      the condition is same*/
      bool isFull(){
      if(top1+1==top2)
      return true;
      return false;
      }


      /*push the element into the stack ,flag=1,Ôòpush to stack1
      else if flag=2,push to stack2
      if the stack is full ,return false.
      else return true;
      */
      bool push(elemType element, int flag){


            if(1==flag){
                  if(isFull()) return false;
                  top1++;
                  shareArray[top1]=element;
                  return true;
            }


            else if(2==flag){
            if(isFull()) return false;
                  top2--;
                  shareArray[top2]=element;
                  return true;
            }
      }


      /* judge the stack whether is empty.*/
      bool isEmpty(int flag){
            if(1==flag){
                  /*stack 1 is empty or not*/
                  if(-1==top1)
                  return true;
                  else return false;
            }


            else if(2==flag){
                  /*stack 2 is empty or not*/
                  if(Maxsize==top2)
                  return true;
                  else return false;
            }
      }


      /* get the top element of stack flag*/
      bool getTop(elemType& element,int flag){
            if(1==flag){
                  if(isEmpty(1))
                  return false;
                  element=shareArray[top1];
                  return true;
            }
            else if(2==flag){
                  if(isEmpty(2))
                  return false;


                  element=shareArray[top2];
                  return true;
            }
      }


      /*pop the top element*/


      bool pop(int flag){
            if(1==flag){
                  if(isEmpty(1))
                  return false;


                  top1--;
                  return true;
            }
            else if(2==flag){
                  if(isEmpty(2))
                  return false ;
                  top2++;
                  return true;
            }
      }


      /* get the length of stack flag*/
      int getSize(int flag){
            if(1==flag){
            return top1+1;
            }
            else if(2==flag){
            return Maxsize-top2;
            }
      }
};


int main()
{
      shareStack stack;


      cout<<"push 3,5,4 into stack1 , push 8,9,7,6 into stack2"<<endl;
      stack.push(3,1);
      stack.push(5,1);
      stack.push(4,1);
      stack.push(8,2);
      stack.push(9,2);
      stack.push(7,2);
      stack.push(6,2);


      int size2,size1;
      size2=stack.getSize(2);
      size1=stack.getSize(1);
      cout<<"the size1 is : "<<size1<<endl<<"the size2 is : "<<size2<<endl;


      elemType element1,element2;
      stack.getTop(element1,1);
      stack.pop(2);
      stack.getTop(element2,2);
      cout<<"the top ele of stack1 is : "<<element1<<endl<<"pop ,and get top of stack2 "
      <<element2<<endl;


      size2=stack.getSize(2);
      size1=stack.getSize(1);
      cout<<"the size1 is : "<<size1<<endl<<"the size2 is : "<<size2<<endl;
    cout << "Hello world!" << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值