链栈基本操作模拟系统

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef short ElemType;
typedef short Status;
struct StackNode/* 链栈结点 */
{
    ElemType data;
    struct StackNode *next;
};
/* */
Status InitStack(struct StackNode* &s);
Status Push(struct StackNode* &s, ElemType e);
Status Pop(struct StackNode* &s, ElemType &e);
Status GetTop(struct StackNode* &s);
Status GetLength(struct StackNode* &s);
Status MenuPrint();
/* */
int main()
{
    struct StackNode *S;/* 栈顶指针 */
    short choice;
    ElemType e;
    InitStack(S);/* 初始化链栈 */
    MenuPrint();
    printf("Please input your choice: ");
    while(1)
    {
        while(scanf("%hd", &choice) != 1)
        {
            while(getchar() != '\n') ;
            printf("Now the input buffer has been cleaned, please input a number not a character.\n");
            printf("Please input your choice: ");
        }
        if(choice == 5)
        {
            printf("Thanks for your using, see you.\n");
            break;
        }
        switch(choice)
        {
            case 1:
                printf("Please input the value of the element that will push: ");
                scanf("%hd", &e);
                Push(S, e);
                break;
            case 2:
                if(Pop(S, e) == OK)
                {
                    printf("The element that just have popped is %hd.\n", e);
                }
                else
                {
                    printf("The stack is now empty, so there is no element that could pop.\n");
                }
                break;
            case 3:
                e = GetTop(S);
                if(e == ERROR)
                {
                    printf("The stack is now empty, so you can not get the top element.\n");
                }
                else
                {
                    printf("Now the top element in the stack is %hd.\n", e);
                }
                break;
            case 4:
                e = GetLength(S);
                printf("The number of the elements in the stack is now %hd.\n", e);
                break;
            case 5:
                printf("Please input a valid choice from 1 to 5.\n");
                break;
        }
        printf("Please input your choice: ");
    }
}
/* 打印操作选项 */
Status MenuPrint()
{
    printf("---------------------------链栈基本操作模拟系统---------------------------\n");
    printf("---------------------------1. 入栈----------------------------------------\n");
    printf("---------------------------2. 出栈----------------------------------------\n");
    printf("---------------------------3. 取栈顶元素----------------------------------\n");
    printf("---------------------------4. 获取栈内元素个数----------------------------\n");
    printf("---------------------------5. 退出系统------------------------------------\n");
    return OK;
}
/* 获取栈中元素个数 */
Status GetLength(struct StackNode* &s)
{
    short len = 0;
    struct StackNode *p = s;/* 一开始p指向栈顶 */
    while(p != NULL)
    {
        len ++;
        p = p -> next;
    }
    return len;
}
/* 取栈顶元素 */
Status GetTop(struct StackNode* &s)
{
    if(s == NULL)
    {
        /* 栈空 */
        return ERROR;
    }
    return s -> data;
}
/* 出栈 */
Status Pop(struct StackNode* &s, ElemType &e)
{
    struct StackNode *p;
    if(s == NULL)
    {
        /* 栈空 */
        return ERROR;
    }
    e = s -> data;
    p = s;
    s = s -> next;
    free(p);
    return OK;
}
/* 入栈 */
Status Push(struct StackNode* &s, ElemType e)
{
    struct StackNode *p = (struct StackNode*)malloc(sizeof(struct StackNode));
    if(p != NULL)
    {
        p -> data = e;
        p -> next = s;
        s = p;/* 更新栈顶指针 */
        return OK;
    }
    else
    {
        /* 动态内存申请失败 */
        exit(0);
    }
}
/* 初始化链栈 */
Status InitStack(struct StackNode* &s)
{
    /* 链栈的初始化操作就是构造一个空栈, 因为没必要设头结点, 所以直接将栈顶指针置空即可 */
    s = NULL;
    return OK;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

好梦成真Kevin

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

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

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

打赏作者

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

抵扣说明:

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

余额充值