mirror of
https://github.com/CodePanda66/CSPostgraduate-408.git
synced 2023-05-21 21:49:33 +08:00
✨ Add LinkQueue
This commit is contained in:
@@ -17,7 +17,7 @@ typedef struct {
|
||||
//初始化
|
||||
void InitStack(ShStack &S) {
|
||||
S.top0 = -1;//这种初始化的方式,栈顶指针始终指向栈顶元素
|
||||
S.top1 =MaxSize;//这里的MaxSize就是所谓的第二个栈的栈底
|
||||
S.top1 = MaxSize;//这里的MaxSize就是所谓的第二个栈的栈底
|
||||
//可以根据顺序栈的第二种初试化方式,思考一下这种共享顺序栈的第二种初始化方式
|
||||
//S.top0=0
|
||||
//S.top1=MaxSize-1
|
||||
@@ -26,19 +26,19 @@ void InitStack(ShStack &S) {
|
||||
|
||||
//入栈0
|
||||
bool Push0(ShStack &S, int t) {
|
||||
if (S.top0 +1== S.top1)return false;//注意共享栈满的条件
|
||||
if (S.top0 + 1 == S.top1)return false;//注意共享栈满的条件
|
||||
S.data[++S.top0] = t;//仔细品味一下这个++S.top
|
||||
return true;
|
||||
}
|
||||
|
||||
//入栈1
|
||||
bool Push1(ShStack &S, int t) {
|
||||
if (S.top0 +1== S.top1)return false;//注意共享栈满的条件
|
||||
if (S.top0 + 1 == S.top1)return false;//注意共享栈满的条件
|
||||
S.data[--S.top1] = t;//仔细品味一下这个--S.top,想想为什么?
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//出栈,并打印出栈顶元素
|
||||
bool Pop0(ShStack &S, int &x) {
|
||||
//判断
|
||||
@@ -49,6 +49,7 @@ bool Pop0(ShStack &S, int &x) {
|
||||
// S.top -=1;//再改指针
|
||||
return true;
|
||||
}
|
||||
|
||||
//出栈1
|
||||
bool Pop1(ShStack &S, int &x) {
|
||||
//判断
|
||||
@@ -66,6 +67,7 @@ bool GetTop0(ShStack S, int &x) {
|
||||
x = S.data[S.top0];
|
||||
return true;
|
||||
}
|
||||
|
||||
//栈1
|
||||
bool GetTop1(ShStack S, int &x) {
|
||||
if (S.top1 == MaxSize)return false;
|
||||
@@ -75,18 +77,19 @@ bool GetTop1(ShStack S, int &x) {
|
||||
|
||||
|
||||
//打印整个栈,栈0
|
||||
void PrintStack0(ShStack S){
|
||||
void PrintStack0(ShStack S) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top0>-1){//注意判空的条件
|
||||
printf("S[%d]=%d\n",S.top0,S.data[S.top0--]);
|
||||
while (S.top0 > -1) {//注意判空的条件
|
||||
printf("S[%d]=%d\n", S.top0, S.data[S.top0--]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
|
||||
//打印整个栈
|
||||
void PrintStack1(ShStack S){
|
||||
void PrintStack1(ShStack S) {
|
||||
printf("从栈顶元素开始,栈如下:\n");
|
||||
while (S.top1<MaxSize){//注意判空的条件
|
||||
printf("S[%d]=%d\n",S.top1,S.data[S.top1++]);
|
||||
while (S.top1 < MaxSize) {//注意判空的条件
|
||||
printf("S[%d]=%d\n", S.top1, S.data[S.top1++]);
|
||||
}
|
||||
printf("栈打印完毕\n");
|
||||
}
|
||||
@@ -96,55 +99,55 @@ void testShStack() {
|
||||
ShStack S;
|
||||
InitStack(S);
|
||||
printf("测试第一个栈\n");
|
||||
if (Push0(S,1)){
|
||||
if (Push0(S, 1)) {
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push0(S,2)){
|
||||
if (Push0(S, 2)) {
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack0(S);
|
||||
int x;
|
||||
if (Pop0(S, x)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x);
|
||||
} else{
|
||||
if (Pop0(S, x)) {
|
||||
printf("出栈成功,弹出的元素为:%d\n", x);
|
||||
} else {
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack0(S);
|
||||
int x1;
|
||||
if (GetTop0(S,x1)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x1);
|
||||
}else{
|
||||
if (GetTop0(S, x1)) {
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n", x1);
|
||||
} else {
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
|
||||
|
||||
printf("测试第二个栈\n");
|
||||
if (Push1(S,10)){
|
||||
if (Push1(S, 10)) {
|
||||
printf("入栈成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈失败了\n");
|
||||
}
|
||||
if (Push1(S,9)){
|
||||
if (Push1(S, 9)) {
|
||||
printf("入栈又成功啦!\n");
|
||||
} else{
|
||||
} else {
|
||||
printf("入栈又失败了\n");
|
||||
}
|
||||
PrintStack1(S);
|
||||
int x3;
|
||||
if (Pop1(S, x3)){
|
||||
printf("出栈成功,弹出的元素为:%d\n",x3);
|
||||
} else{
|
||||
if (Pop1(S, x3)) {
|
||||
printf("出栈成功,弹出的元素为:%d\n", x3);
|
||||
} else {
|
||||
printf("出栈失败了,再检出一下吧!\n");
|
||||
}
|
||||
PrintStack1(S);
|
||||
int x4;
|
||||
if (GetTop1(S,x4)){
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x4);
|
||||
}else{
|
||||
if (GetTop1(S, x4)) {
|
||||
printf("读取栈顶元素成功了,栈顶元素为:%d\n", x4);
|
||||
} else {
|
||||
printf("读取栈顶元素失败,再检查一下吧!\n");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user