基于数组实现双端栈
2018-06-17 21:34:13来源:未知 阅读 ()
栈底分别在数组的两端,规定数组大小为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;
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- C++冒泡排序 (基于函数模板实现) 2020-05-31
- opencv-12-高斯滤波-双边滤波(附C++代码实现) 2020-05-10
- 二叉排序树 2020-05-02
- 纯虚函数与基类指针数组的运用 代码参考 2020-04-30
- STL之deque 2020-04-29
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash