資料結構:兩棧共用儲存空間

來源:互聯網
上載者:User

數組有兩個端點,兩個棧有兩個棧底,讓一個棧的棧底為數組的始端,即下標為0處,另一個棧為棧的末端,即下標為數組長度

n-1處。這樣,如果兩個棧增加元素,就是兩端點向中間延伸。當top1 + 1 == top2 的時候為棧滿。

範例程式碼:(改編自《大話資料結構》)

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
using namespace std;

#define  MAXSIZE 20

typedef int ElemType;
typedef struct
{
    ElemType data[MAXSIZE];
    int top1; //棧1棧頂指標
    int top2; //棧2棧頂指標
} SqStack;

/* 構造一個空棧*/
bool InitStack(SqStack *Sq)
{
    cout << "Init Stack ..." << endl;
    Sq->top1 = -1; //表示空棧
    Sq->top2 = MAXSIZE;

    return true;
}
/* 置為空白棧 */
bool ClearStack(SqStack *Sq)
{
    cout << "Clear Stack ..." << endl;
    Sq->top1 = -1;
    Sq->top2 = MAXSIZE;
    return true;
}

bool StackEmpty(SqStack Sq)
{
    if (Sq.top1 == -1 && Sq.top2 == MAXSIZE)
        return true;
    else
        return false;
}

int StackLength(SqStack Sq)
{
    cout << "Stack Length : ";
    return Sq.top1 + 1 + MAXSIZE - Sq.top2;
}
/* 返回棧頂元素 */
bool GetTop(SqStack Sq, ElemType *ptr, int StackNum)
{
    if (StackNum == 1)
    {
        if (Sq.top1 != -1)
        {
            *ptr = Sq.data[Sq.top1];
            cout << "Get Top1 Item " << *ptr << endl;
            return true;
        }
        return false;
    }

    else if (StackNum == 2)
    {
        if (Sq.top2 != MAXSIZE)
        {
            *ptr = Sq.data[Sq.top2];
            cout << "Get Top2 Item " << *ptr << endl;
            return true;
        }
        return false;
    }
    else
    {
        cout << "Stack Num must be 1 or 2!" << endl;
        return false;
    }
}

/* 壓棧 */
bool Push(SqStack *Sq, ElemType Elem, int StackNum)
{
    if (StackNum == 1)
    {
        cout << "Push Item to Stack1 : " << Elem << endl;
        if (Sq->top1 + 1 == Sq->top2)   //棧滿
            return false;
        Sq->data[++Sq->top1] = Elem;
        return true;
    }
    else if (StackNum == 2)
    {
        cout << "Push Item to Stack2 : " << Elem << endl;
        if (Sq->top1 + 1 == Sq->top2)
            return false;
        Sq->data[--Sq->top2] = Elem;
        return true;
    }
    else
    {
        cout << "Stack Num must be 1 or 2!" << endl;
        return false;
    }
}
/* 出棧 */
bool Pop(SqStack *Sq, ElemType *ptr, int StackNum)
{
    if (StackNum == 1)
    {
        if (Sq->top1 == -1)
            return false;

        *ptr = Sq->data[Sq->top1--];
        cout << "Pop Item from Stack1 :  " << *ptr << endl;
        return true;
    }
    else if (StackNum == 2)
    {
        if (Sq->top2 == MAXSIZE)
            return false;
        *ptr = Sq->data[Sq->top2++];
        cout << "Pop Item from Stack2 :  " << *ptr << endl;
        return true;
    }
    else
    {
        cout << "Stack Num must be 1 or 2!" << endl;
        return false;
    }

}

bool StackTraverse(SqStack Sq)
{
    cout << "Traverse Stack ..." << endl;
    if (StackEmpty(Sq))
        return false;
    cout << "Stack1 : ";
    for (int i = 0; i <= Sq.top1; i++)
        cout << Sq.data[i] << ' ';
    cout << endl;
    cout << "Stack2 : ";
    for (int i = MAXSIZE - 1; i >= Sq.top2; i--)
        cout << Sq.data[i] << ' ';
    cout << endl;

    return true;
}

int main(void)
{
    SqStack Sq;
    InitStack(&Sq);
    for (int i = 0; i < 5; i++)
        Push(&Sq, i, 1);
    for (int i = 5; i < 10; i++)
        Push(&Sq, i, 2);
    StackTraverse(Sq);
    int result;
    Pop(&Sq, &result, 1);
    Pop(&Sq, &result, 2);
    StackTraverse(Sq);
    GetTop(Sq, &result, 1);
    GetTop(Sq, &result, 2);
    if (!StackEmpty(Sq))
        cout << StackLength(Sq) << endl;

    ClearStack(&Sq);

    return 0;
}

輸出為:


事實上 ,使用這樣的資料結構,通常都是當兩個棧的空間需求有想法關係時,也就是當一個棧增長時另一個棧在縮短的情況。

還需要注意的一點是必須是同種資料類型的棧,否則不但不能更好地解決問題,反而會使問題更加複雜。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.