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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#include <stdio.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define MAXSIZE 20 typedef int ElemType; typedef int State; typedef struct{ ElemType data[MAXSIZE]; int lefttop; int righttop; }SharedStack;
State initStack(SharedStack *S){ S->lefttop = -1; S->righttop = MAXSIZE; return OK; }
int getLength(SharedStack S){ return MAXSIZE-(S.righttop-S.lefttop-1); }
State isEmpty(SharedStack S){ if(S.lefttop==-1 && S.righttop==MAXSIZE){ return TRUE; }else{ return FALSE; } }
State isFull(SharedStack S){ if(S.lefttop+1==S.righttop){ return TRUE; }else{ return FALSE; } }
State clearStack(SharedStack *S){ S->lefttop=-1; S->righttop=MAXSIZE; return OK; }
State push(SharedStack *S, int i, ElemType *e){ if(isFull(*S)){ printf("栈满,无法入栈"); return ERROR; } if(i!=0 && i!=1){ printf("输入有误!请输入插入左栈的代表数字0或右栈代表数字1!"); return ERROR; } if(i==0){ S->lefttop++; S->data[S->lefttop]=e; return OK; } if(i==1){ S->righttop--; S->data[S->righttop]=e; return OK; } return OK; }
State pop(SharedStack *S, int i, ElemType *e){ if(isEmpty(*S)){ printf("栈空,无法出栈"); return ERROR; } if(i!=0 && i!=1){ printf("输入有误!请输入插入左栈的代表数字0或右栈代表数字1!"); return ERROR; } if(i==0){ *e = S->data[S->lefttop]; S->lefttop--; return OK; } if(i==1){ *e = S->data[S->righttop]; S->righttop++; return OK; } return OK; }
State getTop(SharedStack S, int i, ElemType *e){ if(isEmpty(S)){ printf("栈空,无栈顶元素"); return ERROR; } if(i!=0 && i!=1){ printf("输入有误!请输入插入左栈的代表数字0或右栈代表数字1!"); return ERROR; } if(i==0){ *e = S.data[S.lefttop]; return OK; } if(i==1){ *e = S.data[S.righttop]; } return OK; }
State printStack(SharedStack S, int i){ if(isEmpty(S)){ printf("栈空"); return OK; } if(i!=0 && i!=1 && i!=3){ printf("输入有误!请输入插入左栈的代表数字0或右栈代表数字1代表全栈的数字3!"); return ERROR; } if(i==0){ int i=1; while(S.lefttop!=-1){ printf("左栈栈顶向下第%d个元素为:%d\n", i, S.data[S.lefttop]); S.lefttop--; } return OK; } if(i==1){ int i=1; while(S.righttop!=MAXSIZE){ printf("右栈栈顶向下第%d个元素为:%d\n", i, S.data[S.righttop]); S.righttop++; } return OK; } if(i==3){ int i=1; while(S.lefttop!=-1){ printf("左栈栈顶向下第%d个元素为:%d\n", i, S.data[S.lefttop]); S.lefttop--; } i=1; while(S.righttop!=MAXSIZE){ printf("右栈栈顶向下第%d个元素为:%d\n", i, S.data[S.righttop]); S.righttop++; } } return OK; }
int main(int argc, const char * argv[]) { SharedStack S; initStack(&S); printf("初始化一个共享栈,长度为:%d\n", getLength(S)); printf("将1-5顺序入左栈得:\n"); for(int i=1;i<=5;i++){ push(&S, 0, i); } printStack(S, 0); printf("将11-15顺序入右栈得:\n"); for(int i=11;i<=15;i++){ push(&S, 1, i); } printStack(S, 1); printf("全栈为:\n"); printStack(S, 3); printf("当前共享栈是否为空(0非空1空):%d\n", isEmpty(S)); printf("当前共享栈是否为满(0非满1满):%d\n", isFull(S)); printf("当前共享栈的长度为:%d\n", getLength(S)); int e; pop(&S, 0, &e); printf("左栈出栈元素:%d\n", e); pop(&S, 0, &e); printf("左栈出栈元素:%d\n", e); pop(&S, 1, &e); printf("右栈出栈元素:%d\n", e); getTop(S, 0, &e); printf("获取当前左栈的栈顶元素:%d\n", e); getTop(S, 1, &e); printf("获取当前右栈的栈顶元素:%d\n", e); printf("现在共享栈的元素为:\n"); printStack(S, 3); printf("现在共享栈的长度为:%d\n", getLength(S)); return 0; }
|