For each case, the first line of the input file contains one integer n-length of the sequence (0 < n ≤ 10 000). The second line contains n integers numbers-a1, a2, …, an (-1 000 000 000 000 000 ≤ ai ≤ 1 000 000 000 000 000).
For each case, on the first line of the output file print the sequence in the reverse order.
5 -3 4 6 -8 9
9 -8 6 4 -3
用C语言链栈实现
#include<stdio.h>
#include<stdlib.h>
#define Status int
#define dataType int
typedef struct node {
dataType data;
struct node *next;
}*LinkStack,node;
LinkStack createStack() {
LinkStack p = (node*)malloc(sizeof(node));
p->next = NULL;
return p;
}
//判空
Status isEmpty(LinkStack ls) {
if (!ls->next)
return 1;
else
return 0;
}
//入栈
void push(LinkStack ls, dataType data) {
node* p = (node*)malloc(sizeof(node));
p->data = data;
p->next = ls->next;
ls->next = p;
}
//出栈 需要返回出栈状态 栈为空则失败
Status pop(LinkStack ls) {
if (!isEmpty(ls)) {
node* p = (node*)malloc(sizeof(node));
p = ls->next;
ls->next = ls->next->next;
return 1;
}
return 0;
}
//获取栈顶元素
dataType top(LinkStack ls) {
if (!isEmpty(ls))
return ls->next->data;
return NULL; //栈空则返回null
}
//求栈中元素个数
int size(LinkStack ls) {
int n = 0;
node *p = ls->next;
while (p) {
n++;
p = p->next;
}
return n;
}
int main() {
LinkStack ls = createStack();
int n,t;
while(~scanf("%d",&n)){
while(n--){
scanf("%d",&t);
push(ls,t);
}
while(!isEmpty(ls)){
printf("%d ",top(ls));
pop(ls);
}
printf("\n");
}
}