一段循环出错了
#include <stdio.h>
#define Maxsize 100
typedef int ElemType;
typedef struct {
ElemType data[Maxsize];
int top;
} SqStack;
void InitStack(SqStack &st) {
st.top = -1;
}
void DestoryStack(SqStack st)
{}
int Push(SqStack &st, ElemType x) {
if (st.top == Maxsize - 1)
return 0;
else {
st.top++;
st.data[st.top] = x;
return 1;
}
}
int Pop(SqStack &st, ElemType &x) {
if (st.top == -1)
return 0;
else {
x = st.data[st.top];
st.top--;
return 1;
}
}
int GetTop(SqStack st, ElemType &x) {
if (st.top == -1)
return 0;
else {
x = st.data[st.top];
return 1;
}
}
int StackEmpty(SqStack st) {
if (st.top == -1)
return 1;
else
return 0;
}
int main() {
SqStack st;
InitStack(st);
ElemType n, i;
ElemType k = 0;
ElemType k1 = 0;
ElemType tem[20];
ElemType tem1[20];
ElemType e;
ElemType flag = 0;
printf("栈的长度:");
scanf("%d", &n);
printf("\n");
printf("入栈的次序:");
for (i = 0; i < n; i++)
scanf("%d", &tem[i]);
printf("\n");
printf("出栈的次序:");
for (i = 0; i < n; i++)
scanf("%d", &tem1[i]);
printf("\n");
printf("判断结果:");
printf("\n");
//
for (k; k < n; k++) {
// e = tem[k];
Push(st, tem[k]);
printf("%d进栈", tem[k]);
printf("\n");//输入序列一个个入栈
while (st.data[st.top] == tem1[k1]) { //判断栈顶元素与输出序列的第k1个元素是否相等(相等则出栈)
Pop(st, tem[k]);
k1++;
printf("%d出栈", tem[k]);
printf("\n");
}
}
//
if (StackEmpty(st)) {
printf("结束");
} else
printf("不行");
return 0;
}
运行结果如下