考研数据结构复习——栈(顺序表)

/**
***@Title   :考研数据结构复习
***@Subject :栈(顺序结构)
***@Author  :lxfhahaha
***@language: C语言
***@Time    : 2018/9/26 15:29
*****/ 
#include 
#include 
#include 
#define Inital_Size 50
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int Bool;
typedef int SElemType;
typedef int Status;

typedef struct { 
	SElemType *base;
	SElemType *top;
	int stacksize; //当前已分配的存储空间
}SqStack;

//操作结果:构造一个空栈
Status InitStack(SqStack *stack){
	stack->base=(SElemType*)malloc(Inital_Size*sizeof(SElemType));
	if(!stack->base){
		printf("Malloc Error!\n");
		exit(0);	
	}
	stack->top=stack->base;
	stack->stacksize=Inital_Size;
	return OK;
}

//销毁栈,stack不再存在
Status DestroyStack(SqStack *stack){
	if(!stack->base){  //不存在
		return ERROR;
	}else{
		free(stack->base);
		stack->top=NULL;
		stack->base=NULL;
		stack->stacksize=0;
		return OK;	
	}
}

//把stack置为空栈
Status ClearStack(SqStack *stack){
	if(!stack->base){
		printf("Error!! Please inital first!!\n");
		return ERROR;
	}
	stack->top=stack->base;
	return OK;	
}

//若stack为空,返回TRUE,非空返回FALSE
Bool StackEmpty(SqStack stack){
	if(!stack.base){
		printf("Error!! Please inital first!!\n");
		exit(0);
	}
	return stack.base==stack.top? TRUE : ERROR;	
}

//返回stack中数据元素个数
int StackLength(SqStack stack){
	if(!stack.base){
		printf("Error!! Please inital first!!\n");
		exit(0);
	}
	return stack.top-stack.base ;
}

//若栈不空,则若e返回栈顶的元素,并返回OK,否则返回ERROR
Status GetTop(SqStack stack,SElemType *e){
	if(!stack.base){
		printf("Error!! Please inital first!!\n");
		exit(0);
	}
	if(stack.base==stack.top){
		*e=NULL;
		return ERROR;
	}	
	*e=*(stack.top-1);
	return OK;
}

//插入元素e为新的栈顶元素
Status Push(SqStack *stack,SElemType e){
	if(!stack->base){
		printf("Error!! Please inital first!!\n");
		exit(0);
	}
	if(stack->top-stack->base>=stack->stacksize){
		SElemType *new_base=(SElemType *)realloc(stack->base,(stack->stacksize+Inital_Size)*sizeof(SElemType));
		if(!new_base){
			printf("Realloc Error");
			exit(0);
		}
		stack->base=new_base;
		stack->top=stack->base+stack->stacksize;
		stack->stacksize+=Inital_Size;
	}	
	*(stack->top)=e;
	stack->top++;
	return OK;
}


//若栈不空,则删除stack的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
Status Pop(SqStack *stack,SElemType *e){
	if(!stack->base){
		printf("Error!! Please inital first!!\n");
		exit(0);
	}
	if(stack->top==stack->base){
		return ERROR;	
	}	
	*e=*(stack->top-1);
	stack->top--;
	return OK;
}

//从栈底到栈顶依次对栈中每个元素调用visit(),一旦visit()失败,则操作失败
Status StackTraverse(SqStack stack,Status (*visit)(SElemType)){
	int length=StackLength(stack);
	while(length--){
		if(!(*visit)(*(stack.base)))
			exit(0);
		stack.base++;
	}
	return OK;	
}

//test1——print
Status PrintOne(SElemType one){
	printf("%d->",one);
	return OK;
}


int main()
{
	SqStack stack;
	int i,j;
	SElemType a,b,c,d;
	InitStack(&stack);
	srand((int)time(0));		
	for(i=1;i<=10;i++)
		Push(&stack,(SElemType)rand()%100);
	printf("[");
	StackTraverse(stack, PrintOne);
	printf("]");
	printf("\nlength:%d\n",StackLength(stack));
	GetTop(stack,&a);
	printf("IsEmpty:%d  Top:%d\n\n",StackEmpty(stack),a);	
	
	
	ClearStack(&stack);
	printf("[");
	StackTraverse(stack, PrintOne);
	printf("]");
	printf("\nlength:%d\n",StackLength(stack));
	GetTop(stack,&a);
	printf("IsEmpty:%d  Top:%d\n\n",StackEmpty(stack),a);	
	
	for(i=1;i<=10;i++)
		Push(&stack,(SElemType)rand()%100);
	printf("[");
	StackTraverse(stack, PrintOne);
	printf("]");
	printf("\nlength:%d\n",StackLength(stack));
	GetTop(stack,&a);
	printf("IsEmpty:%d  Top:%d\n\n",StackEmpty(stack),a);
	
	
	Pop(&stack, &b);
	printf("Pop one:%d\n",b);
	printf("[");
	StackTraverse(stack, PrintOne);
	printf("]");
	printf("\nlength:%d\n",StackLength(stack));
	GetTop(stack,&a);
	printf("IsEmpty:%d  Top:%d\n\n",StackEmpty(stack),a);
	
	
	DestroyStack(&stack);
	printf("[");
	StackTraverse(stack, PrintOne);
	printf("]");
	
	return 0;	
}

你可能感兴趣的:(数据结构,考研,数据结构,C语言,栈,顺序表)