stack.cpp

来自「Fibonacci数列的c语言实现」· C++ 代码 · 共 77 行

CPP
77
字号
//使用前应定义SElemType
#include <malloc.h>
#include "preDefine.h"

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

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

Status InitStack(SqStack &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;
}

Status DestroyStack(SqStack &S)
{
 free(S.base); 
 return OK;
}

Status ClearStack(SqStack &S)
{
  S.top=S.base;
  return OK;
}

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

Status Push(SqStack &S,SElemType 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 (1);
}


bool StackEmpty(SqStack S)                     
{
	if(S.top==S.base) return true;
		else return false;
}

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

SElemType* GetMark(SqStack &S)
{
	return S.top;
}

Status SetMark(SqStack &S,SElemType *mark)
{
	S.top=mark;
	return OK;
}

⌨️ 快捷键说明

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