Stack接口
public interface Stack<E> {
E pop();
void push(E e);
E peek();
boolean isEmpty();
void clear();
int size();
}
顺序表栈
public class Stack_Array<T> implements Stack<T> {
/* 当前元素个数 */
private int elementCount;
/* 数组容器默认大小 */
private int DEFAULT_CAPACITY = 10;
/* 数组容器 */
private T[] elementData;
@SuppressWarnings("unchecked")
public Stack_Array() {
this.elementData = (T[]) new Object[DEFAULT_CAPACITY];
this.elementCount = 0;
}
/**
* 弹出栈顶元素
*/
@Override
public T pop() {
if (elementCount == 0) {
throw new EmptyStackException();
}
T e = elementData[elementCount - 1];
elementCount--;
return e;
}
/**
* 将元素压入栈中
*/
@Override
public void push(T e) {
/* 如果elementCount达到数组容的长度,对容器数组空间进行扩展 */
ensureCapacity(elementCount + 1);
elementData[elementCount++] = e;
}
/**
* 扩展容器数组
*
* @param newLength
* 长度
*/
private void ensureCapacity(int newLength) {
if (newLength == elementData.length) {
/* 对其进行2倍长度扩展 */
elementData = Arrays.copyOf(elementData, elementData.length * 2);
}
}
/**
* 获取栈顶元素
*/
@Override
public T peek() {
if (elementCount == 0) {
throw new EmptyStackException();
}
return elementData[elementCount];
}
/**
* 判断栈是否为空
*/
@Override
public boolean isEmpty() {
return elementCount == 0;
}
/**
* 清空栈
*/
@Override
@SuppressWarnings("unchecked")
public void clear() {
this.elementData = (T[]) new Object[DEFAULT_CAPACITY];
elementCount = 0;
}
/**
* 返回栈元素个数
*/
@Override
public int size() {
return elementCount;
}
/**
* 返回栈的hashcode
*/
@Override
public int hashCode() {
int hashCode = 1;
for (int i = 0; i < elementCount; i++) {
hashCode = 31 * hashCode
+ (elementData[i] == null ? 0 : elementData[i].hashCode());
}
return hashCode;
}
}
链表栈
public class Stack_Linked<E> implements Stack<E> {
/* 引入一个空闲节点这样在写算法时更清晰易懂 */
private Node header;
/* 当前指向的节点 */
private Node current;
/* 当前栈元素个数 */
private int count;
public Stack_Linked() {
header = new Node();
current = header;
count = 0;
}
@Override
public E pop() {
if (current.next == null) {
throw new EmptyStackException();
}
count--;
E e = current.value;
current = current.next;
return e;
}
@Override
public void push(E e) {
Node newNode = new Node(e, current);
current = newNode;
count++;
}
@Override
public E peek() {
if (current.next == null) {
throw new EmptyStackException();
}
return current.value;
}
@Override
public boolean isEmpty() {
return current.next == null;
}
@Override
public void clear() {
header = new Node();
current = header;
count = 0;
}
@Override
public int size() {
return count;
}
@Override
public int hashCode() {
int hashcode = 1;
Node temp = current;
while (temp.next != null) {
hashcode = 31
* hashcode
+ (temp.next.value == null ? 0 : temp.next.value.hashCode());
temp = temp.next;
}
temp = null;
return hashcode;
}
private static class Node {
public Node() {
next = null;
value = null;
}
public Node(E value, Node next) {
this.value = value;
this.next = next;
}
Node next;
E value;
}
}
JUnit测试
public class Stack_LinkedTest {
@Test
public void testPop() {
Stack_Linked<String> s = new Stack_Linked<String>();
for(int i = 0 ; i < 10 ;i++){
s.push(String.valueOf(i));
s.pop();
}
assertTrue(s.isEmpty());
}
@Test
public void testPush() {
Stack_Linked<String> s = new Stack_Linked<String>();
for(int i = 0 ; i < 10 ;i++){
s.push(String.valueOf(i));
}
assertEquals(10, s.size());
}
@Test
public void testPeek() {
Stack_Linked<String> s = new Stack_Linked<String>();
for(int i = 0 ; i < 10 ;i++){
s.push(String.valueOf(i));
}
for(int i = 9 ; i >= 0 ; i--){
assertEquals(String.valueOf(i), s.peek());
s.pop();
}
}
@Test(expected = EmptyStackException.class)
public void testPeekException() {
Stack_Linked<String> s = new Stack_Linked<String>();
s.peek();
}
@Test
public void testIsEmpty() {
Stack_Linked<String> s = new Stack_Linked<String>();
for(int i = 0 ; i < 10 ;i++){
s.push(String.valueOf(i));
}
for(int i = 0 ; i < 10 ;i++){
s.pop();
}
assertTrue(s.isEmpty());
}
@Test
public void testClear() {
Stack_Linked<String> s = new Stack_Linked<String>();
for(int i = 0 ; i < 10 ;i++){
s.push(String.valueOf(i));
}
s.clear();
assertTrue(s.isEmpty());
}
@Test
public void testSize() {
Stack_Linked<String> s = new Stack_Linked<String>();
assertEquals(0, s.size());
for(int i = 0 ; i < 10 ;i++){
s.push(String.valueOf(i));
}
int expectedSize = 10;
assertEquals(expectedSize, s.size());
}
}