上一章我们讲了栈的线性存储这里我们将会讲解栈的链式存储,和线性表的顺序存储和链式存储一样。栈的链式存储也解决了栈的顺序存储需要事先分配整块存储空间的问题。
栈的结构
typedef struct stack
{
struct sStackNode *top;
int len;
}sStack,*psStack;
下面来介绍一下栈的操作:
1.初始化栈
sStack * StackInit()
创建栈的头指针,返回栈的头指针的地址。
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,void * value)
向栈中压入元素,返回操作是否成功,成功返回真,反之返回假。
8.从栈中弹出元素
void * Pop(sStack *stack)
从栈中弹出一个元素。改变栈的长度。
9.获取栈顶元素的值
void * 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
{
struct sStackNode *top;
int len;
}sStack,*psStack;
typedef struct stacknode
{
void *data;
struct sStackNode *pre;
}sStackNode,psStackNode;
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,void * value);
void * Pop(sStack *stack);
void * GetTop(sStack *stack);
void StackDisplay(sStack *stack);
#endif
stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
static const int StackMask=MAX;
sStack * StackInit()
{
sStack *stack=NULL;
stack=(psStack)malloc(sizeof(sStack));
if(stack)
{
printf("栈创建成功\n");
stack->top=NULL;
stack->len=0;
}
return stack;
}
void StackDestory(sStack *stack)
{
if(stack)
{
StackClear(stack);
free(stack);
stack=NULL;
printf("释放栈成功\n");
}
}
void StackClear(sStack *stack)
{
if(stack)
{
int len;
sStackNode *current,*pre;
current=stack->top;
len=stack->len;
while ((len--)&¤t)
{
pre=current->pre;
free(current);
current=pre;
}
stack->len=0;
}
}
int StackLength(sStack *stack)
{
if(stack)
{
return stack->len;
}
return STACK_ERR;
}
bool StackEmpty(sStack *stack)
{
if(stack&&(!stack->len))
{
return TRUE;
}
return FALSE;
}
bool StackFull(sStack *stack)
{
if(stack&&(stack->len==StackMask))
{
return TRUE;
}
return FALSE;
}
bool Push(sStack *stack,void * value)
{
if(stack&&stack->len<StackMask)
{
sStackNode *node=NULL;
node =(sStackNode*)malloc(sizeof(sStackNode));
if(NULL==node)
{
return FALSE;
}
node->data=value;
node->pre=stack->top;
stack->top=node;
stack->len++;
return TRUE;
}
return FALSE;
}
void * Pop(sStack *stack)
{
if(stack&&stack->top>0)
{
void * data;
sStackNode *current;
current=stack->top;
stack->len--;
data=current->data;
stack->top=current->pre;
free(current);
return data;
}
return STACK_ERR;
}
void * GetTop(sStack *stack)
{
if(stack&&stack->top>0)
{
return (void *)((sStackNode *)(stack->top))->data;
}
return STACK_ERR;
}
void StackDisplay(sStack *stack)
{
if(stack&&stack->top>0)
{
sStackNode *node=stack->top;
int len=stack->len;
int i=0;
while (len--&&node)
{
printf("The %d is %d\n",i,node->data);
node=node->pre;
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;
}