數據結構——順序雙棧模板類實現

數據結構3.1.2
順序棧中單棧和雙棧(數組的兩端分別爲棧底)其實極爲相似,道理上是一樣的,實現的時候只需要多定義一套top標記和bottom標記即可,在判斷棧FULL的時候,即是判斷兩個top是否相遇,這裏我用兩個元素的數組來定義兩個棧頂標記,下面貼出代碼(實現方式是多種多樣的,如有不當,萬望指正)^ _ ^
模板類代碼:

#include <iostream>
#include <cassert>
using namespace std;
template<class T>
class DBstack {
public:
    DBstack(int sz = 100);                      //構造函數/每個棧的起始容量是50
    ~DBstack();                                    //析構函數
    bool Push(T & x, int d);                    //將x推入d代表的棧中,d = 0是1號棧否則爲2號棧
    bool Pop(T & x, int d);                     //將d號棧頂元素彈出,其值返回在引用x當中,d = 0是1號棧否則爲2號棧
    bool getTop(T & x, int d);                  //得到d號棧的棧頂元素,其值返回帶x的引用當中(不彈出)
    bool isEmpty(int d)const;                   //判斷d號棧是否爲NULL
    bool isFull()const;                         //判斷棧是否爲滿
    int getSize(int d)const;                    //返回d號棧當前元素的個數
    void makeEmpty(int d);                      //清空d號棧
    void output(int d)const;                    //輸出d號棧的內容
private:
    T *stack;                                   //雙棧數組
    int top[2];                                 //分別是兩個棧的棧頂指針
    int bottom[2];                              //兩個棧的棧底指針
    void overflowProcess();                     //棧的溢出處理
};
//函數定義
template<class T>
DBstack<T>::DBstack(int sz) {
    //構造函數
    stack = new T[sz];          //開闢雙棧數組
    assert(stack != NULL);      //中斷處理
    top[0] = bottom[0] = -1;
    top[1] = bottom[1] = sz;
}

template<class T>
DBstack<T>::~DBstack() {
    //析構函數,釋放程序中的資源
    delete[] stack;
}

template<class T>
bool DBstack<T>::Push(T & x, int d) {
    //將x推入d代表的棧中,d = 0是1號棧否則爲2號棧
    if (top[0] + 1 == top[1]) {                     //再插入即兩個top相遇
        return false;                               //棧滿,存儲失敗
    }
    if (0 == d) {
        //推入1號棧
        stack[++top[0]] = x;
    }
    else {
        stack[--top[1]] = x;
    }
    return true;
}

template<class T>
bool DBstack<T>::Pop(T & x, int d) {
    //將d號棧頂元素彈出,其值返回在引用x當中,d = 0是1號棧否則爲2號棧
    if (isEmpty(d)) {                           //檢查棧是否爲NULL
        return false;                           
    }
    //執行彈出操作
    if (0 == d) {
        x = stack[top[0]--];
    }
    else {
        x = stack[top[1]++];
    }
    return true;
}

template<class T>
bool DBstack<T>::getTop(T & x, int d) {
    //得到d號棧的棧頂元素,其值返回帶x的引用當中(不彈出)
    if (0 == d) {
        x = stack[top[0]];
        return true;
    }
    else {
        x = stack[top[1]];
        return true;
    }
    return false;
}

template<class T>
bool DBstack<T>::isEmpty(int d)const {
    //判斷d號棧是否爲NULL
    if (0 == d) {
        if (top[0] == bottom[0]) {
            return true;
        }
    }
    else {
        if (top[1] == bottom[1]) {
            return true;
        }
    }
    return false;
}

template<class T>
bool DBstack<T>::isFull()const {
    //判斷棧是否爲FULL
    if (top[0] == top[1]) {
        return true;
    }
    return false;
}

template<class T>
int DBstack<T>::getSize(int d) const {
    //返回d號棧當前元素的個數
    if (0 == d) {
        return top[0] + 1;
    }
    else {
        return 100 - top[1];
    }
}

template<class T>
void DBstack<T>::makeEmpty(int d) {
    //set NULL
    if (0 == d) {
        top[0] = bottom[0];
    }
    else {
        top[1] = bottom[1];
    }
}

template<class T>
void DBstack<T>::output(int d)const {
    //輸出任意棧的全部元素
    if (0 == d) {
        //輸出1號棧的全部元素
        for (int i = bottom[0] + 1; i <= top[0]; i ++) {
            cout << stack[i] << ' ';
        }
    }
    else {
        //輸出2號棧的全部元素
        for (int j = bottom[1] - 1; j >= top[1]; j --) {
            cout << stack[j] << ' ';
        }
    }
    cout << endl;
}

main測試代碼:

int main()
{
    //填充元素測試
    DBstack<int> doubl_stack;                   //創建一個雙棧
    for (int i = 1; i <= 5; i ++) {             //給0號棧填充上5個值
        doubl_stack.Push(i , 0);
    }
    for (int j = 1; j <= 7; j++) {              //同上
        doubl_stack.Push(j, 1);                 //第二個參數非0即可
    }
    doubl_stack.output(0);                      //輸出兩個棧中的元素
    doubl_stack.output(1);

    //彈出元素測試
    int pop_1, pop_2, pop_3, pop_4;
    doubl_stack.Pop(pop_1, 0);
    doubl_stack.Pop(pop_2, 1);
    doubl_stack.Pop(pop_3, 1);
    doubl_stack.Pop(pop_4, 1);
    doubl_stack.output(0);                      //輸出兩個棧中的元素
    doubl_stack.output(1);

    //得到元素個數測試
    int add_1 = 100;
    doubl_stack.Push(add_1, 0);
    cout << doubl_stack.getSize(0) << endl;
    cout << doubl_stack.getSize(1) << endl;

    //set Empty測試
    int pop_5;
    doubl_stack.makeEmpty(0);
    doubl_stack.output(0);
    doubl_stack.output(1);
    doubl_stack.Pop(pop_5 ,0);                  //檢測pop函數對Empty的處理是否正常

    system("pause");
    return 0;
}

運行效果:
這裏寫圖片描述

發佈了50 篇原創文章 · 獲贊 228 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章