数据结构--栈--共享顺序栈
生活随笔
收集整理的這篇文章主要介紹了
数据结构--栈--共享顺序栈
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
共享順序棧:內(nèi)部也是一個數(shù)組
將兩個棧放在數(shù)組的兩端,一個從數(shù)組首端開始壓棧,一個從數(shù)組尾部開始壓棧,等到兩邊棧頂在中間相遇時,棧滿。
共享順序棧在某些情況下可以節(jié)省空間。
頭文件 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; //棧數(shù)組int capacity; //棧容量 public:sharingStack(int size = 10); //初始化,總?cè)萘磕J(rèn)10~sharingStack(){delete [] arr;arr = NULL;}void push(const T& data, int stackIndex); //將數(shù)據(jù)壓入index號棧內(nèi)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; //打印一側(cè)棧void printAll() const; //打印所有 };#endif //STACK_SHARINGSTACK_H共享順序棧 類實(shí)現(xiàn) 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檢查結(jié)果
上面給定棧容量8,#0棧長度5,讓#1棧長度分別為0,3,4,當(dāng)為4時棧滿溢出。
總結(jié)
以上是生活随笔為你收集整理的数据结构--栈--共享顺序栈的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 图片背景前景分离_【绝了】
- 下一篇: POJ 1321 棋盘问题(回溯)