【栈】两栈共享空间
2015-11-15 10:38
501 查看
SeqDoubleStack.h
#ifndef SEQDOUBLESTACK_H
#define SEQDOUBLESTACK_H
#include<iostream>
int default_size = 20;
// SeqDoubleStack
template<typename T>
class SeqDoubleStack{
public:
typedef T& reference;
SeqDoubleStack(int size = default_size);//constructor
~SeqDoubleStack();//destructor
void push(int stack_num,const T value);//push
void pop(int stack_num);//pop
reference get_top(int stack_num);//top
bool empty();//whether stack is empty or not
bool full(); // whether stack is full or not
void print();//print all elements in stack
private:
T* base;
T* top1;
T* top2;
int stack_size;
};
//constructor
template<typename T>
SeqDoubleStack<T>::SeqDoubleStack(int size){
if (size > 0){
base = new T[size];
stack_size = size;
}
else{
base = new T[default_size];
stack_size = default_size;
}
top1 = base;
top2 = base + stack_size - 1;
}
//destructor
template<typename T>
SeqDoubleStack<T>::~SeqDoubleStack(){
delete[] base;
base = NULL;
top1 = NULL;
top2 = NULL;
}
//push
template<typename T>
void SeqDoubleStack<T>::push(int stack_num, const T value){
if (full()){
std::cout << "stack is full" << std::endl;
exit(1);
}
if (stack_num == 1){
*top1 = value;
top1++;
}
else if (stack_num==2){
*top2 = value;
top2--;
}
}
//pop
template<typename T>
void SeqDoubleStack<T>::pop(int stack_num){
if (stack_num == 1){
if (top1 == base){
std::cout << "there is no element in stack1" << std::endl;
exit(1);
}
top1--;
}
else if (stack_num == 2){
if (full()) {
std::cout << "there is no element in stack2" << std::endl;
exit(1);
}
top2++;
}
}
//top
template<typename T>
typename SeqDoubleStack<T>::reference SeqDoubleStack<T>::get_top(int stack_num){
if (stack_num == 1){
if (top1 == base){
std::cout << "there is no element in stack1" << std::endl;
exit(1);
}
return *(top1 - 1);
}
else if (stack_num == 2){
if (top2 == base + stack_size - 1) {
std::cout << "there is no element in stack2" << std::endl;
exit(1);
}
return *(top2 + 1);
}
}
//whether stack is empty or not
template<typename T>
bool SeqDoubleStack<T>::empty(){
if (top1 == base&&top2 == base + stack_size - 1)
return true;
return false;
}
// whether stack is full or not
template<typename T>
bool SeqDoubleStack<T>::full(){
return top2 + 1 == top1 ? true : false;
}
//print all elements in stack
template<typename T>
void SeqDoubleStack<T>::print(){
std::cout << "all elements in stack1:";
T* p = top1;
while (p != base){
std::cout << *(--p) << " ";
}
std::cout << std::endl;
std::cout << "all elements in stack2:";
p = top2;
while (p != base+stack_size-1){
std::cout << *(++p) << " ";
}
std::cout << std::endl;
}
#endifmain.cpp
#include"SeqDoubleStack.h"
using namespace std;
int main(){
SeqDoubleStack<int> int_stack;
cout << boolalpha << int_stack.empty() << endl;//true
for (int i = 0; i < 10; i++){
int_stack.push(1,i);
}
cout << int_stack.get_top(1) << endl;//9
cout << boolalpha << int_stack.empty() << endl;//false
int_stack.push(2,21);
cout << int_stack.get_top(2) << endl;//21
int_stack.pop(2);
int num = 10;
for (int i = 0; i < 10; i++){
int_stack.push(2, num++);
}
int_stack.print();//stack1:9 8 7 6 5 4 3 2 1 0
//stack2:19 18 17 16 15 14 13 12 11 10
cout << boolalpha << int_stack.full() << endl;//true
return 0;
}
#ifndef SEQDOUBLESTACK_H
#define SEQDOUBLESTACK_H
#include<iostream>
int default_size = 20;
// SeqDoubleStack
template<typename T>
class SeqDoubleStack{
public:
typedef T& reference;
SeqDoubleStack(int size = default_size);//constructor
~SeqDoubleStack();//destructor
void push(int stack_num,const T value);//push
void pop(int stack_num);//pop
reference get_top(int stack_num);//top
bool empty();//whether stack is empty or not
bool full(); // whether stack is full or not
void print();//print all elements in stack
private:
T* base;
T* top1;
T* top2;
int stack_size;
};
//constructor
template<typename T>
SeqDoubleStack<T>::SeqDoubleStack(int size){
if (size > 0){
base = new T[size];
stack_size = size;
}
else{
base = new T[default_size];
stack_size = default_size;
}
top1 = base;
top2 = base + stack_size - 1;
}
//destructor
template<typename T>
SeqDoubleStack<T>::~SeqDoubleStack(){
delete[] base;
base = NULL;
top1 = NULL;
top2 = NULL;
}
//push
template<typename T>
void SeqDoubleStack<T>::push(int stack_num, const T value){
if (full()){
std::cout << "stack is full" << std::endl;
exit(1);
}
if (stack_num == 1){
*top1 = value;
top1++;
}
else if (stack_num==2){
*top2 = value;
top2--;
}
}
//pop
template<typename T>
void SeqDoubleStack<T>::pop(int stack_num){
if (stack_num == 1){
if (top1 == base){
std::cout << "there is no element in stack1" << std::endl;
exit(1);
}
top1--;
}
else if (stack_num == 2){
if (full()) {
std::cout << "there is no element in stack2" << std::endl;
exit(1);
}
top2++;
}
}
//top
template<typename T>
typename SeqDoubleStack<T>::reference SeqDoubleStack<T>::get_top(int stack_num){
if (stack_num == 1){
if (top1 == base){
std::cout << "there is no element in stack1" << std::endl;
exit(1);
}
return *(top1 - 1);
}
else if (stack_num == 2){
if (top2 == base + stack_size - 1) {
std::cout << "there is no element in stack2" << std::endl;
exit(1);
}
return *(top2 + 1);
}
}
//whether stack is empty or not
template<typename T>
bool SeqDoubleStack<T>::empty(){
if (top1 == base&&top2 == base + stack_size - 1)
return true;
return false;
}
// whether stack is full or not
template<typename T>
bool SeqDoubleStack<T>::full(){
return top2 + 1 == top1 ? true : false;
}
//print all elements in stack
template<typename T>
void SeqDoubleStack<T>::print(){
std::cout << "all elements in stack1:";
T* p = top1;
while (p != base){
std::cout << *(--p) << " ";
}
std::cout << std::endl;
std::cout << "all elements in stack2:";
p = top2;
while (p != base+stack_size-1){
std::cout << *(++p) << " ";
}
std::cout << std::endl;
}
#endifmain.cpp
#include"SeqDoubleStack.h"
using namespace std;
int main(){
SeqDoubleStack<int> int_stack;
cout << boolalpha << int_stack.empty() << endl;//true
for (int i = 0; i < 10; i++){
int_stack.push(1,i);
}
cout << int_stack.get_top(1) << endl;//9
cout << boolalpha << int_stack.empty() << endl;//false
int_stack.push(2,21);
cout << int_stack.get_top(2) << endl;//21
int_stack.pop(2);
int num = 10;
for (int i = 0; i < 10; i++){
int_stack.push(2, num++);
}
int_stack.print();//stack1:9 8 7 6 5 4 3 2 1 0
//stack2:19 18 17 16 15 14 13 12 11 10
cout << boolalpha << int_stack.full() << endl;//true
return 0;
}
相关文章推荐
- 《并查集》hdu acm 5.1.6 c++ set
- Java 接口技术 Interface
- 数组
- css不定宽度居中
- <shape> android 各属性介绍
- Bitmap与OOM
- 在GridView中针对鼠标单击的某一独立单元格进行编辑
- Clone Graph
- 常用NLP知名研究机构
- asp.net 中GridView控件实现全选及反选的功能
- Java 实现邮件的自动发送
- asp.net 中GridView控件实现全选及反选的功能
- "学霸"系统Alpha版本发布说明
- Fire Again
- 信息安全系统设计基础实验二—20135214万子惠20135227黄晓妍
- [LeetCode]80. Merge Two Sorted Lists合并两个排序链表
- [LeetCode]80. Merge Two Sorted Lists合并两个排序链表
- LCT细节注意
- 详解CSS position属性
- 部分银行卡bin