自定义堆

来源:互联网 发布:ubuntu硬盘分区方案 编辑:程序博客网 时间:2024/06/01 22:39
package com.henry.generic;//自定义堆栈类public class LinkedStack {//定义一个内部类的节点private static class Node{U item;Node next;Node(){item=null;next=null;}Node(U item,Node next){this.item=item;this.next=next;}boolean end(){return item==null && next==null;}}//堆栈底端哨兵private Node top=new Node<>();//入栈public void push(T item){top=new Node<>(item,top);}//出栈,栈为空时,返回一个null值public T pop(){T result = top.item;if(top.end()==false){top=top.next;}return result;}public static void main(String[] args) {LinkedStack lss=new LinkedStack<>();for(String s : "Welcome to beijing.".split(" ")){lss.push(s);}String s;while((s=lss.pop())!=null){System.out.println(s);}}}
原创粉丝点击