stack.h

来自「数据结构的拓扑排序算法」· C头文件 代码 · 共 48 行

H
48
字号
#include "malloc.h"
#define STACK_INT_SIZE 100;
#define STACKINCREMENT	10;
#define  OK 1;
#define  OVERFLOW 0;
#define ERROR 0;
typedef int SElemType;
typedef	bool	Status;

typedef struct{
	SElemType *base;
	SElemType *top;
	int stacksize;
}SqStack;

Status InitStack(SqStack &S)
{S.base=(SElemType *)malloc(100 * sizeof(SElemType));
//S.base=(SElemType *)malloc(STACK_INT_SIZE * sizeof(SElemType));
if(!S.base) return OVERFLOW ;
S.top=S.base;
S.stacksize=STACK_INT_SIZE;
return OK;
}

Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{S.base=(SElemType *)realloc(S.base,(S.stacksize+10) * sizeof(SElemType));
if(!S.base) return OVERFLOW;
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}

Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}

Status StackEmpty(SqStack &S)
{if(S.top==S.base) return 1;
else return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?