Add LinkQueue

This commit is contained in:
Kim Yang
2020-08-02 23:16:06 +08:00
parent cac21ddd08
commit b9ff84ad0a
7 changed files with 326 additions and 197 deletions

View File

@@ -71,10 +71,11 @@ bool GetTop(SqStack S, int &x) {
x = S.data[S.top];
return true;
}
//读取栈顶元素初始化1
bool GetTop1(SqStack S, int &x) {
if (S.top == 0)return false;
x = S.data[S.top-1];//注意按初始化1的方式这里指针减1才是栈顶元素的位置
x = S.data[S.top - 1];//注意按初始化1的方式这里指针减1才是栈顶元素的位置
// 同时注意不能使用--S.top,因为这里是读取,不可修改原栈,所以不可和出栈一样
//但即时这里你错误使用了--S.top也不会有问题因为此处的S是值传递非引用传递所以你修改的也只是复制之后的S不会影响原栈S即时这样也不建议使用--S.top如果老师较真的话可以扣分哒
return true;
@@ -90,23 +91,24 @@ int GetTopOther(SqStack S) {
int GetTopOther1(SqStack S) {
if (S.top == 0)return -1;
return S.data[S.top-1];
return S.data[S.top - 1];
}
//打印整个栈
void PrintStack(SqStack S){
void PrintStack(SqStack S) {
printf("从栈顶元素开始,栈如下:\n");
while (S.top>=0){//注意判空的条件
printf("S[%d]=%d\n",S.top,S.data[S.top--]);
while (S.top >= 0) {//注意判空的条件
printf("S[%d]=%d\n", S.top, S.data[S.top--]);
}
printf("栈打印完毕\n");
}
//打印整个栈初始化方式1
void PrintStack1(SqStack S){
void PrintStack1(SqStack S) {
printf("从栈顶元素开始,栈如下:\n");
while (S.top>0){//注意判空的条件
printf("S1[%d]=%d\n",S.top-1,S.data[--S.top]);//初始化方式1得先移动指针再获取元素
while (S.top > 0) {//注意判空的条件
printf("S1[%d]=%d\n", S.top - 1, S.data[--S.top]);//初始化方式1得先移动指针再获取元素
}
printf("栈打印完毕\n");
}
@@ -117,34 +119,34 @@ void testStack() {
SqStack S;
printf("测试第一种初始化方式\n");
InitStack(S);
if (Push(S,1)){
if (Push(S, 1)) {
printf("入栈成功啦!\n");
} else{
} else {
printf("入栈失败了\n");
}
if (Push(S,2)){
if (Push(S, 2)) {
printf("入栈又成功啦!\n");
} else{
} else {
printf("入栈又失败了\n");
}
PrintStack(S);
int x;
if (Pop(S, x)){
printf("出栈成功,弹出的元素为:%d\n",x);
} else{
if (Pop(S, x)) {
printf("出栈成功,弹出的元素为:%d\n", x);
} else {
printf("出栈失败了,再检出一下吧!\n");
}
PrintStack(S);
int x1;
if (GetTop(S,x1)){
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x1);
}else{
if (GetTop(S, x1)) {
printf("读取栈顶元素成功了,栈顶元素为:%d\n", x1);
} else {
printf("读取栈顶元素失败,再检查一下吧!\n");
}
int x4=GetTopOther(S);
if (x4!=-1){
printf("第二种读取栈顶元素的方式成功了,栈顶元素为:%d\n",x4);
} else{
int x4 = GetTopOther(S);
if (x4 != -1) {
printf("第二种读取栈顶元素的方式成功了,栈顶元素为:%d\n", x4);
} else {
printf("第二种读取栈顶元素的方式失败了\n");
}
@@ -152,34 +154,34 @@ void testStack() {
printf("测试第二种初始化方式\n");
SqStack S1;
InitStack1(S1);
if (Push1(S1,1)){
if (Push1(S1, 1)) {
printf("入栈成功啦!\n");
} else{
} else {
printf("入栈失败了\n");
}
if (Push1(S1,2)){
if (Push1(S1, 2)) {
printf("入栈又成功啦!\n");
} else{
} else {
printf("入栈又失败了\n");
}
PrintStack1(S1);
int x2;
if (Pop1(S1, x2)){
printf("出栈成功,弹出的元素为[%d]\n",x2);
} else{
if (Pop1(S1, x2)) {
printf("出栈成功,弹出的元素为[%d]\n", x2);
} else {
printf("出栈失败了,再检出一下吧!\n");
}
PrintStack1(S1);
int x3;
if (GetTop1(S1,x3)){
printf("读取栈顶元素成功了,栈顶元素为:%d\n",x3);
}else{
if (GetTop1(S1, x3)) {
printf("读取栈顶元素成功了,栈顶元素为:%d\n", x3);
} else {
printf("读取栈顶元素失败,再检查一下吧!\n");
}
int x5=GetTopOther1(S1);
if (x5!=-1){
printf("第二种读取栈顶元素的方式成功了,栈顶元素为:%d\n",x5);
} else{
int x5 = GetTopOther1(S1);
if (x5 != -1) {
printf("第二种读取栈顶元素的方式成功了,栈顶元素为:%d\n", x5);
} else {
printf("第二种读取栈顶元素的方式失败了\n");
}