Stack实现

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());
	}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DFS(深度优先搜索)是一种图遍历算法,可以用来解决很多问题,比如查找图中的连通分量、查找图中的路径等。在Java中,我们可以用Stack实现DFS算法。 DFS使用栈(Stack数据结构来存储待遍历的节点。具体实现步骤如下: 1. 首先,我们需要创建一个Stack对象来存储待遍历的节点。 2. 然后,我们选择一个起始节点作为DFS的起点,将其入栈。 3. 接着,我们进行循环操作,直到栈为空为止。每次循环中,我们从栈中取出一个节点,将其标记为已访问,并遍历其邻居节点。将邻居节点中未访问过的节点入栈。 4. 最后,当栈为空时,表示DFS遍历结束。 这样就可以使用Stack实现DFS算法。下面是一个简单的Java代码示例: ```java import java.util.Stack; public class DFSStack { public void dfs(int[][] graph, int start) { boolean[] visited = new boolean[graph.length]; Stack<Integer> stack = new Stack<>(); stack.push(start); while (!stack.isEmpty()) { int node = stack.pop(); if (!visited[node]) { visited[node] = true; System.out.print(node + " "); for (int i = 0; i < graph.length; i++) { if (graph[node][i] == 1 && !visited[i]) { stack.push(i); } } } } } public static void main(String[] args) { int[][] graph = { {0, 1, 1, 0, 0}, {1, 0, 0, 1, 0}, {1, 0, 0, 1, 1}, {0, 1, 1, 0, 1}, {0, 0, 1, 1, 0} }; DFSStack dfs = new DFSStack(); dfs.dfs(graph, 0); } } ``` 以上就是用Stack实现DFS算法的一个简单例子。通过这种方式,我们可以使用Stack数据结构实现深度优先搜索算法,解决各种与图相关的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值