Skip to content

Commit

Permalink
Update DS_2_0_SqStack.cpp
Browse files Browse the repository at this point in the history
The expressions in printf() will get evaluated first before print out, so the counter needs to be put outside the printf() function.
  • Loading branch information
BruceDai003 authored Nov 25, 2020
1 parent 704ee1e commit ba18b5f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions DataStructure/Src/DS_2_StackAndQueue/DS_2_0_SqStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ int GetTopOther1(SqStack S) {
void PrintStack(SqStack S) {
printf("从栈顶元素开始,栈如下:\n");
while (S.top >= 0) {//注意判空的条件
printf("S[%d]=%d\n", S.top, S.data[S.top--]);
printf("S[%d]=%d\n", S.top, S.data[S.top]);
S.top--;
}
printf("栈打印完毕\n");
}
Expand All @@ -129,7 +130,8 @@ void PrintStack(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得先移动指针再获取元素
S.top--;
printf("S1[%d]=%d\n", S.top, S.data[S.top]);//初始化方式1得先移动指针再获取元素
}
printf("栈打印完毕\n");
}
Expand Down Expand Up @@ -214,4 +216,4 @@ int main() {
printf("Hello, SqStack!");
testStack();
return 0;
}
}

0 comments on commit ba18b5f

Please sign in to comment.