共享栈的基本操作code_legend
来源:互联网 发布:淘宝导航css代码 透明 编辑:程序博客网 时间:2024/06/07 05:20
/*
一个数组有两个栈来共享所以为共享栈。
所以有两个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;
}
一个数组有两个栈来共享所以为共享栈。
所以有两个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 0
- 共享栈的基本操作code_legend
- 顺序栈的基本操作code_legend
- 链式栈的基本操作code_legend
- 链式队列基本操作之code_legend
- 栈的应用之表达式求值code_legend
- 栈的应用之括号匹配code_legend
- 数据结构-共享栈的基本操作实现
- 栈的应用之进制转换code_legend
- Posix共享内存区的基本操作
- 第2章第1节练习题3 共享栈的基本操作
- 基本的缓存类操作封装(Shared 共享类)
- 栈的基本操作
- 栈的基本操作
- 栈 的基本操作。
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- 栈的基本操作
- Fedora 在线升级
- Java基础篇----toString()方法
- 不懂技术的人不要对懂技术的人说这很容易实现
- 问题笔记
- 这是测试二这是测试二这是测试二这是测试二这是测试二这是测试二这是测试二这是测试二这是测试二
- 共享栈的基本操作code_legend
- 机器学习中的优化算法
- 与蓝牙数据通信
- Mac OS X设置OpenVPN客户端Tunnelblick
- 破解myeclipse9.1
- Sublime Plugin - SQL格式化插件
- google 替代
- 数据结构之双向链表
- linux中的jiffies变量