栈的顺序表示与实现

#include <stdio.h>
#include <malloc.h>

/**********************Data Object Structure********************/
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
#define ElemType int
typedef struct
{
	ElemType *base;//Stack bottom pointer
	ElemType *top;//Stack top pointer
	int capacity;//Stack capacity
}SqStack;

#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define null -65535

/***********************Operations******************************/
int InitStack(SqStack* stack);/* Initialize stack */
void DestroyStack(SqStack* stack);/* Destroy stack */
void ClearStack(SqStack* stack);/* Reset stack */
int Empty(SqStack stack);/* Wether stack empty */
int Size(SqStack stack);/* Length of stack */
ElemType PeekTop(SqStack stack);/* Return top but not remove from stack */
int Push(SqStack* stack,ElemType e);/* Push */
ElemType Pop(SqStack* stack);/* Return top element in stack and remove from it */ 
void PrintStack(SqStack stack);/* Print stack elements */
void PrintlnStack(SqStack stack);/* Print stack elements and return a new line */

int InitStack(SqStack* stack)
{
	stack->base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));//Default size
	
	if(!stack->base)//Malloc failed
		return OVERFLOW;
	
	stack->top=stack->base;
	stack->capacity=STACK_INIT_SIZE;

	return TRUE;
}

void DestroyStack(SqStack* stack)
{
	free(stack->base);
	stack->base=NULL;
	stack->top=NULL;
	stack->capacity=0;
}

void ClearStack(SqStack* stack)
{
	stack->top=stack->base;
}

int Empty(SqStack stack)
{
	if(stack.top==stack.base)
		return TRUE;
	
	return FALSE;
}

int Size(SqStack stack)
{
	return stack.top-stack.base;
}

ElemType PeekTop(SqStack stack)
{
	if(stack.top==stack.base)//Empty
		return null;
	
	return *(stack.top-1);
}

int Push(SqStack* stack,ElemType e)
{
	if(stack->top-stack->base >= stack->capacity)//Stack is full
	{
		stack->base=(ElemType*)realloc(stack->base, (STACK_INIT_SIZE+STACK_INCREMENT)*sizeof(ElemType) );
		if(!stack->base)
			return OVERFLOW;

		stack->top=stack->base+stack->capacity;
		stack->capacity+=STACK_INIT_SIZE;
	}

	*stack->top++=e;//Set value and move to next position

	return TRUE;
}

ElemType Pop(SqStack* stack)
{
	if(stack->top==stack->base)//Empty
		return null;

	return *--stack->top;
}

void PrintStack(SqStack stack)
{
	ElemType* p=NULL;

	if(!stack.base)//Not Exist
	{
		printf("null\n");
		return;
	}

	if(stack.base==stack.top)//Empty
	{
		printf("null");
		return;
	}

	p=stack.base;
	while(p<stack.top-1)
	{
		printf("%d ",*p++);
	}
	printf("%d",*p);
}

void PrintlnStack(SqStack stack)
{
	ElemType* p=NULL;

	if(!stack.base)//Not Exist
	{
		printf("null\n");
		return;
	}

	if(stack.base==stack.top)//Empty
	{
		printf("null\n");
		return;
	}

	p=stack.base;
	while(p<stack.top-1)
	{
		printf("%d ",*p++);
	}
	printf("%d\n",*p);
}


/* Test Instance */
int main(int argc , char** argv)
{
	SqStack stack;
	InitStack(&stack);
	
	Push(&stack,1);
	Push(&stack,2);
	Push(&stack,3);
	Push(&stack,4);
	Push(&stack,5);
	printf("After pushed 1 2 3 4 5:\n");
	printf("Stack size: %d\n",Size(stack));
	PrintlnStack(stack);

	printf("PeekTop:%d\n",PeekTop(stack));
	printf("After PeekTop:\n");
	PrintlnStack(stack);

	Pop(&stack);
	printf("After pup:\n");
	printf("Stack size: %d\n",Size(stack));
	PrintlnStack(stack);

	DestroyStack(&stack);
	printf("After Destroyed:\n");
	PrintlnStack(stack);
	
	Push(&stack,5);
	Push(&stack,4);
	Push(&stack,3);
	Push(&stack,2);
	Push(&stack,1);

	printf("Push 5 4 3 2 1:\n");
	PrintlnStack(stack);

	ClearStack(&stack);
	printf("After clear:\n");
	PrintlnStack(stack);

	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值