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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++实现双栈结构(一个顺序表中使用两个栈)

發(fā)布時(shí)間:2023/12/4 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++实现双栈结构(一个顺序表中使用两个栈) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

因?yàn)槠匠V衟ush的數(shù)據(jù)不會(huì)太多,為了節(jié)約空間,所以可以在一個(gè)順序表中使用兩個(gè)棧

結(jié)構(gòu)圖:

在這里我會(huì)留一個(gè)空間用來判斷棧是否滿!

#include <iostream> using namespace std; typedef int ElemType;class DoubleStack { private:ElemType *top_1;ElemType *base_1;ElemType *top_2;ElemType *base_2;public:DoubleStack(int n = 3){base_1 = new ElemType[n];top_1 = base_1;top_2 = top_1 + n - 1;base_2 = top_2;}bool push_1(ElemType e){if (top_1 == top_2){cout << "Stack_1 has fullen" << endl;return false;}*top_1 = e;top_1++;return true;}bool push_2(ElemType e){if (top_2 == top_1){cout << "Stack_2 has fullen" << endl;return false;}*top_2 = e;top_2--;return true;}bool pop_1(ElemType &e){if (top_1 == base_1){cout << "Stack_1 is empty" << endl;return false;}top_1--;e = *top_1;return true;}bool pop_2(ElemType &e){if (top_2 == base_2){cout << "Stack_2 is empty" << endl;return false;}top_2++;e = *top_2;return true;}bool isEmpty_1(){if (top_1 == base_1){return true;}return false;}bool isEmpty_2(){if (top_2 == base_2){return true;}return false;}bool getTop_1(ElemType &e){if (isEmpty_1()){cout << "Stack_1 is empty" << endl;return false;}e = *(top_1 - 1);return true;}bool getTop_2(ElemType &e){if (isEmpty_2()){cout << "Stack_2 is empty" << endl;return false;}e = *(top_2 + 1);return true;}void printStack_1(){if (isEmpty_1()){cout << "Stack_1 is empty" << endl;return;}ElemType *p = base_1;cout << "底在前頂在后:" << endl;while (p != top_1){cout << *p << " ";p++;}cout << endl;}void printStack_2(){if (isEmpty_2()){cout << "Stack_2 is empty" << endl;return;}ElemType *p = base_2;cout << "底在前頂在后:" << endl;while (p != top_2){cout << *p << " ";p--;}cout << endl;}};int main() {DoubleStack s(10);for (int i = 0; i < 7; i++){s.push_1(i);}s.push_2(3);s.push_2(4);s.push_1(5);s.printStack_1();s.printStack_2();if (s.isEmpty_1()){cout << "yes" << endl;}else cout << "no" << endl;if (s.isEmpty_2()){cout << "yes" << endl;}else cout << "no" << endl;return 0; }

總結(jié)

以上是生活随笔為你收集整理的C++实现双栈结构(一个顺序表中使用两个栈)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。