建立单项链表类:
java 代码
- package lk;
- public class ListedStack<T> {
- private static class Node<U> {
- U itme;
- Node<U> next;
- Node() {
- itme = null;
- next = null;
- }
- Node(U item, Node<U> next) {
- this.itme = item;
- this.next = next;
- }
- boolean end() {
- return this.itme == null && this.next == null;
- }
- }
- private Node<T> top = new Node<T>();
- public void push(T item){
- top = new Node<T>(item,top);
- }
- public T pop(){
- T result = top.itme;
- if(!top.end()){
- top = top.next;
- }
- return result;
- }
- public static void main(String[] args){
- ListedStack<String> lis = new ListedStack<String>();
- for(String s : "wo am i!".split(" ")){
- lis.push(s);
- }
- String s;
- while((s=lis.pop())!=null){
- System.out.println(s);
- }
- }
- }