最近开始复习严蔚敏的《数据结构》,以下是顺序栈基本功能的实现,与书中完全对应。
/*
* 功能:顺序栈的实现
* 作者:风清扬
* 日期:2014-12-30
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT 2
typedef int Status;
typedef int SElemType;
//==============================================================================
/*------------------------------定义顺序栈数据类型-----------------------------*/
typedef struct
{
SElemType * bottom;//栈底
SElemType * top;//栈顶
int stackSize;//栈容量
} SqStack;
void InitStack_Sq(SqStack * S);
void DestroyStack_Sq(SqStack * S);
void ClearStack_Sq(SqStack * S);
Status StackEmpty_Sq(SqStack * S);
int StackLength_Sq(SqStack * S);
Status GetTop_Sq(SqStack * S, SElemType * e);
void Push(SqStack * S, SElemType e);
Status Pop(SqStack * S, SElemType * e);
void StackTraverse_Sq(SqStack * S, void (* vist)(SElemType));
//===============================================================================
void visit(SElemType e)
{
printf("%d ", e);
}
//===========================================================================================================
int main(void)
{
int index;
SqStack stack;
SElemType e;
InitStack_Sq(&stack);
for(index=1; index<=12; ++index)
Push(&stack, index);
printf("栈中元素依次为: ");
StackTraverse_Sq(&stack, visit);
Pop(&stack, &e);
printf("出栈元素为: %d\n", e);
printf("栈空否(1:空0:否):%d\n", StackEmpty_Sq(&stack));
GetTop_Sq(&stack, &e);
printf("栈顶元素为: %d\n栈的长度为: %d\n", e, StackLength_Sq(&stack));
ClearStack_Sq(&stack);
printf("清空栈后,栈空否(1:空0:否):%d\n",StackEmpty_Sq(&stack));
DestroyStack_Sq(&stack);
printf("销毁栈后,stack.top=%u stack.bottom=%u s.stackSize=%d\n",stack.top,stack.bottom, stack.stackSize);
return 0;
}
//===========================================================================================================
//===========================================================================================================
/*------------------------------------------顺序栈基本功能的实现-------------------------------------------*/
//构造一个空栈,栈的初始容量为 STACK_INIT_SIZE
void InitStack_Sq(SqStack * S)
{
S->bottom = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if (!S->bottom)
{
printf("内存不足,初始化栈失败,程序终止!\n");
exit(OVERFLOW);
}
S->top = S->bottom;
S->stackSize = STACK_INIT_SIZE;
}
//销毁S指向的栈,栈不再存在
void DestroyStack_Sq(SqStack * S)
{
free(S->bottom);
S->bottom = S->top = NULL;
S->stackSize = 0;
}
//把S指向的栈置为空栈
void ClearStack_Sq(SqStack * S)
{
S->top = S->bottom;
}
//若S指向的栈为空,返回TRUE,否则返回FALSE
Status StackEmpty_Sq(SqStack * S)
{
if (S->bottom == S->top)
return TRUE;
else
return FALSE;
}
//返回S指向的栈中元素个数,即栈的长度
int StackLength_Sq(SqStack * S)
{
return S->top-S->bottom;
}
//若栈不空,则用e返回栈顶元素,并返回OK;否则返回ERROR
Status GetTop_Sq(SqStack * S, SElemType * e)
{
if (S->bottom == S->top)
return ERROR;
*e = *(S->top-1);
return OK;
}
//入栈,如果栈满,则追加 STACK_INCREMENT 个元素空间
void Push(SqStack * S, SElemType e)
{
if ((S->top-S->bottom) == S->stackSize)
{
S->bottom = (SElemType *)realloc(S->bottom, (S->stackSize+STACK_INCREMENT)*sizeof(SElemType));
if (!S->bottom)
{
printf("内存分配失败,程序终止!\n");
exit(OVERFLOW);
}
S->top = S->bottom+S->stackSize;
S->stackSize += STACK_INCREMENT;
}
*(S->top)++ = e;
}
//出栈,若栈不空,则删除栈顶元素,用e返回其值,并返回OK;否则返回ERROR
Status Pop(SqStack * S, SElemType * e)
{
if (S->bottom == S->top)
return ERROR;
*e = *--(S->top);
return OK;
}
void StackTraverse_Sq(SqStack * S, void (* visit)(SElemType))
{
SElemType * p = S->bottom;
while (p != S->top)
visit(*p++);
printf("\n");
}