标签:style blog class code java ext
栈是一种基于后进先出的数据集合。用链表实现比数组实现更加高效。
使用链表以及泛型机制来实现可以达到最优设计目标:
(1)可以处理任意类型的数据;
(2)所需的空间总是和集合的大小成正比;
(3)操作所需的时间总是和集合的大小无关。
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
数组实现代码:
1 import java.util.Iterator; 2 import java.util.Scanner; 3 4 public class ResizingArrayStack<Item> implements Iterable<Item> { 5 6 private Item[] a = (Item[]) new Object[1]; //item 7 private int n = 0; //size 8 9 public boolean isEmpty(){ 10 return n == 0; 11 } 12 13 public int size(){ 14 return n; 15 } 16 17 private void resize(int max){ 18 //new Item[max] 19 Item[] tmp = (Item[]) new Object[max]; 20 for(int i = 0; i < n; i ++){ 21 tmp[i] = a[i]; 22 } 23 a = tmp; 24 } 25 26 public void push(Item item){ 27 if(n == a.length){ 28 //full 29 resize(n * 2); 30 } 31 a[n ++] = item; 32 } 33 34 public Item pop(){ 35 Item item = a[-- n]; 36 a[n] = null; //avoid loitering 37 38 if(n > 0 && n == a.length / 4){ 39 //too large 40 resize(a.length / 2); 41 } 42 43 return item; 44 } 45 46 public Iterator<Item> iterator(){ 47 return new ReverseArrayIterator(); 48 } 49 50 private class ReverseArrayIterator implements Iterator<Item> { 51 52 private int i = n; 53 54 public boolean hasNext(){ 55 return i > 0; 56 } 57 58 public Item next(){ 59 return a[-- i]; 60 } 61 62 public void remove(){} 63 64 } 65 66 public static void main(String[] args) { 67 Scanner in = new Scanner(System.in); 68 69 ResizingArrayStack<String> s = new ResizingArrayStack<>(); 70 while(in.hasNext()){ 71 String item = in.next(); 72 if(!item.equals("-")){ 73 s.push(item); 74 } 75 else if(!s.isEmpty()){ 76 System.out.print(s.pop() + " "); 77 } 78 } 79 80 System.out.println("(" + s.size() + " left on stack)"); 81 } 82 }
链表实现代码:
1 import java.util.Scanner; 2 import java.util.Iterator; 3 4 public class Stack<Item> implements Iterable<Item>{ 5 6 private class Node{ 7 //LinkedList Node 8 Item item = null; 9 Node next = null; 10 } 11 12 private Node first = null; //top 13 private int n = 0; //size 14 15 public boolean isEmpty(){ 16 return first == null; 17 } 18 19 public int size(){ 20 return n; 21 } 22 23 public void push(Item item){ 24 Node oldFirst = first; 25 first = new Node(); 26 first.item = item; 27 first.next = oldFirst; 28 n ++; 29 } 30 31 public Item pop(){ 32 Item item = first.item; 33 first = first.next; 34 n --; 35 return item; 36 } 37 38 public Iterator<Item> iterator(){ 39 return new ListIterator(); 40 } 41 42 private class ListIterator implements Iterator<Item>{ 43 private Node current = first; 44 45 public boolean hasNext(){ 46 return current != null; 47 } 48 49 public Item next(){ 50 Item item = current.item; 51 current = current.next; 52 return item; 53 } 54 55 public void remove(){} 56 } 57 58 public static void main(String[] args) { 59 Scanner in = new Scanner(System.in); 60 61 Stack<String> s = new Stack<>(); 62 while(in.hasNext()){ 63 String item = in.next(); 64 if(!item.equals("-")){ 65 s.push(item); 66 } 67 else if(!s.isEmpty()){ 68 System.out.print(s.pop() + " "); 69 } 70 } 71 72 System.out.println("(" + s.size() + " left on stack)"); 73 } 74 }
(参考自《Algorithm 4th》)
标签:style blog class code java ext
原文地址:http://www.cnblogs.com/7hat/p/3718131.html