同样的代码我和我同学电脑上运行的结果不一样,我的不断显示线程退出,进程退出。是我的代码错了还是别的问题呢?调试结果如图
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <process.h>
#include <iostream>
using namespace std;
#define Status int
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100 //存储空间初始分配量
#define STACKINCREMENT 10 //存储空间分配增量
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
void visit(SElemType &e) {
printf("%d\n", e);
}
//————基本操作的函数原型说明————
Status InitStack(SqStack &S);
Status Push(SqStack S, SElemType e);
Status Pop(SqStack S, SElemType e);
Status StackTraverse(SqStack S,void(*visit)(SElemType&));
Status StackEmpty(SqStack S);
Status InitStack(SqStack &S)
{//构造一个空栈S
S.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!S.base) exit (OVERFLOW);//存储分配失败
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}//InitStack
Status Push(SqStack S, SElemType e) {
//插入新元素e为新的栈顶元素
if (S.top - S.base >= S.stacksize) {//栈满,追加存储空间
S.base = (SElemType*)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
if (!S.base)exit(OVERFLOW);//存储分配失败
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top++ = e;
return OK;
}//Push
Status Pop(SqStack S, SElemType e) {
//若栈不空,则拿出S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
if (S.top == S.base)return ERROR;
e = *--S.top;
return OK;
}//Pop
Status StackTraverse(SqStack S,void(*visit)(SElemType&)){
SElemType* p;
p=S.base;
while(S.top>p) visit(*p++);
return OK;
}//StackTraverse
Status StackEmpty(SqStack S){
if (S.top==S.base) return TRUE;
return FALSE;
};//StackEmpty
int main() {
SqStack S;
int i;
SElemType e;
InitStack(S);
Push(S,27);
Push(S, 927);
Push(S, 112);
Push(S, 10);
Push(S, 25);
StackTraverse(S,*visit);
}