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
| void SqStackInit(SqDoubleStack *s) { s->top1 = -1; s->top2 = MAX_SIZE; }
Status Push(SqDoubleStack *s, SElemType e,int stackNumber) { if (s->top1+1 == s->top2) { return ERROR; } if (stackNumber==1) { s->top1++; s->data[s->top1] = e; } else { s->top2--; s->data[s->top2] = e; } return OK; }
Status Pop(SqDoubleStack *s, SElemType &e, int stackNumber) { if (stackNumber==1) { if (s->top1 == -1) { return ERROR; } else { e = s->data[s->top1]; s->top1--; } } else if (stackNumber==2) { if (s->top2 == MAX_SIZE) { return ERROR; } else { e = s->data[s->top2]; s->top2++; } } return OK; }
int main() { SqDoubleStack S1; SElemType e; SqStackInit(&S1); for (int i=0;i<3;i++) { Push(&S1, i,1); printf("%d,", i); } printf("\n"); for (int i = 3; i < 5; i++) { Push(&S1, i, 2); printf("%d,", i); } printf("\n"); for (int i = 0; i < MAX_SIZE; i++) { if (Pop(&S1, e, 1)==ERROR) { break; } printf("%d,", e); } printf("\n"); for (int i = 0; i < MAX_SIZE; i++) { if (Pop(&S1, e, 2) == ERROR) { break; } printf("%d,", e); } }
|