近來複習數據結構,本身動手實現了棧。棧是一種限制插入和刪除只能在一個位置上的表。最基本的操做是進棧和出棧,所以,又被叫做「先進後出」表。java
實現方式是這樣的:首先定義了一個接口,而後經過這個接口實現了線性棧和鏈式棧,代碼比較簡單,以下:數組
1 package com.peter.java.dsa.interfaces; 2 3 /** 4 * 棧操做定義 5 * 6 * @author Peter Pan 7 */ 8 9 public interface Stack<T> { 10 /* 判空 */ 11 boolean isEmpty(); 12 13 /* 清空棧 */ 14 void clear(); 15 16 /* 彈棧 */ 17 T pop(); 18 19 /* 入棧 */ 20 boolean push(T data); 21 22 /* 棧的長度 */ 23 int length(); 24 25 /* 查看棧頂的元素,但不移除它 */ 26 T peek(); 27 28 /* 返回對象在棧中的位置 */ 29 int search(T data); 30 }
線性棧:以數組的方式實現。數據結構
1 package com.peter.java.dsa.common; 2 3 import com.peter.java.dsa.interfaces.Stack; 4 5 /** 6 * 線性棧 7 * 8 * @author Peter Pan 9 */ 10 public class LinearStack<T> implements Stack<T> { 11 @SuppressWarnings("unchecked") 12 private T[] t = (T[]) new Object[16]; 13 private int size = 0; 14 15 @Override 16 public boolean isEmpty() { 17 // TODO Auto-generated method stub 18 return size == 0; 19 } 20 21 @Override 22 public void clear() { 23 // TODO Auto-generated method stub 24 for (int i = 0; i < t.length; i++) { 25 t[i] = null; 26 } 27 size = 0; 28 } 29 30 @Override 31 public T pop() { 32 // TODO Auto-generated method stub 33 if (size == 0) { 34 return null; 35 } 36 T tmp = t[size - 1]; 37 t[size - 1] = null; 38 size--; 39 return tmp; 40 } 41 42 @Override 43 public boolean push(T data) { 44 // TODO Auto-generated method stub 45 if (size >= t.length) { 46 resize(); 47 } 48 t[size++] = data; 49 return true; 50 } 51 52 @Override 53 public int length() { 54 // TODO Auto-generated method stub 55 return size; 56 } 57 58 @Override 59 public T peek() { 60 // TODO Auto-generated method stub 61 if (size == 0) { 62 return null; 63 } else { 64 return t[size - 1]; 65 } 66 } 67 68 /* return index of data, return -1 if no data */ 69 @Override 70 public int search(T data) { 71 // TODO Auto-generated method stub 72 int index = -1; 73 for (int i = 0; i < t.length; i++) { 74 if (t[i].equals(data)) { 75 index = i; 76 break; 77 } 78 } 79 return index; 80 } 81 82 @SuppressWarnings("unchecked") 83 private void resize() { 84 T[] tmp = (T[]) new Object[t.length * 2]; 85 for (int i = 0; i < t.length; i++) { 86 tmp[i] = t[i]; 87 t[i] = null; 88 } 89 t = tmp; 90 tmp = null; 91 } 92 93 /* from the left to the right is from the top to the bottom of the stack */ 94 @Override 95 public String toString() { 96 // TODO Auto-generated method stub 97 StringBuffer buffer = new StringBuffer(); 98 buffer.append("Linear Stack Content:["); 99 for (int i = t.length - 1; i > -1; i--) { 100 buffer.append(t[i].toString() + ","); 101 } 102 buffer.append("]"); 103 buffer.replace(buffer.lastIndexOf(","), buffer.lastIndexOf(",") + 1, ""); 104 return buffer.toString(); 105 } 106 107 }
鏈式棧:經過單鏈表進行實現。app
1 package com.peter.java.dsa.common; 2 3 import com.peter.java.dsa.interfaces.Stack; 4 5 public class LinkedStack<T> implements Stack<T> { 6 private Node top; 7 private int size; 8 9 @Override 10 public boolean isEmpty() { 11 // TODO Auto-generated method stub 12 return size == 0; 13 } 14 15 @Override 16 public void clear() { 17 // TODO Auto-generated method stub 18 top = null; 19 size = 0; 20 } 21 22 @Override 23 public T pop() { 24 // TODO Auto-generated method stub 25 T topValue = null; 26 if (top != null) { 27 topValue = top.data; 28 Node oldTop = top; 29 top = top.prev; 30 oldTop.prev = null; 31 size--; 32 } 33 return topValue; 34 } 35 36 @Override 37 public boolean push(T data) { 38 // TODO Auto-generated method stub 39 Node oldTop = top; 40 top = new Node(data); 41 top.prev = oldTop; 42 size++; 43 return true; 44 } 45 46 @Override 47 public int length() { 48 // TODO Auto-generated method stub 49 return size; 50 } 51 52 @Override 53 public T peek() { 54 // TODO Auto-generated method stub 55 T topValue = null; 56 if (top != null) { 57 topValue = top.data; 58 } 59 return topValue; 60 } 61 62 @Override 63 public int search(T data) { 64 // TODO Auto-generated method stub 65 int index = -1; 66 Node tmp = top; 67 for (int i = size - 1; i > -1; i--) { 68 if (tmp.data.equals(data)) { 69 index = i; 70 break; 71 } else { 72 tmp = tmp.prev; 73 } 74 } 75 tmp = null; 76 return index; 77 } 78 79 @Override 80 public String toString() { 81 // TODO Auto-generated method stub 82 StringBuffer buffer = new StringBuffer(); 83 buffer.append("Linked Stack Content:["); 84 Node tmp = top; 85 for (int i = 0; i < size - 1; i++) { 86 buffer.append(tmp.toString() + ","); 87 tmp = tmp.prev; 88 } 89 tmp = null; 90 buffer.append("]"); 91 buffer.replace(buffer.lastIndexOf(","), buffer.lastIndexOf(",") + 1, ""); 92 return super.toString(); 93 } 94 95 private class Node { 96 T data; 97 Node prev; 98 99 public Node(T data) { 100 // TODO Auto-generated constructor stub 101 this.data = data; 102 } 103 } 104 105 }
學習還在進行中,之後會繼續更新代碼。ide