数组有两个端点,两个栈有两个栈底,让一个栈的栈底为数组的始端,即下标为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; } |
输出为:
事实上 ,使用这样的数据结构,通常都是当两个栈的空间需求有想法关系时,也就是当一个栈增长时另一个栈在缩短的情况。
还需要注意的一点是必须是同种数据类型的栈,否则不但不能更好地解决问题,反而会使问题更加复杂。