数据结构C语言实现之链栈

来源:互联网 发布:广州丽晶软件 编辑:程序博客网 时间:2021/09/29 05:03
#include <stdio.h>#include <stdlib.h>//定义函数结果状态码#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0//定义数据类型typedef int ElemType ;//定义程序返回状态类型typedef int State;//栈中结点的存储结构typedef struct _StackNode{    ElemType data;    struct _StackNode *next;//指向栈顶位置}StackNode;//声明链栈//将头指针与栈顶指针结合typedef struct _LinkStack{    StackNode *top;//栈顶指针    int count;//栈长度}LinkStack;/*************************************************Function:       InitStackDescription:    初始化,构造空栈Input:          链栈指针 LinkStack *stackOutput:Return:         成功返回OKOthers:         空栈top为NULL,count=0*************************************************/State InitStack(LinkStack *stack){    stack->top = NULL;    stack->count = 0;    return OK;}/*************************************************Function:       IsStackEmptyDescription:    判断栈是否为空Input:          链栈指针 LinkStack *stackOutput:Return:         为空返回TRUE,否则返回FALSEOthers:*************************************************/State IsStackEmpty(LinkStack *stack){    if(stack->count == 0)        return TRUE;    else        return FALSE;}/*************************************************Function:       PushDescription:    入栈Input:          链栈指针 LinkStack *stack                数据元素   ElemType eOutput:Return:         成功返回OK,失败返回ERROROthers:         新结点的next指向位置与单链表不同,是指向前一个*************************************************/State Push(LinkStack *stack, ElemType e){    StackNode *s = malloc(sizeof(StackNode));    if(s == NULL)    {        printf("Apply StackNode Failed!");        return FALSE;    }    //给新申请的结点赋值    s->data = e;    s->next = stack->top;    //更新链栈指针    stack->top = s;    stack->count++;    return OK;}/*************************************************Function:       PopDescription:    出栈Input:          链栈指针 LinkStack *stackOutput:Return:         成功返回数据元素,否则程序退出Others:         不要忘记释放出栈结点的空间*************************************************/ElemType Pop(LinkStack *stack){    //判断是否为空栈    if(stack->count == 0)    {        printf("The LinkStack is empty!");        exit(EXIT_FAILURE);    }    //保存栈顶指针    StackNode *s = stack->top;    //保存返回值    ElemType e = s->data;    //更新链栈指针    stack->top = s->next;    stack->count--;    //释放结点指针    free(s);    return e;}/*************************************************Function:       GetTopDescription:    取栈顶元素Input:          链栈指针 LinkStack *stackOutput:Return:         成功返回数据元素,否则程序退出Others:*************************************************/ElemType GetTop(LinkStack *stack){    //判断是否为空栈    if(stack->top == 0)    {        printf("The stack is empty!");        exit(EXIT_FAILURE);    }    return stack->top->data;}/*************************************************Function:       ClearStackDescription:    把栈置空Input:          链栈指针 LinkStack *stackOutput:Return:         成功返回OKOthers:*************************************************/State ClearStack(LinkStack *stack ){    StackNode *p,*q;    //p指向栈顶    p = stack->top;    while(p)    {        q = p;        p = p->next;        free(q);    }    stack->count = 0;    return OK;}/*************************************************Function:       GetLengthDescription:    取得栈的长度Input:          链栈指针 LinkStack *stackOutput:Return:         返回栈的长度Others:*************************************************/int GetLength(LinkStack *stack){    return stack->count;}