1. 程式人生 > >棧的數組和鏈表實現(Java實現)

棧的數組和鏈表實現(Java實現)

javascrip search 分享圖片 sys blog inter () 結果 length

我以前用JavaScript寫過棧和隊列,這裏初學Java,於是想來實現棧,基於數組和鏈表。

下面上代碼:

  1 import java.io.*;
  2 //用接口來存放需要的所有操作
  3 interface stack<T>{
  4     boolean isEmpty(); //判空
  5     void clear(); //清空
  6     T pop();   //彈棧
  7     boolean push(T data); //入棧
  8     int length(); //返回長度
  9     T peek();  //查看棧頂值
 10     int search(T t); //
查找元素位置 11 void display(); //輸出所有的元素 12 } 13 14 //用數組實現棧 15 class ArrayStack<T> implements stack<T>{ 16 public ArrayStack(){} 17 private T[] array = (T[])new Object[16]; 18 private int size = 0; 19 20 public int length(){ 21 for(int i=0;i<array.length;i++){
22 if(array[i] != null) size++; 23 } 24 return size; 25 } 26 27 public boolean isEmpty(){ 28 return (size == 0); 29 } 30 31 public void clear(){ 32 for(int i=0;i<array.length;i++){ 33 array[i] = null; 34 }
35 size = 0; 36 } 37 38 public T pop(){ 39 if(size == 0) return null; 40 else{ 41 T temp = array[size-1]; 42 array[size-1] = null; 43 size--; 44 return temp; 45 } 46 } 47 48 public boolean push(T data){ 49 if(size >= array.length) { 50 resize(); //重新分配一個兩倍大小的數組 51 array[size++] = data; 52 } 53 else{ 54 array[size++] = data; 55 } 56 return true; 57 } 58 59 public void resize(){ 60 T[] temp = (T[])new Object[array.length*2]; 61 for(int i=0;i<array.length;i++){ 62 temp[i] = array[i]; 63 } 64 for(int i=array.length;i<array.length*2;i++){ 65 temp[i] = null; 66 } 67 array = temp; 68 temp = null; 69 } 70 71 public T peek(){ 72 if(size == 0) return null; 73 else return array[size-1]; 74 } 75 76 public int search(T t){ 77 for(int i=0;i<size;i++){ 78 if(array[i] == t) return i+1; 79 } 80 return 0; 81 } 82 83 public void display(){ 84 for(int i=0;i<size;i++){ 85 System.out.println("data: " + array[i]); 86 } 87 } 88 89 } 90 91 //用鏈表實現棧 92 93 class LinkStack<T> implements stack<T>{ 94 public LinkStack(){ 95 this.top = null; 96 this.size = 0; 97 } 98 //存放數據的結點 99 class Node{ 100 private T data; 101 private Node pre; 102 } 103 private Node top; //棧頂指針 104 private int size; //棧的大小 105 106 public boolean isEmpty(){ 107 if(size == 0) return true; 108 else return false; 109 } 110 111 112 113 public void clear(){ 114 top = null; 115 size = 0; 116 } 117 118 119 public T pop(){ 120 if(top != null){ 121 T temp = top.data; 122 top = top.pre; 123 size--; 124 return temp; 125 } 126 return null; 127 } 128 129 130 public boolean push(T data){ 131 Node node = new Node(); 132 node.data = data; 133 node.pre = null; 134 if(top == null){ 135 top = node; 136 node = null; 137 size++; 138 return true; 139 } 140 else{ 141 node.pre = top; 142 top = node; 143 node = null; 144 size++; 145 return true; 146 } 147 } 148 149 150 public int length(){ 151 return size; 152 } 153 154 public T peek(){ 155 return top.data; 156 } 157 158 public int search(T t){ 159 int num = size; 160 while(top.pre != null) 161 { 162 if(top.data == t) 163 return (num-1); 164 else 165 { 166 top = top.pre; 167 num--; 168 } 169 } 170 return 0; 171 } 172 173 public void display(){ 174 Node node = top; 175 while(top != null) 176 { 177 System.out.println("data: " + top.data); 178 top = top.pre; 179 } 180 top = node; 181 node = null; 182 } 183 184 } 185 186 187 public class test{ 188 public static void main(String[] args){ 189 ArrayStack<String> a = new ArrayStack(); 190 a.push("hello,world..."); 191 a.push("my name is: "); 192 a.push("jeavenwong"); 193 a.display(); 194 195 LinkStack<String> b = new LinkStack(); 196 b.push("how are you?"); 197 b.push("i am fine..."); 198 b.push("and you?..."); 199 b.display(); 200 } 201 }

下面是我的運行結果:

技術分享圖片

如有不對,歡迎批評指正。

棧的數組和鏈表實現(Java實現)