共享顺序栈:内部也是一个数组
将两个栈放在数组的两端,一个从数组首端开始压栈,一个从数组尾部开始压栈,等到两边栈顶在中间相遇时,栈满。
共享顺序栈在某些情况下可以节省空间。
头文件 sharingStack.h
//共享顺序栈
// Created by mingm on 2019/3/28.
//
#ifndef STACK_SHARINGSTACK_H
#define STACK_SHARINGSTACK_H#include <iostream>
template <class T> class sharingStack
{
private:int top[2], bot[2]; //双栈的栈顶指针和栈底指针T* arr; //栈数组int capacity; //栈容量
public:sharingStack(int size = 10); //初始化,总容量默认10~sharingStack(){delete [] arr;arr = NULL;}void push(const T& data, int stackIndex); //将数据压入index号栈内int pop(int stackIndex); //将index号栈顶元素弹出T* getTop(int stackIndex); //返回index号栈顶元素bool empty(int stackIndex) const //判断是否为空{return top[stackIndex] == bot[stackIndex];}bool full() const //判断栈是否满{return top[0]+1 == top[1];}void clear(int stackIndex); //清空index号栈void printOneSide(int stackIndex) const; //打印一侧栈void printAll() const; //打印所有
};#endif //STACK_SHARINGSTACK_H
共享顺序栈 类实现 sharingStack.cpp
//共享顺序栈
// Created by mingm on 2019/3/28.
//
#include "sharingStack.h"
//#include <assert.h>
#include <iostream>
template <class T>
sharingStack<T>::sharingStack(int size):capacity(size)
{top[0] = -1;bot[0] = -1;top[1] = size;bot[1] = size;arr = new T [size];
}
template <class T>
void sharingStack<T>::push(const T &data, int stackIndex)
{
// assert(!full()); //如果栈满了(条件为false),程序终止if(full())throw("stack is full !");if(stackIndex == 0)arr[++top[0]] = data;elsearr[--top[1]] = data;
}
template <class T>
int sharingStack<T>::pop(int stackIndex)
{if(empty(stackIndex))return 0;if(stackIndex == 0)top[0]--;elsetop[1]++;return 1;
}
template <class T>
T* sharingStack<T>::getTop(int stackIndex)
{if(empty(stackIndex))return NULL;return &arr[top[stackIndex]];
}
template <class T>
void sharingStack<T>::clear(int stackIndex)
{if(stackIndex == 0)top[0] = bot[0] = -1;elsetop[1] = bot[1] = capacity;
}
template <class T>
void sharingStack<T>::printOneSide(int stackIndex) const
{if(empty(stackIndex)){std::cout << "----Stack " << stackIndex << " is empty---- " << std::endl;return;}else{if(stackIndex == 0){std::cout << "----Stack " << stackIndex << " bottom---- " << top[stackIndex]+1 << " elem(s)" << std::endl;for(int i = bot[0]+1; i<= top[0]; ++i){std::cout << arr[i] << std::endl;}std::cout << "----Stack " << stackIndex << " top---- " << top[stackIndex]+1 << " elem(s)" << std::endl;}else{std::cout << "----Stack " << stackIndex << " top---- " << bot[stackIndex]-top[stackIndex] << " elem(s)" << std::endl;for(int i = top[1]; i< bot[1]; ++i){std::cout << arr[i] << std::endl;}std::cout << "----Stack " << stackIndex << " bottom---- " << bot[stackIndex]-top[stackIndex] << " elem(s)" << std::endl;}}
}
template <class T>
void sharingStack<T>::printAll() const
{std::cout << "****capacity of doubleStack is " << capacity << " *****" << std::endl;printOneSide(0);printOneSide(1);std::cout << "*******************************************" << std::endl;
}
测试主程序 sharingStack_testMain.cpp
//
// Created by mingm on 2019/3/28.
//
#include "sharingStack.cpp"
#include <iostream>
using namespace std;
int main()
{int L[3] = {0,3,4};int len1 = 5, len2;for(int k = 0; k < 3; ++k){len2 = L[k];sharingStack<int> doubleIntStack(8);for(int i = 0; i < len1; ++i){try{doubleIntStack.push(i,0);}catch(const char* ch){cout << ch << endl;break;}}for(int i = 0; i < len2; ++i){try{doubleIntStack.push(i,1);}catch(const char* ch){cout << ch << endl;break;}}doubleIntStack.printAll();}return 0;
}
valgrind检查结果
上面给定栈容量8,#0栈长度5,让#1栈长度分别为0,3,4,当为4时栈满溢出。