码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构 栈 c++ 源代码实现

时间:2017-11-26 14:57:33      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:数据结构 栈 c/c++

#include <malloc.h>

#include <stdlib.h>

#define  STACK_INIT_SIZE 10

#define  STACK_INCREMENT_SIZE 10

#define Status bool

#define OK true

#define ERROR false

typedef int SElemType;


typedef struct SqStack{

SElemType* top;

SElemType* base;

int stackSize;

}SqStack,*pSqStack;


class Stack{

public:

Stack();

~Stack();

Status InitStack(SqStack &S);

Status DestroyStack(SqStack &S);

Status ClearStack(SqStack &S);

Status StackIsEmpty(SqStack &S);

Status StackLength(SqStack &S,int& len);

Status getTop(SqStack &S,SElemType& e);

Status push(SqStack &S,SElemType& e);

Status pop(SqStack &S,SElemType& e);

};

Stack::Stack(){


}

Stack::~Stack(){


}


/*

初始化栈

分配默认大小空间

*/

Status Stack::InitStack(SqStack &S){

S.base = (SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));

if (!S.base)

return ERROR;

S.top = S.base;

S.stackSize = STACK_INCREMENT_SIZE;

return OK;

}

/*

销毁栈

默认分配的空间也会被回收

*/

Status Stack::DestroyStack(SqStack &S){

if (S.base)

       free(S.base);

S.base = S.top = NULL;

return true;

}

/*

清空栈

逻辑上的清空

*/

Status Stack::ClearStack(SqStack &S){

if (!S.base)

  return ERROR;

S.top = S.base;

}

/*

栈是否为空

*/

Status Stack::StackIsEmpty(SqStack &S){

if (S.top == S.base) return OK;

return ERROR;

}

/*

栈的长度

*/

Status Stack::StackLength(SqStack &S,int& len){

if (S.top == S.base){

len = 0;

return ERROR;

}

len = (S.top - S.base)%(S.stackSize+1);

return OK;

}

/*

栈顶得值

*/

Status Stack::getTop(SqStack &S,SElemType& e){

if (S.top == S.base) return ERROR;

e = *(S.top-1);

return OK;

}

/*

压栈

*/

Status Stack::push(SqStack &S,SElemType& e){

if (S.top - S.base >= S.stackSize)

{

//需要增加空间

S.base = (SElemType*)realloc(S.base,(S.stackSize+STACK_INCREMENT_SIZE)*sizeof(SElemType));

if (!S.base) return ERROR;

S.top = S.base+S.stackSize;

S.stackSize+=STACK_INCREMENT_SIZE;

}

*S.top++ = e;

return OK;

}

/*

出栈

*/

Status Stack::pop(SqStack &S,SElemType& e){

if (S.top == S.base)return ERROR;

e=*--S.top;

return OK;

}


/*

测试


*/

int _tmain(int argc, _TCHAR* argv[])

{

SqStack sq;

Stack* stack = new Stack();


stack->InitStack(sq);

if(stack->StackIsEmpty(sq))

printf("栈为空,请push\n");

SElemType i = 0;

while(i++<30)

stack->push(sq,i);

if (stack->StackIsEmpty(sq))

printf("栈不为空\n");

int len;

if (stack->StackLength(sq,len))

printf("栈的长度=%d\n",len);

SElemType e;

if (stack->getTop(sq,e))

printf("栈顶元素=%d\n",e);

i=0;

while (i++<len)

{

if (stack->pop(sq,e))

{

printf("栈顶元素%d=%d\n",i,e);

}

}

if (stack->StackIsEmpty(sq))

printf("栈为空\n");   

stack->ClearStack(sq);

stack->DestroyStack(sq);

if (NULL == sq.base)

{

printf("栈已销毁\n"); 

}

if (stack)

{

delete stack;

}

getchar();

return 0;

}



数据结构 栈 c++ 源代码实现

标签:数据结构 栈 c/c++

原文地址:http://blog.51cto.com/whish/2044374

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!