栈(记忆性线性结构)
是一种线性结构,基本特点:从同一端入栈或出栈,
先入后出
基本操作:入栈(push),出栈(pop)
基本组成:存储空间、栈顶指针、栈底指针
一个堆栈的必须需要的元素:堆栈空间、栈底指针、最大空间
#ifndef _STACK_H_
#define _STACK_H_
#include<malloc.h>
typedef unsigned char Boolean;
typedef struct STACK{
USER_TYPE *stack;
unsigned int maxRoom;
unsigned int top;
}STACK;
#define TURE 1
#define FLASE 0
Boolean initStack(STACK **head,unsigned int maxRoom);
Boolean destoryStack(STACK **head);
Boolean isStackEmpty(STACK stack);
Boolean isStackFull(STACK stack);
Boolean push(STACK *stack,USER_TYPE data);
Boolean pop(STACK *stack,USER_TYPE *data);
Boolean readTop(STACK stack,USER_TYPE *data);
Boolean readTop(STACK stack,USER_TYPE *data){
Boolean OK = TURE;
if(isStackFull(stack))
OK = FLASE;
else
*data = stack.stack[stack.top - 1];
}
Boolean pop(STACK *stack,USER_TYPE *data){
Boolean OK = TURE;
if(stack == NULL || isStackFull(*stack))
OK = FLASE;
else
*data = stack->stack[--stack->top];
return OK;
}
Boolean push(STACK *stack,USER_TYPE data){
Boolean OK = TURE;
if(stack == NULL || isStackEmpty(*stack))
OK = FLASE;
else
stack->stack[stack->top++] = data;
return OK;
}
Boolean isStackFull(STACK stack){
return stack.top >= stack.maxRoom;
}
Boolean isStackEmpty(STACK stack){
return 0 == stack.top;
}
Boolean destoryStack(STACK **head){
Boolean OK = TURE;
if(*head == NULL){
OK = FLASE;
}else{
free((*head)->stack);
free(*head);
(*head) = NULL;
}
return OK;
}
Boolean initStack(STACK **head,unsigned int maxRoom){
Boolean OK = TURE;
if(maxRoom <= 0 || (*head)){
OK = FLASE;
}
if(OK && (*head = (STACK *)malloc(sizeof(STACK))) == NULL){
OK = FLASE;
}else if(OK && (*head)){
(*head)->maxRoom = maxRoom;
(*head)->top = 0;
(*head)->stack = (USER_TYPE *)malloc(sizeof(USER_TYPE)*maxRoom);
if((*head)->stack == NULL){
free((*head));
*head = NULL;
OK = FLASE;
}
}
return OK;
}
#endif