java使用泛型不使用任何库函数实现栈
2014-08-21 23:16
344 查看
有的笔试题需要不适用任何现成的数据结构实现stack或者queue,这是我写的一个简单的列子,只有入栈和出栈,希望对大家有帮助
package study; import java.util.LinkedList; import java.util.List; import java.util.Stack; /** * 使用泛型实现栈 * @author Shanys * * @param */ public class LinkedStack { private Node head; private static class Node{ U item; Node[u] next; public Node(){ item=null; next=null; } public Node(U item,Node[u] next){ this.item=item; this.next=next; } } public void push(T item){ //头插法,链表的思想实现栈 Node node = new Node(); node.item=item; if(head.next==null){ head.next=node; }else{ node.next=head.next; head.next=node; } } public T pop(){ T item = null; if(head.next!=null){ Node node = head.next; item = node.item; head.next=node.next; node = null; } return item; } public LinkedStack(){ head = new Node(); } public static void main(String[] args) { LinkedStack t = new LinkedStack(); for(String s : "this is a test".split(" ")){ System.out.println(s); t.push(s); } String temp;System.out.println("hello"); while((temp = t.pop())!=null){ System.out.println(temp); } } } [/u][/u]
相关文章推荐
- java web在不使用任何插件情况下实现文本框输入自动补全功能
- 【系列】重新认识Java——泛型(基础、使用和实现原理)
- JAVA 泛型实现原理及使用详解
- 详解Java中使用泛型实现快速排序算法的方法
- 不使用c的任何库函数 实现字符串到整数的转换 整数到字符串的转换
- [置顶] 实现内存的移动,功能类似于memmove,可以不实现内存重叠拷贝,不使用任何库函数
- java中的方法返回值使用泛型,实现灵活的返回值类型
- Java 使用泛型实现堆栈类
- 五星-原型模式常使用于以下场景--而JAVA中的任何类只要实现了Cloneable标识接口,就可以使用clone方法来进行对象的拷贝
- 从头认识java-13.15 使用泛型时出现的问题(2)-实现参数化接口与重载
- java使用泛型实现栈结构示例分享
- 不能使用任何库函数,不能定义任何变量实现strlen和strrev
- Java使用泛型实现栈结构
- Java初学者如何迈出AOP第一步--使用Java 动态代理实现AOP
- Java初学者如何迈出AOP第一步--使用Java 动态代理实现AOP(转)
- Java初学者如何迈出AOP第一步--使用Java 动态代理实现AOP
- 使用JAVA中的动态代理实现数据库连接池
- 使用Java动态代理实现AOP
- 使用Java技术在Cocoon中实现商业逻辑
- 在 Java 的泛型类型中使用通配符