数据结构(栈与队列) java实现

  • 栈的相关定义
package StackDef;

import java.util.Arrays;
import java.util.EmptyStackException;

/**
 * 通过数组模拟栈
 * @author tian
 *
 */
public class ArrayStack {
	private Object[] elementData;
	
	private int top;
	
	private int size;
	
	public int getTop() {
		return top;
	}

	public void setTop(int top) {
		this.top = top;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	/**
	 * 默认构造一个容量为10的栈
	 */
	public ArrayStack(){
		this.elementData = new Object[10];
		this.top = -1;
		this.size = 10;
	}
	
	public ArrayStack(int initSize){
		if (initSize <0) {
			throw new IllegalArgumentException("栈的初始容量不能为0");
		}
		
		this.elementData = new Object[initSize];
		this.top = -1;
		this.size = initSize;
	}
	
	
	public Object push(Object obj){
		isGrow(top+1);
		elementData[++top] = obj;
		return obj;
	}
	
	public Object pop(){
		if (this.top == -1) {
			throw new EmptyStackException();
		}
		Object obj = elementData[top];
		elementData[top]=null;
		this.top --;
		return obj;
	}
	
	public boolean isEmpty(){
		return top ==-1;
	}
	
	
	
	public boolean isGrow(int minSize){
		int oldSize = size;
		if (minSize >= oldSize) {
			int newSize = 0;
			if ((oldSize<<1) - Integer.MAX_VALUE >0) {
				newSize = Integer.MAX_VALUE;
			} else {
				newSize = (oldSize <<1);
			}
			
			this.size = newSize;
			elementData = Arrays.copyOf(elementData, size);
			return true;
			
		} else {
			return false;
		}
	}
	
	
}
  • 两个栈构成一个队列
public class Queue {
	
	private ArrayStack stack1;
	private ArrayStack stack2;
	
	public Queue() {
		stack1 = new ArrayStack(10);
		stack2 = new ArrayStack(10);
	}
	
	public Object pop() throws Exception{
		if (stack2.getTop()==-1) {
			while (stack1.getTop() !=-1) {
				Object obj = stack1.pop();
				stack2.push(obj);
			}
		}
		
		if (stack2.getTop() == -1) {
			throw new Exception("queue is empty");
		}
		return stack2.pop();
		
	}
	
	public Object push(Object obj){
		return stack1.push(obj);
	}
	
	public static void main(String[] args) throws Exception {
		Queue queue = new Queue();
		queue.push(1);
		queue.push(2);
		queue.push(3);
		queue.push(4);
		queue.push(5);
		
		System.out.println(queue.pop());
		System.out.println(queue.pop());
		System.out.println(queue.pop());
		System.out.println(queue.pop());
		System.out.println(queue.pop());
		System.out.println(queue.pop());
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
迷宫问题是一个经典的算法问题,可以使用Java中的数据结构队列来解决。下面是一个使用队列解决迷宫问题的示例代码: ```java import java.util.*; public class MazeSolver { private static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 上下左右四个方向 public static boolean solveMaze(int[][] maze, int startX, int startY, int endX, int endY) { int rows = maze.length; int cols = maze[0].length; boolean[][] visited = new boolean[rows][cols]; // 记录是否访问过 visited[startX][startY] = true; Stack<int[]> stack = new Stack<>(); // 使用来进行深度优先搜索 stack.push(new int[]{startX, startY}); while (!stack.isEmpty()) { int[] curr = stack.pop(); int currX = curr[0]; int currY = curr[1]; if (currX == endX && currY == endY) { return true; // 找到终点 } for (int[] dir : DIRECTIONS) { int nextX = currX + dir[0]; int nextY = currY + dir[1]; if (nextX >= 0 && nextX < rows && nextY >= 0 && nextY < cols && maze[nextX][nextY] == 0 && !visited[nextX][nextY]) { stack.push(new int[]{nextX, nextY}); visited[nextX][nextY] = true; } } } return false; // 无法找到路径 } public static void main(String[] args) { int[][] maze = { {0, 1, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 0, 0, 0}, {0, 1, 1, 1, 0}, {0, 0, 0, 1, 0} }; int startX = 0; int startY = 0; int endX = 4; int endY = 4; boolean canSolve = solveMaze(maze, startX, startY, endX, endY); System.out.println("Can solve maze: " + canSolve); } } ``` 这段代码使用了一个二维数组来表示迷宫,其中0表示可以通过的路径,1表示墙壁。通过深度优先搜索的方式,使用来记录路径,直到找到终点或者无法找到路径为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值