数据结构——栈

数组实现

#include<stdio.h>
#include<stdlib.h>
#define EmptyTOS -1
#define MinStackSize 5
#ifndef _Stack_h//stack头文件

struct StackRecord;
typedef struct StackRecord *Stack;
typedef int ElementType;

int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int MaxElement);
void DisposeStack(Stack S);//删除栈
void MakeEmpty(Stack S);//清空栈
void Push(ElementType X,Stack S);
ElementType Top(Stack S);
void Pop(Stack S);
ElementType TopandPop(Stack S);

#endif

struct StackRecord
{
    int Capacity;//容量
    int TopOfStack;//栈顶元素下标
    ElementType *Array;
};

int IsEmpty(Stack S)
{
    return S->TopOfStack==EmptyTOS;
}

int IsFull(Stack S)
{
    return S->TopOfStack==S->Capacity-1;
}

Stack CreateStack(int MaxElement)
{
    Stack S;
    if(MaxElement<MinStackSize)
    {
        printf("Stack size is too small\n");
    }
    S=(struct StackRecord*)malloc(sizeof(struct StackRecord));
    if(S==NULL)printf("Out of space!\n");
    S->Array=(ElementType*)malloc(sizeof(ElementType)*MaxElement);
    if(S->Array==NULL)printf("Out of space!\n");
    S->Capacity=MaxElement;
    MakeEmpty(S);
    return S;
}

void DisposeStack(Stack S)
{
    if(S!=NULL)
    {
        free(S->Array);
        free(S);
    }
}

void MakeEmpty(Stack S)
{
    S->TopOfStack=EmptyTOS;
}

void Push(ElementType X,Stack S)
{
    if(IsFull(S))printf("Full Stack!\n");
    else S->Array[++S->TopOfStack]=X;
}

ElementType Top(Stack S)
{
    if(!IsEmpty(S))
    {
        return S->Array[S->TopOfStack];
    }
    printf("Empty Stack!\n");
    return 0;
}

void Pop(Stack S)
{
    if(IsEmpty(S))printf("Empty stack!\n");
    else S->TopOfStack--;
}

ElementType TopandPop(Stack S)
{
    if(!IsEmpty(S))
    {
        return S->Array[S->TopOfStack--];
    }
    printf("Empty Stack!\n");
    return 0;
}

int main()
{
    system("pause");
    return 0;
} 

链表实现

#include<stdio.h>
#include<stdlib.h>
#ifndef _Stack_h//stack头文件

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
typedef int ElementType;

int IsEmpty(Stack S);//判断栈是否已空
Stack CreateStack();//创建栈
void MakeEmpty(Stack S);//清空栈
void DisposeStack(Stack S);//删除栈
void Push(ElementType X,Stack S);//压入
ElementType Top(Stack S);//获取栈顶元素
void Pop(Stack S);//弹出栈顶元素
void PrintStack(Stack S);//输出栈

#endif

struct Node
{
    ElementType Element;
    PtrToNode Next;
};

int IsEmpty(Stack S)
{
    return S->Next==NULL;
}

void MakeEmpty(Stack S)
{
    if(S==NULL)
    {
        printf("Must use createStack first!\n");
    }
    else 
    {
        while(!IsEmpty(S))
        {
            Pop(S);
        }
    }
}

Stack CreateStack()
{
    Stack S;
    S=(struct Node*)malloc(sizeof(struct Node));
    if(S==NULL)
    {
        printf("out of space!\n");
    }
    S->Next=NULL;
    MakeEmpty(S);
    return S;
}

void DisposeStack(Stack S)
{
    MakeEmpty(S);
    free(S);
    return;
}

void Push(ElementType X,Stack S)
{
    PtrToNode Tmpcell;
    Tmpcell=(struct Node*)malloc(sizeof(struct Node));
    if(Tmpcell==NULL)
    {
        printf("out of space!\n");
    }
    else
    {
        //插入链表头
        Tmpcell->Element=X;
        Tmpcell->Next=S->Next;
        S->Next=Tmpcell;
    }
}

ElementType Top(Stack S)
{
    if(!IsEmpty(S))
    return S->Next->Element;
    else printf("Empty Stack\n");
    return 0;
}

void Pop(Stack S)
{
    PtrToNode Firstcell;
    if(IsEmpty(S))
    {
        printf("Empty Stack\n");
    }
    else
    {
        Firstcell=S->Next;
        S->Next=S->Next->Next;
        free(Firstcell);
    }
}

void PrintStack(Stack S)
{
    while(S->Next!=NULL)
    {
        printf("%d ",S->Next->Element);
        S->Next=S->Next->Next;
    }
}

int main()
{
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青云遮夜雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值