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;
}
}
栈
最新推荐文章于 2024-05-21 23:18:35 发布