1. 程式人生 > >棧的順序表示與實現

棧的順序表示與實現

#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;
}