6-5 链栈的基本操作(10 分)PTA无法评测但是感觉写的符合要求

本文介绍了一种使用链栈实现基本栈操作的方法,包括入栈、出栈、判断空栈、获取栈长度及打印栈内元素等核心功能,并提供了一个完整的示例程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

6-5 链栈的基本操作(10 分)
S是一个顺序栈,函数Push_L(LinkStack &S, SElemType e)是在链栈中插入一个元素e,函数Pop_L(LinkStack &S, SElemType &e)是删除链栈的栈顶元素(进行此操作是保证栈里有元素),函数StackEmpty_L(LinkStack &S)是判断栈是否为空,函数StackLength_L(LinkStack &S)返回栈内元素的个数,函数PrintStack_L(LinkStack S);是从栈顶到栈底依次属于元素的值。数据保证合法。。
函数接口定义:

void Push_L(LinkStack &S, SElemType e);
void Pop_L(LinkStack &S, SElemType &e);
bool StackEmpty_L(LinkStack &S);
int StackLength_L(LinkStack &S);
void PrintStack_L(LinkStack S);
其中 S 是链栈,e 元素。
裁判测试程序样例:

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

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int Status;
typedef int SElemType;

typedef struct SNode {
    SElemType data;
    struct SNode * next;
}SNode, *LinkStack;

void Push_L(LinkStack &S, SElemType e);
void Pop_L(LinkStack &S, SElemType &e);
bool StackEmpty_L(LinkStack &S);
int StackLength_L(LinkStack &S);
void PrintStack_L(LinkStack S);

Status InitStack_L(LinkStack &S) {
    S = (LinkStack) malloc (sizeof(SNode));
    if (S == NULL)
        return OVERFLOW;
    S->next = NULL;
    return OK;
}

int main() {
    LinkStack S;

    if(InitStack_L(S) != OK) {
    printf("InitStack_L: 初始化失败!!!\n");
        return -1;
    }

    for(int i = 1; i <= 5; ++ i) 
        Push_L(S, i);

    int OperationNumber;
    scanf("%d", &OperationNumber);

    while(OperationNumber --) {
        int type;
        scanf("%d", &type);

        if(type == 1) {
            int elem;
            scanf("%d", &elem);
            Push_L(S, elem);
        } else if(type == 2) {
            SElemType elem;
            Pop_L(S, elem);
            printf("%d\n", elem);
        } else if(type == 3) {
            if(StackEmpty_L(S)) puts("Empty!");
            else puts("Not Empty");
        } else if(type == 4) {
            int len = StackLength_L(S);
            printf("%d\n", len);
        } else {
            PrintStack_L(S);
        }
    }

    return 0;
}

/* 你的代码将被嵌在这里 */

输入格式:

第一行输入一个整数OperationNumber,表示操作数,接下来OperationNumber行,每行表示一个操作(表示为“操作编号 操作内容”)。 编号为1表示入栈操作,后面一个参数表示插入的元素值 编号为2表示出栈操作 编号为3表示判断栈是否为空 编号为4表示查看栈内元素个数 编号为5表示从栈顶到栈底依次输出栈内元素
输出格式:

对于操作2,输出出栈的元素的值 对于操作3,输出”Empty!”或”Not Empty” 对于操作4,输出栈内元素个数 对于操作5,从栈顶到栈底依次输出栈内元素
输入样例:

6
1 2
1 3
2
3
4
5
输出样例:

3
Not Empty
6
2 5 4 3 2 1

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

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int Status;
typedef int SElemType;

typedef struct SNode {
    SElemType data;
    struct SNode * next;
}SNode, *LinkStack;

/*
S是一个顺序栈
函数Push_L(LinkStack &S, SElemType e)是在链栈中插入一个元素e
函数Pop_L(LinkStack &S, SElemType &e)是删除链栈的栈顶元素(进行此操作是保证栈里有'

元素
函数StackEmpty_L(LinkStack &S)是判断栈是否为空
函数StackLength_L(LinkStack &S)返回栈内元素的个数
函数PrintStack_L(LinkStack S)是从栈顶到栈底依次属于元素的值。数据保证合法。。
编号为1表示入栈操作,后面一个参数表示插入的元素值 
编号为2表示出栈操作 
编号为3表示判断栈是否为空 
编号为4表示查看栈内元素个数 
编号为5表示从栈顶到栈底依次输出栈内元素
*/
void Push_L(LinkStack &S, SElemType e)
{
    LinkStack p = (struct SNode*)malloc(sizeof(struct SNode));
    p->data = e;
    p->next = S->next;
    S->next = p;
}
void Pop_L(LinkStack &S, SElemType &e)
{
        LinkStack p;
        p = S->next;
        int num = (p->data);
        S->next = p->next;
        free(p);
        e = num;
}
bool StackEmpty_L(LinkStack &S)
{
    if (S->next == NULL)
        return 1;
    return 0;
}
int StackLength_L(LinkStack &S)
{
    int cnt = 0;
    LinkStack p = S;//不能直接访问
    while (p->next)
    {
        cnt++;
        p =p->next;
    }
    return cnt;
}
void PrintStack_L(LinkStack S)
{
    S = S->next;
    while (S)
    {
        printf("%d ", S->data);
        S = S->next;
    }
    puts("");
}

Status InitStack_L(LinkStack &S) 
{
    S = (LinkStack)malloc(sizeof(SNode));
    if (S == NULL)
        return OVERFLOW;
    S->next = NULL;
    return OK;
}

int main() {
    LinkStack S;

    if (InitStack_L(S) != OK) {
        printf("InitStack_L: 初始化失败!!!\n");
        return -1;
    }

    for (int i = 1; i <= 5; ++i)
        Push_L(S, i);

    int OperationNumber;
    scanf("%d", &OperationNumber);

    while (OperationNumber--) {
        int type;
        scanf("%d", &type);

        if (type == 1) {
            int elem;
            scanf("%d", &elem);
            Push_L(S, elem);
        }
        else if (type == 2) {
            SElemType elem;
            Pop_L(S, elem);
            printf("%d\n", elem);
        }
        else if (type == 3) {
            if (StackEmpty_L(S)) puts("Empty!");
            else puts("Not Empty");
        }
        else if (type == 4) {
            int len = StackLength_L(S);
            printf("%d\n", len);
        }
        else {
            /*S->next;
            while (S!=NULL)
            {
                printf("*%d\n",S->data);

                S = S->next;
            }*/
            PrintStack_L(S);
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值