基于数组实现双端栈

栈底分别在数组的两端,规定数组大小为128,int类型。

可以实现的操作为验空、验满、分别向两个栈添加元素、分别删除栈顶元素、分别展示栈内所有元素。

代码如下:

#include<iostream>

using namespace std;

typedef int StackElement;

const int STACK_CAPACITY = 128;

class Stack

{

public:

Stack();

int size1()const;

int size2()const;

bool empty1()const;

bool empty2()const;

bool full()const;

void push1(const StackElement & item);

void push2(const StackElement & item);

void display1();

void display2();

void pop1();

void pop2();

int capacity()const;

StackElement top1()const;

StackElement top2()const;

private:

StackElement myArray[STACK_CAPACITY];

int myTop1;

int myTop2;

};

Stack::Stack():myTop1(-1),myTop2(-1)

{}

//-----------------------------------------------

bool Stack::empty1()const

{

return (myTop1==-1);

}

//-----------------------------------------------

bool Stack::empty2()const

{

return (myTop2==-1);

}

//-----------------------------------------------

int Stack::size1()const

{

return myTop1;

}

//-----------------------------------------------

int Stack::size2()const

{

return myTop2;

}

//-----------------------------------------------

int Stack::capacity()const

{

return STACK_CAPACITY;

}

//-----------------------------------------------

void Stack::push1(const StackElement & item)

{

if(myTop1+myTop2>=capacity()-2)

{

cerr<<"***Full Full***"<<endl;

exit(1);

}

if(myTop1<STACK_CAPACITY-1)

{

++myTop1;

myArray[myTop1]=item;

}

else

{

cerr<<"***Error Error***"<<endl;

exit(1);

}

}

//-----------------------------------------------

void Stack::push2(const StackElement & item)

{

if(myTop1+myTop2>=capacity()-2)

{

cerr<<"***Full Full***"<<endl;

exit(1);

}

if(myTop2<STACK_CAPACITY-1)

{

++myTop2;

myArray[STACK_CAPACITY-1-myTop2]=item;

}

else

{

cerr<<"***Error Error***"<<endl;

exit(1);

}

}

//-----------------------------------------------

void Stack::display1()

{

for(int i = myTop1;i>=0;i--)

cout<<myArray[i]<<" ";

cout<<endl;

}

//-----------------------------------------------

void Stack::display2()

{

for(int i = myTop2;i>=0;i--)

cout<<myArray[STACK_CAPACITY-1-i]<<" ";

cout<<endl;

}

//-----------------------------------------------

void Stack::pop1()

{

    if(!empty1())

    myTop1--;

else

    cerr<<"***no element no element***"<<endl;

}

//-----------------------------------------------

void Stack::pop2()

{

if(!empty2())

    myTop2--;

else

    cerr<<"***no element no element***"<<endl;

}

//-----------------------------------------------

StackElement Stack::top1()const

{

if(!empty1())

    return (myArray[myTop1]);

else

{

cerr<<"***Stack is empty -- returning gabage value***"<<endl;

StackElement garbage;

return garbage;

}

}

//-----------------------------------------------

StackElement Stack::top2()const

{

if(!empty2())

    return (myArray[STACK_CAPACITY-1-myTop2]);

else

{

cerr<<"***Stack is empty -- returning gabage value***"<<endl;

StackElement garbage;

return garbage;

}

}

//-----------------------------------------------

bool Stack::full()const

{

return(capacity()<=myTop1+myTop2+2);

}

//-----------------------------------------------

int main()

{

Stack s;

cout<<"Two Stack created.Stack 1 Empty?"<<boolalpha<<s.empty1()<<endl;

cout<<"Stack 2 Empty?"<<boolalpha<<s.empty2()<<endl;

cout<<"please input a number to fill in Stack 1  :";

int numItems1;

cin>>numItems1;

for(int i=1;i<=numItems1;i++)

    s.push1(i);

cout<<"please input a number to fill in Stack 2  :";

int numItems2;

cin>>numItems2;

for(int i=1;i<=numItems2;i++)

    s.push2(i);

    

cout<<"Stack 1 contents : "<<endl;

s.display1();

cout<<"Stack 2 contents : "<<endl;

s.display2();

 

cout<<"Stack 1 empty ? "<<boolalpha<<s.empty1()<<endl;

cout<<"Stack 2 empty ? "<<boolalpha<<s.empty2()<<endl;

 

cout<<"Stack 1 's Top value is : "<<s.top1()<<endl;

cout<<"Stack 2 's Top value is : "<<s.top2()<<endl;

cout<<endl;

cout<<"The all Stack is full ? "<<s.full()<<endl;

 

s.pop1();

cout<<"delete Stack 1 a value,then the top value is : "<<s.top1()<<endl;

s.pop2();

cout<<"delete Stack 2 a value,then the top value is : "<<s.top2()<<endl;

}

 

转载于:https://www.cnblogs.com/ssss98/p/7838917.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值