算法五:栈
什么是栈?先进后出/后进先出为栈。与队列相反。就是先来的要排到最后,后来的却可以先走。栈最早是由图灵奖命名来源者图灵发明,最初为解决程序的调用和返回。而栈的应用之一就是递归。
#include <stdio.h>
void stack(int top, int *ranks);
int main(){
int ranks[100] = {4,5,0,6,8,1,3,9,7,2};
//栈顶
int top = 9;
stack(top, ranks);
}
void stack(int top, int *ranks){
char more = '\0';
int new = 0;
while(top >= 0){
printf("----------------%d-------------------\n", ranks[top]);
top--;
printf("Add Element?[y/n] \n");
scanf(" %c", &more);
if(more == 'y'){
printf("New Number:\n");
scanf(" %d", &new);
ranks[++top] = new;
}
}
}