Java实现队列-顺序队列&链式队列

 

Java实现顺序队列

SeqQueue.class

package sequence;

import java.util.Arrays;

public class SeqQueue<T> {
	private int front;
	private int rear;
	private T[] elementData;
	//private int queueLength;
	private int queueCapacity;
	private static final int DEAFAULT_LENGTH = 3;
	
	public SeqQueue(){
		front = rear = 0;
		queueCapacity = DEAFAULT_LENGTH;
		elementData = (T[]) new Object[DEAFAULT_LENGTH];
	}
	
	public SeqQueue(T element){
		this();
		elementData[0] = element;
		rear++;
	}
	
	public SeqQueue(int size){
		this();
		queueCapacity = size;
		elementData = (T[]) new Object[size];
	}
	
	public boolean isEmpty(){
		return rear == front;
	}
	
	public int getSize(){
		return rear - front;
	}
	
	public void add(T element){
		//ensureCapacity()判断是否可以插入,满的话开辟空间
		ensureCapacity(rear + 1);
		elementData[rear++] = element;
	}
	
	public void ensureCapacity(int minCapacity){
		int oldCapacity = elementData.length;
		if(minCapacity > oldCapacity){
			int newCapacity = oldCapacity*2+1;
			if(newCapacity < minCapacity)
				newCapacity = minCapacity;
			T[] old = elementData;
			//开辟新的数组空间
			elementData = (T[]) new Object[newCapacity];
			//将现在数组元素复制到新的数组
			int j = 0;
			for(int i = front;i<rear;i++){
				elementData[j++] = old[i];
			}
			front = 0;
			rear = j;
			
			//上面数组赋值可直接调用api
//			elementData = Arrays.copyOf(elementData, newCapacity);
		}
	}
	
	public T remove(){
		if(isEmpty()){
			throw new IndexOutOfBoundsException("数组为空");
		}
		T tempValue = elementData[front];
		elementData[front++] = null;
		return tempValue;
	}
	
	//返回队首元素,但不删除
	public T peek(){
		if(isEmpty()){
			throw new IndexOutOfBoundsException("数组为空");
		}
		return (T)elementData[front];
	}
	
	public void traverse(){
		for(int i = front; i<rear;i++){
			System.out.println(elementData[i]);
		}
	}
	
	public void clear(){
		elementData = null;
		front = rear =0;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SeqQueue<String> sq = new SeqQueue<>();
		sq.add("aa");
		sq.add("bb");
		sq.add("cc");
		String s = sq.peek();
		System.out.println(s);
		System.out.println("=====");
		sq.traverse();
		System.out.println("=====");
		sq.remove();
		sq.traverse();
		System.out.println("=====");
		sq.add("dd");
		sq.add("ee");
		sq.traverse();
		System.out.println("=====");
		sq.clear();
		System.out.println(sq.getSize());
	}

}

 

 

 

 

 

 

结果:

aa
aa
bb
cc
=====
bb
cc
=====
bb
cc
dd
ee
=====
0

Java实现循环队列

CycleQueue.class

 
package cycle;

public class CycleQueue <T> {

	private int front;
	private int rear;
	private int queueCapacity;
	private int queueLength;
	private static final int DEAFAULT_LENGTH = 4;
	private Object[] elementData;


	public CycleQueue() {
		front = rear = queueLength = 0;
		queueCapacity = DEAFAULT_LENGTH;
		elementData = new Object[DEAFAULT_LENGTH];
	}

	public CycleQueue(Object element){
		this();
		rear++;
		queueLength++;
		elementData[0] = element;
	}
	
	public boolean isEmpty(){
		if(queueLength == 0)
			return true;
		return false;
	}

	public boolean isFull(){
		if(queueLength == queueCapacity)
			return true;
		return false;
	}
	
	public void clear(){
		elementData = null;
		rear = front = queueLength = 0;
	}
	
	public boolean add(T element){
		//判满
		if(isFull()){
			return false;
		}
		elementData[rear] = element;
		rear = (++rear) % queueCapacity;
		queueLength++;
		return true;
	}
	
	public T remove(){
		if(isEmpty()){
			throw new IndexOutOfBoundsException("空队列异常");
		}
		T element = (T) elementData[front];
		front = (++front) % queueCapacity;
		queueLength--;
		return element;
	}
	
	public void traverse(){
		for(int i=front;i<front+queueLength;i++){
			System.out.println(elementData[i % queueCapacity]);
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CycleQueue<String> cq = new CycleQueue<>();
		cq.add("1");
		cq.add("2");
		cq.add("3");
		cq.add("4");
		cq.add("5");
		cq.traverse();
		System.out.println();
		String tempValue = cq.remove();
		System.out.println(tempValue);
		System.out.println();
		cq.add("5");
		cq.traverse();
	}

}
 

结果:

traverse:1 2 3 4 
remove:1  
add : 5
traverse: 2 3 4 5

 

Java实现链式队列

 

 

 

LinkQueue.class
package link;

public class LinkQueue<T> {

	//定义内部类实现节点类
	class Node{
		public T data;
		public Node next;
		
		public Node(){
			
		}
		public Node(T data,Node next){
			this.data = data;
			this.next = next;
		}
	}
	
	private Node front;
	private Node rear;
	private int size;
	
	public LinkQueue(){
		front = rear = null;
		size = 0;
	}
	public LinkQueue(T element){
		front = new Node(element, null);
		rear = front;
		size++;
	}
	
	public int getSize(){
		return size;
	}
	public boolean isEmpty(){
		return front == null;
	}
	
	public void add(T element){
		//判断是否是空队列
		if(isEmpty()){
			front = new Node(element, null);
			rear = front;
		}else{
			Node node = new Node(element, null);
			rear.next = node;
			rear = node;
		}
		
		size++;
	}
	
	public T remove(){
		if(isEmpty()){
			throw new IndexOutOfBoundsException("空队列异常");
		}
		Node oldFront = front;
		front = front.next;
		oldFront.next = null;
		size--;
		return oldFront.data;
	}
	//返回队首但不删除队首元素
	public T peek(){
		return front.data;
	}
	
	public void clear(){
		front = rear =null;
		size=0;
		
	}
	
	public void traverse(){
		//循环结束条件是current!=null(不是current.next这样最后一个就不会输出)
		for(Node current = front;current != null;current = current.next){
			System.out.println(current.data);
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LinkQueue<String> lq = new LinkQueue<>("aaa");
		lq.add("bbb");
		lq.add("ccc");
		System.out.println(lq.peek());
		System.out.println("-------");
		lq.traverse();
		System.out.println("-------");
		lq.remove();
		lq.add("ddd");
		lq.traverse();
	}

}

 

结果:
aaa
-------
aaa
bbb
ccc
-------
bbb
ccc
ddd

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值