您的位置:首页 > 其它

2016-01-28 23:47 218 查看
package luyunzhou.one;

import java.util.Iterator;
import java.util.Scanner;

public class StackDemo {

public static void main(String[] args) {
stack <String>st = new stack<String>();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
st.push(scanner.next());
}
System.out.println(st.peak());
for (int i = 0; i < 5; i++) {
System.out.print(st.pop()+" ");
}

//      while(!st.IsEmpty()){
//          System.out.println(st.pop());
//      }
}

}

class stack<Item>{
private Node first;
private int N;
private class Node{
Item item;
Node next;
}
public boolean IsEmpty() {
return first == null;
}
public int size(){
return N;
}
public Item peak(){
return first.item;
}
public void push(Item item) {
Node oldfirst = first;
first = new Node();
first.item = item;
first.next=oldfirst;
N++;
}
public Item pop(){
if(N == 0){
return null;
}
Item item = first.item;
first = first.next;
N--;
return item;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: