PTA-反向输出整数序列

输入一个整数序列(非负整数,只含 正整数 和 0 )。序列以 -1 结束。要求反向输出这个非负整数序列。

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

输入

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

输出

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

提示:

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

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

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

函数接口定义:

栈的定义如下: 
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);//清空分配给栈的存储空间

裁判测试程序样例:

#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; 
} 
/* 请在这里填写答案 */

输入样例:

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

3 127 64 1991 -1

输出样例:

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

1991 64 127 3 

分析:

  1. createStack():

    • 函数用于创建一个新的栈。
    • 首先,通过malloc动态分配了一个Stack结构体的内存空间。
    • 然后,为栈的底层存储空间分配内存,容量为capacity
    • 设置栈的初始为空(top = 0)。
    • 返回新创建的栈的指针。
  2. destroy(Stack pStack):

    • 函数用于销毁一个栈。
    • 释放了栈的底层存储空间的内存。
    • 注意:这里只是释放了底层空间的内存,并没有释放Stack结构体的内存,所以会造成内存泄漏。在实际使用中,通常需要一次性释放整个栈的内存。
  3. push(Stack pStack, int x):

    • 函数用于将一个整数x压入给定的栈中。
    • 首先检查栈是否已满,如果已满则返回0。
    • 否则,将x存储在栈顶的位置,并将栈顶指针top自增。
  4. pop(Stack pStack):

    • 函数用于从给定的栈中弹出一个元素。
    • 首先检查栈是否为空,如果为空则返回0。
    • 否则,将栈顶指针top自减,并返回栈顶的元素。
  5. top(Stack pStack):

    • 函数用于返回给定栈顶的元素。
    • 返回栈顶元素,即pStack->base[pStack->top - 1]
  6. empty(Stack pStack):

    • 函数用于检查给定的栈是否为空。
    • 如果栈顶指针top等于0,则返回true,表示栈为空。

C语言:

Stack createStack()
{  
    Stack  S = (Stack )malloc(sizeof(Stack));
    S->capacity = 1000000000;
    S->base = (int *)malloc(sizeof(int) * S->capacity);
	S->top = 0;
	 
    

    return S;
}
 void destroy(Stack pStack)
{
    free(pStack->base);
}

void push(Stack pStack, int x) 
{
	if(pStack->top==pStack->capacity)
	{ 
		return 0;
	}
    pStack->base[pStack->top++] = x;

}

void pop(Stack pStack) 
{
	if(pStack->top==0)
	{ 
		return 0;
	}
    pStack->top--;

}

int top(Stack pStack)  
{

    return pStack->base[pStack->top - 1];
}



bool empty(Stack pStack)
{
    return pStack->top == 0;
}

总结:

整体来看,这段代码实现了一个动态分配的栈,并提供了基本的栈操作功能,如压入、弹出、查看栈顶元素和检查栈是否为空等。需要注意的是,在销毁栈的时候,只释放了底层存储空间的内存,而没有释放Stack结构体的内存,这在某些情况下可能会导致内存泄漏。

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值