#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef int Elemtype;
typedef struct{
Elemtype *base[2];
Elemtype *top[2];
}BDStacktype; //双向栈类型
int Init_Stack(BDStacktype &tws,int m)//初始化一个大小为m的双向栈tws
{
tws.base[0]=(Elemtype*)malloc(sizeof(Elemtype));
tws.base[1]=tws.base[0] + m;
tws.top[0]=tws.base[0];
tws.top[1]=tws.base[1];
return OK;
}//Init_Stack
int push(BDStacktype &tws,int i,Elemtype x)//x入栈,i=0表示低端栈,i=1表示高端栈
{
if(tws.top[0]>tws.top[1]) return OVERFLOW;
if(i==0) *tws.top[0]++=x;
else if(i==1) *tws.top[1]--=x;
else return ERROR;
return OK;
}//push
int pop(BDStacktype &tws,int i,Elemtype &x)//x出栈,i=0表示低端栈,i=1表示高端栈
{
if(i==0)
{
if(tws.top[0]==tws.base[0]) return OVERFLOW;
x=*--tws.top[0];
// free(tws.top[0]+1);
}
else if(i==1)
{
if(tws.top[1]==tws.base[1]) return OVERFLOW;
x=*++tws.top[1];
// free(tws.top[1]);
}
else return ERROR;
return OK;
}//pop
void main()
{
Elemtype e;
BDStacktype tws;
Init_Stack(tws,6);
// printf("输入栈中元素");//i=0为低端,i=1为高端。
push(tws, 0, 1);
push(tws, 0, 2);
push(tws, 0, 3);
push(tws, 1, 6);
push(tws, 1, 5);
push(tws, 1, 4);
printf("输出元素:");
pop(tws,0,e);
printf("%d ", e);
pop(tws,0,e);
printf("%d ", e);
pop(tws,0,e);
printf("%d ", e);
pop(tws,1,e);
printf("%d ", e);
pop(tws,1,e);
printf("%d ", e);
pop(tws,1,e);
printf("%d ", e);
printf("\n");
// getchar();
system("PAUSE");
}
Critical error detected c0000374
Windows 已在 test.exe 中触发一个断点。
其原因可能是堆被损坏,这说明 test.exe 中或它所加载的任何 DLL 中有 Bug。
原因也可能是用户在 test.exe 具有焦点时按下了 F12。
输出窗口可能提供了更多诊断信息。
(VC运行完全OK。。。)