题目描述
利用栈的基本运算,编写一个算法输入若干整数,以0标识输入结束。然后按与输入相反次序输出这些整数。
输入
输入:
1 2 3 4 5 0
输出
输出:
5 4 3 2 1
样例输入
6 7 8 9 10 0
样例输出
10 9 8 7 6
提示
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct
{
int data[105];
int top;
}zhan;
void InitStack(zhan *&s)
{
s=(zhan*)malloc(sizeof(zhan));
memset(s->data,0,sizeof(zhan));
s->top=-1;
}
void Push(zhan *&s,int x)
{
s->top++;
s->data[s->top]=x;
}
void tuizhan(zhan *&s)
{
cout<<s->data[s->top]<<" ";
s->top--;
}
int zhanEmpty(zhan *s)
{
return s->top==-1;
}
int main()
{
zhan *qwe;
InitStack(qwe);
int q;
while(cin>>q)
{
if(q==0)
break;
Push(qwe,q);
}
while(!zhanEmpty(qwe))
{
tuizhan(qwe);
}
cout<<endl;
}
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct
{
int data[105];
int top;
}zhan;
void InitStack(zhan *&s)
{
s=(zhan*)malloc(sizeof(zhan));
memset(s->data,0,sizeof(zhan));
s->top=-1;
}
void Push(zhan *&s,int x)
{
s->top++;
s->data[s->top]=x;
}
void tuizhan(zhan *&s)
{
cout<<s->data[s->top]<<" ";
s->top--;
}
int zhanEmpty(zhan *s)
{
return s->top==-1;
}
int main()
{
zhan *qwe;
InitStack(qwe);
int q;
while(cin>>q)
{
if(q==0)
break;
Push(qwe,q);
}
while(!zhanEmpty(qwe))
{
tuizhan(qwe);
}
cout<<endl;
}