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 168 169 170 171 172
| #include"All.h"
void initStack(SharedStack &S) { for(int i=0;i<MAXSIZE;i++) { S.data[i]=0; } S.top1=0; S.top2=MAXSIZE-1; S.size=0; }
bool isFull1(SharedStack S) { return S.top1>S.top2; }
bool isFull2(SharedStack S) { return S.top1>S.top2; }
bool isEmpty1(SharedStack S) { if(S.top1==0) { return true; }else{ return false; } }
bool isEmpty2(SharedStack S) { if(S.top2==MAXSIZE) { return true; }else{ return false; } }
bool push1(SharedStack &S,ElemType e) { if(!isFull1(S)) { S.data[S.top1++]=e; S.size++; return true; }else{ return false; } }
bool push2(SharedStack &S,ElemType e) { if(!isFull2(S)) { S.data[S.top2--]=e; S.size++; }else{ return false; } return true; }
int pop1(SharedStack &S) { if(!isEmpty1(S)) { S.size--; ElemType x=S.data[--S.top1]; S.data[S.top1]=0; return x; } else { return -999; } }
int pop2(SharedStack &S) { if(!isEmpty2(S)) { S.size--; ElemType x=S.data[++S.top2]; S.data[S.top2]=0; return x; } else { return -999; } }
ElemType peek1(SharedStack S) { if(!isEmpty1(S)) { return S.data[S.top1-1]; }else { return -999; } }
ElemType peek2(SharedStack S) { if(!isEmpty2(S)) { return S.data[S.top2+1]; }else { return -999; } }
int getLength1(SharedStack S) { return S.top1; }
int getLength2(SharedStack S) { return MAXSIZE-1-S.top2; }
void printStack1(SharedStack S) { for(int i=S.top1-1;i>=0;i--) { printf("%d ",S.data[i]); } printf("\n"); }
void printStack2(SharedStack S) { for(int i= S.top2+1;i<MAXSIZE;i++) { printf("%d ",S.data[i]); } printf("\n"); }
void printStack(SharedStack S) { for(int i=0;i<S.size;i++) { printf("%d ",S.data[i]); } printf("\n"); }
|