대학원 데이터 구조 복습 - 스 택 (순서 표)

/**
***@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!
"); 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!!
"); return ERROR; } stack->top=stack->base; return OK; } // stack , TRUE, FALSE Bool StackEmpty(SqStack stack){ if(!stack.base){ printf("Error!! Please inital first!!
"); exit(0); } return stack.base==stack.top? TRUE : ERROR; } // stack int StackLength(SqStack stack){ if(!stack.base){ printf("Error!! Please inital first!!
"); exit(0); } return stack.top-stack.base ; } // , e , OK, ERROR Status GetTop(SqStack stack,SElemType *e){ if(!stack.base){ printf("Error!! Please inital first!!
"); 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!!
"); 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!!
"); 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("
length:%d
",StackLength(stack)); GetTop(stack,&a); printf("IsEmpty:%d Top:%d

",StackEmpty(stack),a); ClearStack(&stack); printf("["); StackTraverse(stack, PrintOne); printf("]"); printf("
length:%d
",StackLength(stack)); GetTop(stack,&a); printf("IsEmpty:%d Top:%d

",StackEmpty(stack),a); for(i=1;i<=10;i++) Push(&stack,(SElemType)rand()%100); printf("["); StackTraverse(stack, PrintOne); printf("]"); printf("
length:%d
",StackLength(stack)); GetTop(stack,&a); printf("IsEmpty:%d Top:%d

",StackEmpty(stack),a); Pop(&stack, &b); printf("Pop one:%d
",b); printf("["); StackTraverse(stack, PrintOne); printf("]"); printf("
length:%d
",StackLength(stack)); GetTop(stack,&a); printf("IsEmpty:%d Top:%d

",StackEmpty(stack),a); DestroyStack(&stack); printf("["); StackTraverse(stack, PrintOne); printf("]"); return 0; }

좋은 웹페이지 즐겨찾기