《数据结构》严蔚敏 3.1 栈

It’s enough easy,so I complete it smoothly.
Well,This morning I meet a handsome and nice Philadelphia boy,May be It’s also a catalyst~

the screenshot of result

//Order Stack

#include<stdio.h>
#include<stdlib.h>

#define STACK_INIT_SIZE 100
#define STACKINCREMENT  10

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0


typedef int SElemType;
typedef int Status;

typedef struct
{
	SElemType *base;
	SElemType *top;

	int stackesize;

}SqStack;

Status
InitStack(SqStack *S)
{
	(*S).base =(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SqStack));
	//why first I used the (*S).top ???
	//the verfy I always forget... So my code always weak...
	if(!S->base)
		exit(ERROR);

	(*S).top = (*S).base; // If you don't understand it ,I think you should learn about the structure in C firstly.
	(*S).stackesize = STACK_INIT_SIZE;

	return OK;
}

// Well,Remeber,It's only use in allocation stack dynamic

Status
DestoryStack(SqStack *S)
{
	if(S->stackesize > 0)
	{
		S->stackesize = 0;
	}

	free(S);
	S = NULL;
	return OK;

}

Status ClearStack(SqStack *S)
{
	//(*S).top = (*S).base;

	while(S->base != S->top)
		S->top -- ;
	S->stackesize = 0;

	return OK;
}

Status StackEmpty(SqStack *S)
{
	if(S->top == S->base)
		return TRUE;
	else
		return FALSE;

	// or use ? : make things seems concise

}

//point could do subtraction?
// obviously ok1

int
StackLength(SqStack S)
{
	// int i = 0;;
	// while(S.top != S.base)
	// {
	// 	i++;
	// 	S.top --;
	// }
	// return i;
	return S.top - S.base;
}

Status
GetTop(SqStack S,SElemType *e)
{
	if(S.top == S.base)
		return ERROR;
	else
		//e = S.top; No,top point is above the really elements stored.
		*e = *(S.top -1);

	return OK;

}

// When Push,first add then change the point

Status
Push(SqStack *S,SElemType e)
{
	//lack of verfy
	if(S->top - S->base >= S->stackesize)
	{
		S->base = (SElemType*)realloc(S->base,
			(S->stackesize + STACK_INIT_SIZE)*sizeof(SElemType));
		if(!S->base)
			exit(ERROR);
		S->top = S->base + S->stackesize;
		S->stackesize += STACKINCREMENT;
	}
	

	////////// the first ////
	*(*S).top = e;
	S->top ++;
	
	return OK;	

}

//when pop,first change point then pop element

Status
Pop(SqStack *S,SElemType *e)
{
	if(StackEmpty(S) == 0)
	{
		S->top --;
		//e = (*S).top ;
		//well must write like this,orthise can;t getthe return value
		*e = *(*S).top; 
		//it this ok ? How poped?
		return OK;
	}
	else
		return ERROR;

}

//reversr output
Status
StackTraverse(SqStack S,Status(*visit)(SElemType c))
{
	do
	{
		S.top --;
		visit(*S.top); 

	}while(S.top != S.base);

	printf("\n");
	return OK;

}

Status
visit(SElemType c)
{
	printf("%d ", c);
	return OK;

}

int main(int argc, char const *argv[])
{
	SElemType pop_value = 0;
	SElemType Top = 0;

	SqStack S;
	InitStack(&S);
	Push(&S,1);
	Push(&S,2);
	Push(&S,3);
	Push(&S,4);

	StackTraverse(S,visit);

	GetTop(S,&Top);
	printf(" Top is %d\n", Top);
	StackTraverse(S,visit);

	Pop(&S,&pop_value);
	
	printf(" pop_value is %d\n", pop_value);
	StackTraverse(S,visit);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37414405/article/details/85910706
今日推荐