数据结构之Queue

数据结构之queue的抽象:


```java
package com.dzlijian.utils.comments;
/**
 * 
 * @ClassName:  Queue   
 * @Description: 队列的数据结构抽象,先入先出
 * @author: 长子科技 
 * @date:   2021年7月7日 下午10:56:21   
 *   
 * @param <E>  
 * @Copyright: 2021 www.tydic.com Inc. All rights reserved. 
 */
public interface Queue<E> {
	/*
	 * 获取元素的个数
	 */
public int size();
/*
 * 判空
 */
public boolean empty();
/*
 * 进入队列
 */
public void enqueue(E e);
/*
 * 出队列
 */
public E dequeue();
/*
 * 获取队首元素
 */
public E peek();
}

1,利用数组进行实现:

```java
package com.dzlijian.utils.comments;

import javax.management.RuntimeErrorException;

import org.apache.commons.collections.list.GrowthList;

public class ArrayQueue<E> implements Queue<E> {
	private static final int DEFAULT_CAPACITY=10;
	private E[] data;
	
	private int size;
	
	private int head=-1;
	private int tail=-1;
	
public  ArrayQueue(int capacity) {
	data=(E[])new Object[capacity];
	this.size=0;
}
public  ArrayQueue() {
	this(DEFAULT_CAPACITY);
}
	@Override
	public int size() {
		// TODO Auto-generated method stub
		return size;
	}

	@Override
	public boolean empty() {
		// TODO Auto-generated method stub
		return size==0;
	}
	
	private void grow(int capacity){
		if(capacity<=DEFAULT_CAPACITY){
			return;
		}
		E[] newdata=(E[])new Object[capacity];
		for(int i=0;i<size;i++){
			newdata[i]=data[(head+i)%data.length];
		}
		data=newdata;
		head=0;
		tail=size-1;
	}

	@Override
	public void enqueue(E e) {
		if(e==null){
			throw new IllegalArgumentException("入队数据为空");
		}
		if(data.length==size){
			grow(2*size);
		}
		tail=(tail+1)%data.length;
		data[tail]=e;
		if(size==0){
			head=tail;
		}
		size++;
	}

	@Override
	public E dequeue() {
		if(size==0){
			throw new RuntimeException("队列为空");
		}
		E e=data[head];
		data[head]=null;
		head=(head+1)%data.length;
		size--;
		if(size<data.length/2){
			grow(data.length/2);
		}
		return e;
	}

	@Override
	public E peek() {
		if(size==0){
			throw new RuntimeException("队列为空");
		}
		return data[head];
	}
	
	public static void  main(String[] args){
	  Queue<Integer> queue=new ArrayQueue<>();
	  for(int i=0;i<20;i++){
		  queue.enqueue(i+1);
	  }
	  System.out.println(queue.size());
	  
	  for(int i=0;i<9;i++){
		  System.out.println("The  "+(i+1)+"th element is:"+queue.dequeue());
	  }
	  System.out.println(queue.size());
	  
	  for(int i=11;i>0;i--){
		  queue.enqueue(i);
	  }
	  System.out.println(queue.size());
	  
	  for(int i=0;i<19;i++){
		  System.out.println("The "+(i+1)+"th element is:"+queue.dequeue());
	  }
	  
	}

}

测试代码,可以自己跑一下。
2,利用链表来实现:
package com.dzlijian.utils.comments;

public class LinkedQueue implements Queue {

private static class Node<E>{
	private E data;
	
	private Node<E> next;
	
	public Node(E e,Node<E> next){
		data=e;
		this.next=next;
	}
}

private int size;
private Node<E> head;
private Node<E> tail;

public  LinkedQueue() {
	this.size=0;
}

@Override
public int size() {
	// TODO Auto-generated method stub
	return size;
}

@Override
public boolean empty() {
	// TODO Auto-generated method stub
	return size==0;
}

@Override
public void enqueue(E e) {
	Node<E> pre=tail;
	tail=new Node(e,null);
	if(size==0){
		head=tail;
	}else{
		pre.next=tail;
	}
	size++;
}

@Override
public E dequeue() {
	if(size==0){
		throw new RuntimeException("队列为空");
	}
	E data=head.data;
	head=head.next;
	size--;
	return data;
}

@Override
public E peek() {
	if(size==0){
		throw new RuntimeException("队列为空");
	}
	return head.data;
}

public static void  main(String[] args){
	  Queue<Integer> queue=new LinkedQueue<>();
	  for(int i=0;i<20;i++){
		  queue.enqueue(i+1);
	  }
	  System.out.println(queue.size());
	  
	  for(int i=0;i<9;i++){
		  System.out.println("The  "+(i+1)+"th element is:"+queue.dequeue());
	  }
	  System.out.println(queue.size());
	  
	  for(int i=11;i>0;i--){
		  queue.enqueue(i);
	  }
	  System.out.println(queue.size());
	  
	  for(int i=0;i<19;i++){
		  System.out.println("The "+(i+1)+"th element is:"+queue.dequeue());
	  }
	  
	}

}
请复制代码后,自己进行测试修改,有不对的地方欢迎指出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一截木化石

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值