[蓝蓝计算机考研算法训练二期]-day05
8、逆序输出链表。
输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。
如输入{1,2,3}的链表如下图:
返回一个数组为[3,2,1]
0 <= 链表长度 <= 10000
示例1
输入:
{1,2,3}
返回值:
[3,2,1]
示例2
输入:
{67,0,24,58}
返回值:
[58,24,0,67]
思路
看到球友发出的文章可以用栈,然后再根据上网查大佬们的代码进行学习和分析,自己其实不是很会写qwq,有借鉴别人的代码
具体实现
#include<stdio.h>
#include<string.h>
#define MaxSize 10000
typedef struct SqStack{
char data[MaxSize];
int top;
};
void InitStack(SqStack &S){
S.top=-1;
}
bool Push(SqStack &S,char e){
if(S.top ==MaxSize-1)
return false;
if(e==123){
e=93;
}
if(e==125){
e=91;
}
S.data[++S.top ]=e;
return true;
}
bool Pop(SqStack &S){
if(S.top ==-1)
return false;
printf("%c",S.data[--S.top ]);
}
int main()
{
SqStack S;
char str[MaxSize];
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<len+1;i++){
Push(S,str[i]);
}
for(int i=0;i<len;i++){
Pop(S);
}
}
(只实现了个位数的转换……