函数题:反向输出整数序列
输入一个整数序列(非负整数,只含 正整数 和 0 )。序列以-1 结束。要求反向输出这个正整数序列。
要求定义一个栈来实现这个程序的功能。
输入
一个整数序列,每个数之间以空格隔开,非负整数,只含 正整数 和 0 。-1 表示输入结束。
输出
反向输出输入文件中的整数序列。
提示:
在C语言程序中使用 bool 类型,必须包含头文件 stdbool.h
### 函数接口定义:
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
答案:若无答案欢迎评论
要求定义一个栈来实现这个程序的功能。
输入
一个整数序列,每个数之间以空格隔开,非负整数,只含 正整数 和 0 。-1 表示输入结束。
输出
反向输出输入文件中的整数序列。
提示:
在C语言程序中使用 bool 类型,必须包含头文件 stdbool.h
### 函数接口定义:
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
答案:若无答案欢迎评论