GeezerVison is showing raw.githubusercontent.com — raw.githubusercontent.com
package datastructure;
/**
* 使用Java泛型实现栈
*
* @author liu yuning
*
* @param
*/
public class Stack {
private static class Node {
private U value;
private Node next;
public Node() {
value = null;
next = null;
}
public Node(U value, Node next) {
this.value = value;
this.next = next;
}
/**
* 判断栈是否为空
*
* @return true 如果为空;否则,false
*/
public boolean empty() {
return value == null && next == null;
}
}
/**
* 定义栈顶元素
*/
private Node top = new Node();
/**
* 入栈操作
*
* @param value
* 入栈元素的值
*/
public void push(T value) {
top = new Node(value, top);
}
/**
* 出栈操作
*
* @return 栈顶元素的值
*/
public T pop() {
T value = top.value;
if (!top.empty()) {
top = top.next;
}
return value;
}
public static void main(String[] args) {
Stack stack = new Stack();
String string = "This is a test for stack";
System.out.println("入栈元素(String)依次为:");
for (String nodeIn : string.split(" ")) {
stack.push(nodeIn);
System.out.println(nodeIn);
}
System.out.println("出栈元素(String)依次为:");
String nodeOut;
while ((nodeOut = stack.pop()) != null) {
System.out.println(nodeOut);
}
}
}