小林Code 2020-03-27 12:43 采纳率: 0%
浏览 2348

关于栈的反向输出整数序列。

输入一个整数序列(非负整数,只含 正整数 和 0 )。序列以-1 结束。要求反向输出这个正整数序列。 要求定义一个栈来实现这个程序的功能。 输入 一个整数序列,每个数之间以空格隔开,非负整数,只含 正整数 和 0 。-1 表示输入结束。 输出 反向输出输入文件中的整数序列。 提示: 在C语言程序中使用 bool 类型,必须包含头文件 stdbool.h

函数接口定义:
栈的定义如下:

typedef int ElemType;

typedef struct
{
ElemType *base; //指向栈的元素存储空间
int top; // 栈顶
int capacity; // 当前已分配空间,以元素为单位
}Stack;

写出 createStack,push, pop,top,empty,destroy函数的定义。函数声明如下:

Stack* createStack(void); //初始化一个空栈。空栈拥有16个元素的空间,栈顶值为 -1

void push(Stack *pStack, ElemType x);//把 x 入栈

ElemType top(Stack *pStack);//返回当前栈顶元素的值

void pop(Stack *pStack); //当前栈顶元素出栈

bool empty(Stack *pStack);//如果栈空,则返回 true,否则返回 false

void destroy(Stack *pStack);//清空分配给栈的存储空间

裁判测试程序样例:
#include
#include
#include

typedef int ElemType;

typedef struct
{
ElemType *base; //指向栈的元素存储空间
int top; // 栈顶
int capacity; // 当前已分配空间,以元素为单位
}Stack;

Stack* createStack();
void push(Stack* pStack, ElemType x);
void pop(Stack* pStack);
ElemType top(Stack* pStack);
bool empty(Stack* pStack);
void destory(Stack* pStack);

int main(void)
{
Stack* pStack;
pStack = createStack();
int x;
scanf("%d", &x);
while (x != -1)
{
push(pStack, x);
scanf("%d", &x);
}
while (!empty(pStack))
{
x = top(pStack);
printf("%d ", x);
pop(pStack);
}
destory(pStack);
return 0;
}

/* 请在这里填写答案 */

输入样例:
在这里给出一组输入。例如:

3 127 64 1991 -1

输出样例:
在这里给出相应的输出。例如:

1991 64 127 3

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-03-27 13:02
    关注

    问题解决的话,请点采纳

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef int ElemType;
    #define N 100
    
    
    typedef struct
    {
        ElemType *base; //指向栈的元素存储空间
        int top; // 栈顶
        int capacity; // 当前已分配空间,以元素为单位
    }Stack;
    
    Stack* createStack();
    void push(Stack* pStack, ElemType x);
    void pop(Stack* pStack);
    ElemType top(Stack* pStack);
    bool empty(Stack* pStack);
    void destory(Stack* pStack);
    
    Stack* createStack(void)
    {
        Stack * s = (Stack *)malloc(sizeof(Stack));
        s->capacity = N;
        s->top = 0;
        s->base = (ElemType *)malloc(sizeof(ElemType) * s->capacity);
        return s;
    }
    
    void push(Stack *pStack, ElemType x)
    {
        pStack->base[pStack->top++] = x;
    }
    ElemType top(Stack *pStack)
    {
        return pStack->base[pStack->top - 1];
    }
    void pop(Stack *pStack)
    {
        pStack->top--;
    }
    bool empty(Stack *pStack)
    {
        return pStack->top == 0;
    }
    void destory(Stack *pStack)
    {
        free(pStack->base);
    }
    
    int main(void)
    {
        Stack* pStack;
        pStack = createStack();
        int x;
        scanf("%d", &x);
        while (x != -1)
        {
            push(pStack, x);
            scanf("%d", &x);
        }
        while (!empty(pStack))
        {
            x = top(pStack);
            printf("%d ", x);
            pop(pStack);
        }
        destory(pStack);
        return 0;
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题