【栈】两栈共享空间

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;
}

#endif
main.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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值