データ構造のキュー

データ構造のキューの抽象化:


```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. リンク リストを使用して次のことを実現します。
パッケージ com.dzlijian.utils.comments;

パブリック クラス LinkedQueue はキューを実装します {

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

コードをコピーしてテストし、自分で修正してください。何
か間違っている場合は、ご指摘ください。

Supongo que te gusta

Origin blog.csdn.net/yijiemuhuashi/article/details/118559605
Recomendado
Clasificación