C语言 栈的基本操作 栈的实现

#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 10
#define OK 1
#define ERROR -1
#define STACKINCREMENT 10/*存储空间分配增量*/
typedef int ElemType;
typedef int Status;

栈的结构体

typedef struct SqStack
{
    int top;
    int bottom;
    ElemType array[STACK_SIZE];
}SqStack;

栈的初始化

Status Init_Stack(SqStack *S)
{
    S->top = S->bottom =0;

    return OK;
}

入栈

Status Push(SqStack *S, ElemType e)//e为待压入的元素
{
    int a = 0;

    if(S->top - S->bottom == STACK_SIZE - 1)
    {
        printf("This stack is full!\n\n");
        a = ERROR;
    }
    else
    {
        S->array[S->top] = e;
        S->top++;
        a = OK;
    }
    return a;
}

出栈

Status Pop(SqStack *S)
    int e;
    if(S->bottom == S->top)
        printf("This stack is empty!\n\n");
    e = S->array[(S->top) - 1];
    S->top--;
    return e;
}

获取栈顶元素

Status GetTop(SqStack *S)
{
    int e;

    if(S->bottom == S->top)
    {
        printf("The stack is empty!\n\n");
    }
    else
    {
        S->top--;
        e = S->array[S->top];
        S->top++;
        S->array[S->top] = e;
    }
    return e;
}

遍历栈

Status StackLength(SqStack *S)
{
    int i;
    int len = 0;

    if(S->top == S->bottom)
        printf("This stack is empty!\n\n");
    else
    {
        for(i = 0; i < S->top; i++)
            len++;
    }
    return len;
}

清空栈

Status ClearStack(SqStack *S)
{
    S->top = S->bottom;
    return OK;
}

输出栈

void OutPut_Stack(SqStack *S)
{
    int i;
    int len = 0;
    int j = 0;

    if(S->top == 0)
        printf("This stack is empty!\n\n");
    for(i = 0; i < S->top; i++)
        len++;

    j = len;
    for(i = len - 1; i >= 0; i--)
    {
        printf("第%d个元素为:%d\n\n",j ,S->array[i]);
        j--;
    }
}

在主函数中调用以上函数

int main()
{
    int i;
    int a;
    int j = 0;
    SqStack S;
    int res1 = 0;//初始化栈的返回值
    int res2 = 0;//压入栈的返回值
    int res3 = 0;//获取栈顶元素的返回值
    int res4 = -1;//弹出栈顶元素的返回值
    int res6 = 0;//清除栈的返回值

    printf("1.初始化一个栈\n2.压入一个元素\n3.弹出栈顶元素\n4.获取栈的长度\n5.输出站\n6.获取栈顶元素\n7.清空栈\n");

    while(1)
    {
        printf("请输入操作编号:");
        scanf("%d", &i);
        printf("\n");

        if(i == 1)
        {
            //初始化一个栈
            res1 = Init_Stack(&S);
            if(res1 == 1)
                printf("初始化成功\n\n");
            else
                printf("初始化失败\n\n");
            res2 = 0;
            j = 1;
        }
        else if(i == 2)
        {
            //压入元素
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("请插入数据:");
                scanf("%d", &a);
                printf("\n");
                res2 = Push(&S, a);
                if(res2 == 1)
                    printf("元素压入成功\n\n");
                else
                    printf("元素压入失败\n\n");
                res2 = 0;
            }
        }
        else if(i == 3)
        {
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("\n-----------弹出栈顶元素-----------\n\n");
                res4 = Pop(&S);
                if(res4 != -1)
                {
                    printf("栈顶元素弹出成功\n");
                    printf("栈顶元素为:%d\n", res4);
                }
            }

        }
        else if(i == 4)
        {
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("\n-----------获取栈的长度-----------\n\n");
                StackLength(&S);
            }
        }
        else if(i == 5)
        {
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("\n-----------输出栈-----------\n\n");
                OutPut_Stack(&S);
            }
        }
        else if(i == 6)
        {
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("\n-----------获取栈顶元素-----------\n\n");
                res3 = GetTop(&S);
                printf("栈顶元素为:%d\n", res3);
            }
        }
        else if(i == 7)
        {
            if(j == 0)
            {
                printf("Error!\n");
                printf("Please initialize a stack first!\n");
            }
            else
            {
                printf("\n-----------清空栈-----------\n\n");
                //清空栈
                res6 = ClearStack(&S);
                if(res6 == OK)
                    printf("清除成功\n");
                else
                    printf("清除失败\n");
            }

        }
        else
        {
            printf("操作编码错误\n请重新输入\n");
        }
    }

    return 0;
}

以上就是栈的操作的基本实现,希望能帮到大家

  • 32
    点赞
  • 153
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值