0
#include<stdio.h>
int tp=-1;
void push(int arr[],int value)
{
arr[++tp]=value;
}
void pop(int arr[])
{
if(size()==0)
{
puts("-1");
return;
}
printf("%d\n",arr[tp--]);
}
int size()
{
return tp+1;
}
void empty()
{
if(size()==0)puts("1");
else puts("0");
}
int top(int arr[])
{
if(size()==0)
{
puts("-1");
return;
}
printf("%d\n",arr[tp]);
}
int main()
{
int arr[10000];
unsigned int i,repeat;
char command[6];
scanf("%d",&repeat); //repeating
for(i=0;i<repeat;i++)
{
scanf("%s",command);
switch(command[0])
{
case 'p':
if(command[1]=='u') //push
{
int value;
scanf("%d",&value);
push(arr,value);
}
else pop(arr); //pop. if stack is empty, output -1
break;
case 's':
printf("%d\n",size()); //print size of stack
break;
case 'e':
empty(); //if stack is empty, print 1. if not, print 0.
break;
case 't':
top(arr); //print value that is on top of stack. if stack is empty, print -1
break;
}
}
我想使此代码使用较少的内存... 这个代码使用1116KB, 但相同的算法代码使用1000KB。 我怎样才能让这段代码使用更少的内存?
这个代码是这样工作的 -
此代码具有5个命令:
1.push X:在堆叠中添加X
2.pop:从栈和打印移除项它。
3.size:打印
4.empty堆叠的元件的数量:堆栈是否为空的,打印1.如果不是打印0
5.top:打印是项在堆栈的顶部
步骤
输入值(repeat循环量)
输入命令
利润!