栈的输出_数据结构与算法:4栈的链式存储

上一章我们讲了栈的线性存储这里我们将会讲解栈的链式存储,和线性表的顺序存储和链式存储一样。栈的链式存储也解决了栈的顺序存储需要事先分配整块存储空间的问题。

栈的结构

typedef struct stack{ struct sStackNode *top; int len;}sStack,*psStack;
8bf536bdadc649896fdfe5def67bfe5d.gif

下面来介绍一下栈的操作:

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 0typedef 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
8bf536bdadc649896fdfe5def67bfe5d.gif

stack.c

#include #include #include "stack.h"static const int StackMask=MAX;sStack * StackInit(){ sStack *stack=NULL; stack=(psStack)malloc(sizeof(sStack)); if(stack) { printf("栈创建成功"); stack->top=NULL; stack->len=0; } return stack;}void StackDestory(sStack *stack){ if(stack) { StackClear(stack); free(stack); stack=NULL; printf("释放栈成功"); }}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->lendata=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",i,node->data); node=node->pre; i++; }  }}
8bf536bdadc649896fdfe5def67bfe5d.gif

main.c

#include #include "stack.h"int main(){ sStack *stack=NULL; int status=0; stack=StackInit(); if(!stack) { printf("初始化栈失败"); } status=StackEmpty(stack); if(status) { printf("栈为空"); } else { printf("栈不为空"); } status=Push(stack,11); if(status) { printf("压栈成功"); } else { printf("压栈失败"); } status=StackEmpty(stack); if(status) { printf("栈为空"); } else { printf("栈不为空"); } status=Push(stack,22); if(status) { printf("压栈成功"); } else { printf("压栈失败"); } status=StackLength(stack); printf("当前栈的长度为%d",status); status=Push(stack,33); if(status) { printf("压栈成功"); } else { printf("压栈失败"); } status=StackFull(stack); if(status) { printf("栈为满"); } else { printf("栈不为满"); } status=StackLength(stack); printf("当前栈的长度为%d",status); status=GetTop(stack); printf("当前栈的栈顶的值为%d",status); status=Push(stack,44); if(status) { printf("压栈成功"); } else { printf("压栈失败"); } status=StackFull(stack); if(status) { printf("栈为满"); } else { printf("栈不为满"); } StackDisplay(stack); status=Push(stack,55); if(status) { printf("压栈成功"); } else { printf("压栈失败"); } status=StackFull(stack); if(status) { printf("栈为满"); } else { printf("栈不为满"); } status=StackLength(stack); printf("当前栈的长度为%d",status); status=GetTop(stack); printf("当前栈的栈顶的值为%d",status); status=Pop(stack); printf("当前取栈的栈顶的值为%d",status); status=StackFull(stack); if(status) { printf("栈为满"); } else { printf("栈不为满"); } status=StackLength(stack); printf("当前栈的长度为%d",status); status=GetTop(stack); printf("当前栈的栈顶的值为%d",status); StackClear(stack); status=StackEmpty(stack); if(status) { printf("栈为空"); } else { printf("栈不为空"); } status=StackLength(stack); printf("当前栈的长度为%d",status); StackDestory(stack); return 0;}
8bf536bdadc649896fdfe5def67bfe5d.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值