Stack 栈

栈(First-in,Last-out)
StackX.java

package test.stack;

public class StackX {

private int maxSize;
private int top;
private String[] stackArr ;

public StackX(int size) {
maxSize = size;
stackArr = new String[maxSize];
top = -1;
}

public void push(String j){
stackArr[++top] = j;
}

public String pop(){
return stackArr[top--];
}

public String seek(){
return stackArr[top];
}

public boolean isEmpty(){
return (top == -1);
}

public boolean isFull(){
return (top ==(maxSize-1));
}

}



StackApp.java

package test.stack;

public class StackApp {

/**
* @param args
*/
public static void main(String[] args) {

// StackX stack = new StackX(3);
// stack.push("a");
// stack.push("b");
// stack.push("c");
// while(!stack.isEmpty()){
// System.out.println(stack.pop());
// }
//
String []arr = {"a","b","c"};
StackX stack = new StackX(arr.length);
for(int i=0; i<arr.length; i++){
stack.push(arr[i]);
}
while(!stack.isEmpty()){
System.out.println(stack.pop());
}

String hello ="Hello World";

String[] res = hello.split(" ");
// for(int i=0; i<res.length; i++){
// System.out.println(res[i]);
// }
StackX stack2 = new StackX(res.length);

for(int i=0;i<res.length;i++){
stack2.push(res[i]);
}
while(!stack2.isEmpty()){
System.out.println(stack2.pop());

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值