您的位置:首页 > 其它

【栈】两栈共享空间

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: