栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据结构队列的练习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构队列的练习

数据结构队列的练习
  • 数组实现队列
  • linkedList集合实现队列

数组实现队列

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

linkedList集合实现队列
package com.bennett.test0927;

 
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 list = new linkedList();
//	判空
	@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();
	}
}


转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号