数组模拟堆栈的入栈与出栈
作者:互联网
点击查看代码
package com.javastudy.example05;
import java.util.Scanner;
public class ArrayWork {
public static void main(String[] args) {
Object[] stack={"123","234","345","hzx",123};
System.out.println("请输入要操作的次数");
Scanner m=new Scanner(System.in);
int n=m.nextInt();
for(int i=1;i<=n;i++){
Scanner s=new Scanner(System.in);
System.out.println("请输入压栈元素");
int s1=s.nextInt();
push(s1, stack);
}
System.out.println("进行出栈-----------");
pop(stack);
}
public static void push(int e,Object []array){
if(array[4].equals(0)){
for(int i=0;i<array.length;i++){
if(array[i].equals(0)){
array[i]=e;
System.out.println(e+"已压栈");
break; } }
}
else {System.out.println("栈满");
} }
public static void pop(Object []array){
if(array[0].equals(0)){
System.out.println("栈空、无法出栈");
}
else {
for(int i=array.length-1;i>=0;i--){
if(!array[i].equals(0)){
System.out.println(array[i]+"已出栈");
array[i]=0;
}
}
}
}
}
标签:出栈,Scanner,int,System,堆栈,array,out,public,入栈 来源: https://www.cnblogs.com/hzxiang/p/16484592.html