-->
当前位置:首页 > 题库 > 正文内容

函数题:反向输出整数序列

Luz3年前 (2022-03-29)题库709
### 题目描述
输入一个整数序列(非负整数,只含 正整数 和 0 )。序列以 -1 结束。要求反向输出这个非负整数序列。

要求定义一个栈来实现这个程序的功能。

### 输入
一个整数序列,每个数之间以空格隔开,非负整数,只含 正整数 和 0 。输入-1 表示输入结束。

### 输出
反向输出这个非负整数序列。

### 提示:

1、在C语言程序中使用 bool 类型,必须包含头文件 stdbool.h

2、题目要求创建栈时,栈空间有16个存储单元。但是服务器测试数据有超过50万个数据需要输入。因此,要求Push操作检查当前存储空间是否已满。若当前存储空间已满,则需要把容量(capacity)扩大为原来容量的2倍。

3、在Pop操作中,也要求检查容量。若弹出1个元素后,已用空间只有容量的三分之一,把容量减少为原来容量的一半。

### 函数接口定义:
c++
栈的定义如下:

typedef int ElemType;

struct StackRecord;
typedef struct StackRecord *Stack;

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


写出 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);//清空分配给栈的存储空间


### 裁判测试程序样例:
c++
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int ElemType;

struct StackRecord;
typedef struct StackRecord *Stack;

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

Stack createStack();
void push(Stack pStack, ElemType x);
void pop(Stack pStack);
ElemType top(Stack pStack);
bool empty(Stack pStack);
void destroy(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);
}
destroy(pStack);
return 0;
}
/* 请在这里填写答案 */


### 输入样例:

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

in
3 127 64 1991 -1


### 输出样例:

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

out
1991 64 127 3







答案:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。