栈
2016-01-28 23:47
218 查看
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; } }
相关文章推荐
- keil软件异常
- 关于Redis中的Replication
- 【Groovy】闭包的上下文
- 论架构师的自我修养
- hdoj5512Pagodas【gcd】
- Android中的线程池(学习笔记)
- java如何实现人类识别技术
- Android gradle学习(一)
- Hello World!
- 单片机Json框架的实现
- Java对IP地址排序
- 详解MemCached原理
- 异步加载之AsyncTask的基本使用(一)
- Mysql时间存储
- BZOJ4361 : isn
- Android自定义一个播放器控件
- Android必学-异步加载(一阶段)
- 使用VideoView自定义一个播放器控件
- 使用VideoView自定义一个播放器控件
- Windows Server 2012 R2在线安装.NET Framework3.5