日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

双栈共享存储空间(C++代码实现)

發布時間:2023/12/10 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 双栈共享存储空间(C++代码实现) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

理論部分

理論來源:b站up主:跟懶貓老師快樂數據結構

C++代碼

#include<iostream> using namespace std; const int STACKSIZE = 6; //兩棧共享存儲空間編程 //使用類模板編程 template<class DataType> class BothStack { private:DataType *data;//屬性,順序棧,數組int top1, top2;//屬性,top1指向棧1的棧頂,top2指向棧2的棧頂int capacity;//屬性,棧的容量 public:BothStack();//無參構造函數BothStack(int size);//有參構造函數~BothStack();//析構函數DataType getTop(int num);//彈出棧頂元素,num表明對棧1操作,還是對棧2操作DataType Pop(int num);//出棧void Push(int num, DataType elem);//入棧bool isFull();//判斷棧滿bool isEmpty(int num);//判斷棧空class IndexError{};//定義內部異常類class Empty{};class Full{}; };template<class DataType> BothStack<DataType>::BothStack() {data = new DataType[STACKSIZE];top1 = -1;top2 = STACKSIZE;capacity = STACKSIZE; }template<class DataType> BothStack<DataType>::BothStack(int size) {data = new DataType[size];top1 = -1;top2 = size;capacity = size; }template<class DataType> BothStack<DataType>::~BothStack() {delete[] data; }template<class DataType> DataType BothStack<DataType>::getTop(int num) {if (num == 1){//對棧1進行操作if (isEmpty(num)){throw Empty();}else{return data[top1];}}else if (num == 2){//對棧2進行操作if (isEmpty(num)){throw Empty();}else{return data[top2];}}else{throw IndexError();} }template<class DataType> DataType BothStack<DataType>::Pop(int num) {if (num == 1){//對棧1進行操作if (isEmpty(num)){throw Empty();}else{return data[top1--];}}else if (num == 2){//對棧2進行操作if (isEmpty(num)){throw Empty();}else{return data[top2++];}}else{throw IndexError();} }template<class DataType> void BothStack<DataType>::Push(int num, DataType elem) {if (isFull()){throw Full();}else {if (num == 1){data[++top1] = elem;}else if (num == 2){data[--top2] = elem;}else {throw IndexError();}} } template<class DataType> bool BothStack<DataType>::isFull() {if (top2 == top1 +1){return true;}return false; } template<class DataType> bool BothStack<DataType>::isEmpty(int num) {if (num == 1){if (top1 == -1){return true;}return false;}else if (num == 2){if (top2 == capacity){return true;}return false;}else{throw IndexError();} } int main() {BothStack<char> bs1;//棧1中依次壓入'a','b','c'//棧2中依次壓入'z','x','y'//棧3中壓入'z',拋出異常//try--catch捕獲異常對象try {bs1.Push(1, 'a');bs1.Push(1, 'b');bs1.Push(1, 'c');bs1.Push(2, 'z');bs1.Push(2, 'y');bs1.Push(2, 'x');bs1.Push(2, 'w');}catch (BothStack<char>::Full){cout << "Stack Full!!!" << endl;}catch (BothStack<char>::IndexError){cout << "Index Error!!!" << endl;}try {cout << bs1.Pop(1) << endl;cout << bs1.Pop(1) << endl;cout << bs1.Pop(1) << endl;cout << bs1.Pop(2) << endl;cout << bs1.Pop(2) << endl;cout<<bs1.Pop(2)<<endl;cout << bs1.Pop(2) << endl;}catch (BothStack<char>::Empty){cout << "Stack Empty!!!"<< endl;}catch (BothStack<char>::IndexError){cout << "Index Error!!!" << endl;} }

運行結果

棧的大小為6

總結

以上是生活随笔為你收集整理的双栈共享存储空间(C++代码实现)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。