栈是线性表的一种特例,它使得数据先入后出,就行垒砖似的最先垒的砖在最下面但是取的时候需要最后才能取到,最后垒的砖在最上面,但是取的时候是第一个取走。
栈的结构:
typedef struct stack
{
int data[MAX];
int top;
}sStack,*psStack;
这里是使用的int型的数据,data数组为栈空间,最多数据元素为MAX个,top指向栈的栈顶。
下面来介绍一下栈的操作:
1.初始化栈
sStack * StackInit()
用于创建栈申请栈空间,返回非0值表示初始化栈成功返回值为栈地址,返回0表示失败。
2.销毁栈
void StackDestory(sStack *stack)
释放申请的栈空间,需要传入创建栈时返回的地址,无返回值。
3.设置栈为空栈
void StackClear(sStack *stack)
将存在的栈设置为空栈,需要传入创建栈时返回的地址,无返回值。
4.获取栈中数据元素的数量
int StackLength(sStack *stack)
获取栈中数据元素的数量,需要传入创建栈时返回的地址,返回栈中元素的数量。
5.判断栈是否为空栈
bool StackEmpty(sStack *stack)
判断栈是否为空栈,需要传入创建栈时返回的地址,返回栈是否为空栈的结果。
6.判断栈是否为满栈
bool StackFull(sStack *stack)
判断栈是否为满栈,需要传入创建栈时返回的地址,返回栈是否为满栈的结果。
7.将数据元素压入栈
bool Push(sStack *stack,int value)
将数据压入栈,需要传入创建栈时返回的地址和要压入的数据的值,返回将数据压入栈的结果。
8.出栈
int Pop(sStack *stack)
弹出栈顶的元素,需要传入创建栈时返回的地址,返回栈顶的元素的值。
9.获取栈顶元素
int GetTop(sStack *stack)
获取栈顶元素,但是不更改栈的结构即栈顶元素不出栈,需要传入创建栈时返回的地址,返回栈顶元素的值。
10.显示栈中的所有元素
void StackDisplay(sStack *stack)
显示栈中所有的元素,需要传入创建栈时返回的地址。
源程序
stack.h
#ifndef _STACK_H
#define _STACK_H
#define MAX 4
#define STACK_OK 0
#define STACK_ERR -1
#define TRUE 1
#define FALSE 0
typedef int bool;
typedef struct stack
{
int data[MAX];
int top;
}sStack,*psStack;
sStack * StackInit();
void StackDestory(sStack *stack);
void StackClear(sStack *stack);
int StackLength(sStack *stack);
bool StackEmpty(sStack *stack);
bool StackFull(sStack *stack);
bool Push(sStack *stack,int value);
int Pop(sStack *stack);
int GetTop(sStack *stack);
void StackDisplay(sStack *stack);
#endif
stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
static const int StackMask=MAX-1;
sStack * StackInit()
{
sStack *stack=NULL;
stack=(psStack)malloc(sizeof(sStack));
if(stack)
{
printf("栈创建成功\n");
stack->top=-1;
}
return stack;
}
void StackDestory(sStack *stack)
{
if(stack)
{
free(stack);
stack=NULL;
printf("释放栈成功\n");
}
}
void StackClear(sStack *stack)
{
if(stack)
{
stack->top=-1;
}
}
int StackLength(sStack *stack)
{
if(stack)
{
return stack->top+1;
}
return STACK_ERR;
}
bool StackEmpty(sStack *stack)
{
if(stack&&(stack->top==-1))
{
return TRUE;
}
return FALSE;
}
bool StackFull(sStack *stack)
{
if(stack&&(stack->top==StackMask))
{
return TRUE;
}
return FALSE;
}
bool Push(sStack *stack,int value)
{
if(stack&&(StackMask>stack->top))
{
stack->data[++stack->top]=value;
return TRUE;
}
return FALSE;
}
int Pop(sStack *stack)
{
if(stack&&stack->top>=0)
{
return stack->data[stack->top--];
}
return STACK_ERR;
}
int GetTop(sStack *stack)
{
if(stack&&stack->top>=0)
{
return stack->data[stack->top];
}
return STACK_ERR;
}
void StackDisplay(sStack *stack)
{
if(stack&&stack->top>=0)
{
int i;
for(i=0;i<=stack->top;i++)
{
printf("The %d is %d\n",i,stack->data[i]);
}
}
}
main.c
#include <stdio.h>
#include "stack.h"
int main()
{
sStack *stack=NULL;
int status=0;
stack=StackInit();
if(!stack)
{
printf("初始化栈失败\n");
}
status=StackEmpty(stack);
if(status)
{
printf("栈为空\n");
}
else
{
printf("栈不为空\n");
}
status=Push(stack,11);
if(status)
{
printf("压栈成功\n");
}
else
{
printf("压栈失败\n");
}
status=StackEmpty(stack);
if(status)
{
printf("栈为空\n");
}
else
{
printf("栈不为空\n");
}
status=Push(stack,22);
if(status)
{
printf("压栈成功\n");
}
else
{
printf("压栈失败\n");
}
status=StackLength(stack);
printf("当前栈的长度为%d\n",status);
status=Push(stack,33);
if(status)
{
printf("压栈成功\n");
}
else
{
printf("压栈失败\n");
}
status=StackFull(stack);
if(status)
{
printf("栈为满\n");
}
else
{
printf("栈不为满\n");
}
status=StackLength(stack);
printf("当前栈的长度为%d\n",status);
status=GetTop(stack);
printf("当前栈的栈顶的值为%d\n",status);
status=Push(stack,44);
if(status)
{
printf("压栈成功\n");
}
else
{
printf("压栈失败\n");
}
status=StackFull(stack);
if(status)
{
printf("栈为满\n");
}
else
{
printf("栈不为满\n");
}
StackDisplay(stack);
status=Push(stack,55);
if(status)
{
printf("压栈成功\n");
}
else
{
printf("压栈失败\n");
}
status=StackFull(stack);
if(status)
{
printf("栈为满\n");
}
else
{
printf("栈不为满\n");
}
status=StackLength(stack);
printf("当前栈的长度为%d\n",status);
status=GetTop(stack);
printf("当前栈的栈顶的值为%d\n",status);
status=Pop(stack);
printf("当前取栈的栈顶的值为%d\n",status);
status=StackFull(stack);
if(status)
{
printf("栈为满\n");
}
else
{
printf("栈不为满\n");
}
status=StackLength(stack);
printf("当前栈的长度为%d\n",status);
status=GetTop(stack);
printf("当前栈的栈顶的值为%d\n",status);
StackClear(stack);
status=StackEmpty(stack);
if(status)
{
printf("栈为空\n");
}
else
{
printf("栈不为空\n");
}
status=StackLength(stack);
printf("当前栈的长度为%d\n",status);
StackDestory(stack);
return 0;
}