数据结构队列的练习

数组实现单队列

Object数组实现。

package com.bennett.test0927;

import com.sun.xml.internal.txw2.IllegalAnnotationException;

public class ArrayQueue3 {
	private Object[] objects;
	private int size;
	private int start;
	private int end;
	
	public ArrayQueue3(int initSize) {
		if (initSize < 0) {
			throw new IllegalAnnotationException("the init size must more than 0.");
		}
		objects = new Object[initSize];
		start = 0;
		end = 0;
		size = 0;
	}
//	入队
	public void push(int obj) {
		if (size == objects.length) {
			throw new ArrayIndexOutOfBoundsException("The queue is full.");
		}
		objects[end] = obj;
		end = end == objects.length - 1 ? 0:end + 1;
		size++;
	}
//	出队
	public Object pop() {
		if (size == 0) {
			throw new ArrayIndexOutOfBoundsException("The queue is empty.");
		}
		int tmp = start;
		start = start == objects.length - 1 ? 0:start + 1;
		size--;
		return objects[tmp];
	}
//	输出队首元素
	public Object peek(){
		if (size == 0) {
			return null;
		}
		return objects[start];
	}
//	打印队列元素
	public void print() {
		if (size == 0) {
			throw new ArrayIndexOutOfBoundsException("The queue is empty.");
		}
		for (int i = start; i < objects.length; i++) {
			System.out.print(objects[i]+" ");
		}
	}
	public static void main(String[] args) {
		ArrayQueue3 arrayQueue3 = new ArrayQueue3(10);
		for (int i = 0; i < 10; i++) {
			arrayQueue3.push(i);
		}
		arrayQueue3.print();
		System.out.println("\n队首元素:"+arrayQueue3.peek());
		for (int i = 0; i < 3; i++) {
			arrayQueue3.pop();
		}
		arrayQueue3.print();
		System.out.println();
		System.out.println("队首元素:"+arrayQueue3.peek());
	}
}

数组实现循环队列

后续还需继续优化……

package com.bennett.test0915;

import java.util.Scanner;

/**
 * @version:
 * @Description:循环队列
 * @author bennett
 * @date: 2021年10月4日 下午2:44:14
 */
public class Queue {
	private static Scanner scanner = new Scanner(System.in);
	private static int[] queueArray = new int[8];
	private static int head;// 出队一个元素head加1
	private static int tail;// 入队一个元素tail加1

	public static void main(String[] args) {
		options();
	}
//	判断队满
	private static boolean Full_Queue(int[] queueArray2, int head2, int tail2) {
		if (Math.abs(tail2 - head2) == queueArray.length) {
			System.out.println("队满");
			return true;
		} else {
			System.out.println("队非满");
			return false;
		}

	}

//	判断队空
	private static boolean Empty_Queue(int[] queueArray2, int head2, int tail2) {
		if (head2 == tail2) {
			System.out.println("队列为空");
			return true;
		} else {
			System.out.println("队列非空");
			return false;
		}
	}

	// 出队
	private static boolean Out_Queue(int[] queueArray2, int index) {
		if ((head + 1) % queueArray.length == tail) {
			return false;
		}
		System.out.print("出队一个队列元素:\n");
		queueArray[head] = 0;// 添加队元素
		head = (head + 1) % queueArray.length;
		return true;
	}

	// 入队
	private static boolean In_Queue(int[] queueArray, int index) {
		if ((tail + 1) % queueArray.length == head) {
//			throw new IndexOutOfBoundsException("下标越界!!");
			System.out.println("循环队列已经满了!!");
			return false;
		}
		System.out.print("请输入一个入队元素 :");
		int num = scanner.nextInt();
		queueArray[tail] = num;
		tail = (tail + 1) % queueArray.length;
		return true;
	}

//	打印数组
	private static void print(int[] queueArray) {
		for (int i = 0; i < queueArray.length; i++) {
			System.out.print(queueArray[i] + " ");
		}
		System.out.println();
	}

	private static void options() {
		String tip = "\t队的操作\n\t1、判断队空\n\t2、判断队满\n\t3、入队\n\t4、出队\n\t5、打印队列元素\n\t0、结束\n\t请输入您的操作序号:";
		System.out.println(tip);
		int key = scanner.nextInt();
		switch (key) {
		case 1:
			Empty_Queue(queueArray, head, tail);
			options();
			break;
		case 2:
			Full_Queue(queueArray, head, tail);
			options();
			break;
		case 3:
			In_Queue(queueArray, tail);
			options();
			break;
		case 4:
			Out_Queue(queueArray, head);
			options();
			break;
		case 5:
			print(queueArray);
			options();
			break;
		case 0:
			System.out.println("拜拜!");
			break;
		default:
			System.out.println("您的输入错误,请重新输入:");
			options();
			break;
		}
	}
}

LinkedList集合实现队列

package com.bennett.test0927;

/**
 * @version:
 * @Description:队列的接口
 * @author gxd
 * @date: 2021年9月27日 下午7:44:02
 */ 
public interface MyQueue {
	public boolean isEmpty();
	public boolean isFull();
	public void push(Object object);
	public void pop();
}

package com.bennett.test0927;

import java.util.LinkedList;
import java.util.List;

public class ListQueue implements MyQueue{
	private List<Object> list = new LinkedList<Object>();
//	判空
	@Override
	public boolean isEmpty() {
		return list.isEmpty();
	}

	@Override
	public boolean isFull() {
		return true;
	}

	@Override
	public void push(Object object) {
		list.add(object);
	}

	@Override
	public void pop() {
		if (list.size() < 0) {
			throw new IndexOutOfBoundsException("集合为空,下标越界。");
		}
		list.remove(0);
	}
//	打印集合元素
	public void print(){
		for (int i = 0; i < list.size(); i++) {
			System.out.print(list.get(i)+" ");
		}
	}
//	测试类
	public static void main(String[] args) {
		ListQueue listQueue = new ListQueue();
		
		for (int i = 0; i < 5; i++) {
			listQueue.push(i);
		}
		
		listQueue.print();
		System.out.println();
		for (int i = 0; i < 2; i++) {
			listQueue.pop();
		}
		listQueue.print();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值