近来复习数据结构,自己动手实现了栈。栈是一种限制插入和删除只能在一个位置上的表。最基本的操作是进栈和出栈,因此,又被叫作“先进后出”表。
实现方式是这样的:首先定义了一个接口,然后通过这个接口实现了线性栈和链式栈,代码比较简单,如下:
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 }
链式栈:通过单链表进行实现。
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 }
学习还在进行中,以后会继续更新代码。
来源:https://www.cnblogs.com/littlepanpc/p/3436419.html