C实现顺序栈各功能

VS调试的程序,scanf_s会报错。代码加上#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define MAXSIZE 100
#define _CRT_SECURE_NO_WARNINGS 1

typedef int ElemType;
typedef struct
{
    ElemType* base;
    ElemType* top;
    int Length;
} SequenceStack;
//定义栈

int InitStack(SequenceStack* ps)
{
    ps->base = (int*)malloc(MAXSIZE * sizeof(int));
    if (!ps->base)
        return ERROR;
    ps->top = ps->base;
    ps->Length = MAXSIZE;
    return OK;
}
//初始化栈

int EmptyStack(SequenceStack* ps)
{
    if (ps->base == ps->top)
    {
        return OK;
    }
    else
    {
        return ERROR;
    }
}//判断栈是否为空

int GetHead(SequenceStack ps, ElemType* e)
{
    if (ps.base == ps.top)
    {
        return ERROR;
    }
    else
    {
        e = --ps.top;
        return *e;
    }
}//获取栈顶元素

int StackLength(SequenceStack ps, ElemType* len)
{
    if (ps.base == ps.top)
    {
        *len = 0;
        return OK;
    }
    else
    {
        *len = 1;
        while (--ps.top != ps.base)
        {
            *len = *len + 1;
            printf("----");
            printf("\n");
        }
        return OK;
    }
}//获取栈的长度

int StackPop(SequenceStack* ps)
{

    if (ps->top == ps->base)
    {
        return ERROR;
    }
    int e = 0;

    while (--ps->top != ps->base || ps->top == ps->base)
    {

        e = *ps->top;
        printf("%d\n", e);
        if (ps->top == ps->base)
            return OK;
    }
}//将栈中元素依次出栈并输出

int Push(SequenceStack* ps, ElemType e)
{
    if (ps->top - ps->base == MAXSIZE)
    {
        return ERROR;
    }
    *ps->top++ = e;

    return OK;
}

int Pop(SequenceStack* ps)
{
    if (*ps->base == *ps->top)
    {
        return ERROR;
    }
    else
    {
        --ps->top;
    }
    return OK;
}



int ten_eight(SequenceStack* ps, ElemType n)
{
    int t, i = 1;
    t = n % 8;
    *ps->top++ = t;
    n = n / 8;
    while (n > 8)
    {
        if (n > 8)
        {
            t = n % 8;
            *ps->top++ = t;
            i++;
            n = n / 8;
        }
        else
        {
            break;
        }
    }
    *ps->top++ = n;
    while (i >= 0)
    {
        printf("%d", *--ps->top);
        i--;
    }
    printf("\n");
    return OK;
}//进制转化

int main()
{
    SequenceStack stack;
    int status, head, temp, len = 0, num, e,ten;
    InitStack(&stack);

    while (1)
    {
        printf("%-18s\n", "************************");
        printf("%-18s", "0、输入零退出程序。");
        printf("%5s\n", "*");
        printf("%-18s", "1、判断栈是否为空。");
        printf("%5s\n", "*");
        printf("%-18s", "2、获取栈的头元素。");
        printf("%5s\n", "*");
        printf("%-18s", "3、获取此栈的长度。");
        printf("%5s\n", "*");
        printf("%-18s", "4、栈元素依次弹出。");
        printf("%5s\n", "*");
        printf("%-18s", "5、实现十转八进制。");
        printf("%5s\n", "*");
        printf("%-18s", "6、将元素压入栈中。");
        printf("%5s\n", "*");
        printf("%-18s", "7、将元素弹出此栈。");
        printf("%5s\n", "*");
        printf("%-18s\n", "************************");

        printf("你的选择:");
        scanf("%d", &num);
        printf("\n");
        if (num == 0)
        {
            break;
        }
        else if (num == 1)
        {
            if (EmptyStack(&stack))
            {
                printf("当前栈为空!\n");
                continue;
            }
            else
            {
                printf("当前栈不为空!\n");
                continue;
            }
        }
        else if (num == 2)
        {
            head = GetHead(stack, &temp);
            if (head)
            {
                printf("当前头元素为:%d\n", head);
                continue;
            }
            else
            {
                printf("当前栈为空!\n");
                continue;
            }
        }
        else if (num == 3)
        {
            StackLength(stack, &len);
            if (len)
            {
                printf("当前栈的长度为%d\n", len);
                continue;
            }
            else
            {
                printf("当前栈为空!\n");
                continue;
            }
        }
        else if (num == 4) {
            StackPop(&stack);
            continue;
        }
        else if (num == 5)
        {
            scanf("%d", &ten);
            ten_eight(&stack,ten);
            continue;
        }
        else if (num == 6)
        {
            printf("你要压入栈中的数:");
            scanf("%d", &e);
            if (Push(&stack, e))
            {
                printf("入栈成功!\n");
                continue;
            }
            else
            {
                printf("当前栈满!\n");
                continue;
            }
        }
        else if (num == 7)
        {
            if (Pop(&stack))
            {
                printf("出栈成功!\n");
                continue;
            }
            else
            {
                printf("当前栈为空!\n");
                continue;
            }
        }
    }
}

作者:qianyuzz

出处:https://www.cnblogs.com/qianyuzz/p/17059912.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   qianyuzz  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示