• JAVA该队列中的数组,圆阵队列,链队列


    /**
     * 文件名:QueueText.java
     * 时间:2014年10月22下午9:05:13
     * 笔者:维亚康姆维修
     */
    package chapter3;
    /**
     * 类名:ArrayQueue
     * 说明:数组实现
     */
    class ArrayQueue<AnyType>{
    	private static final int DEFAULT_CAPACITY = 10; 
    	private int front;//队头
    	private int rear;//队尾
    	private int theSize;
    	private AnyType[] theItems;
    	public ArrayQueue(){
    		clear();
    	}
    	public void clear(){
    		front = 0;
    		rear = 0;
    		theSize = 0;
    		ensureCapacity(DEFAULT_CAPACITY);
    	}
    	public boolean isEmpty(){
    		return theSize == 0;//or front == rear;
    	}
    	public void  enqueue(AnyType x){
    		theItems[rear++] = x;
    		theSize++;
    	}
    	public AnyType dequeue(){
    		if(theSize == 0)
    			return null;
    			theSize--;
    		return theItems[front++];
    	}
    	//返回队列前面的元素
    	public AnyType element(){
    		if(isEmpty())
    			return null;
    		return theItems[front];
    	}
    	/**
    	 * 方法名:ensureCapacity
    	 * 说明:确保容量
    	 */
    	public void ensureCapacity(int newCapacity){
    		if(newCapacity < theSize)
    			return;
    		AnyType[] old = theItems;
    		theItems = (AnyType[]) new Object[newCapacity];
    		for(int i = 0;i < theSize;i++)
    			theItems[i] = old[i];
    	}
    	
    }
    /**
     * 类名:CirArrQueue
     * 说明:循环队列 (由于用数组实现的队列在删除的时候指针会后移 这样导致了前面的浪费,
     * 循环数组能够解决问题,可是循环队列可能使得执行时间加倍)
     */
    class CirArrQueue<AnyType>{
    	private static final int DEFAULT_CAPACITY = 3; 
    	private int front;//队头
    	private int rear;//队尾
    	private int theSize;
    	private AnyType[] theItems;
    	public CirArrQueue(){
    		clear();
    	}
    	public void clear(){
    		theItems = (AnyType[]) new Object[DEFAULT_CAPACITY];
    		front = 0;
    		rear = 0;
    		theSize = 0;
    	}
    	public boolean isEmpty(){
    		return theSize == 0;//or front == rear;
    	}
    	public boolean  enqueue(AnyType x){
    		if(theSize == DEFAULT_CAPACITY)
    			return false;
    		theItems[rear++] = x;
    		theSize++;
    		if(rear == DEFAULT_CAPACITY)//假设到了尾部则回到0
    			rear = 0;
    		return true;
    	}
    	public AnyType dequeue(){
    		if(theSize == 0)
    			return null;
    			theSize--;
    			AnyType temp = theItems[front];
    			if(++front == DEFAULT_CAPACITY)//假设加1超过了数组 则返回到0;
    				front = 0;
    		return temp;
    	}
    	//返回队列前面的元素
    	public AnyType element(){
    		if(isEmpty())
    			return null;
    		return theItems[front];
    	}
    	
    }
    /**
     * 类名:LinkedQueue
     * 说明:链表实现队列
     */
    class LinkedQueue<AnyType>{
    	private static class Node<AnyType> {
    		Node(AnyType data, Node<AnyType> next) {
    			this.data = data;
    			this.next = next;
    		}
    		
    		private AnyType data;
    		private Node<AnyType> next;
    	}
    	
    	private Node<AnyType> front;
    	private Node<AnyType> rear;
    	public LinkedQueue(){
    		clear();
    	}
    	public void clear(){
    		front = null;
    		rear = null;
    	}
    	public boolean isEmpty(){
    		return front == null;
    	}
    	public void enqueue(AnyType x){
    		if(front == null&&rear == null){//最開始的时候
    			rear = new Node<AnyType>(x,null);
    			front = rear;
    		}else{
    			Node<AnyType> p = new Node<AnyType>(x,null);
    			rear.next = p;
    			rear = p;;
    		}
    			
    	}
    	public AnyType dequeue(){
    		if(!isEmpty()){
    			AnyType x = front.data;
    			front = front.next;
    			return x;
    		}
    		return null;
    	}
    	public AnyType element(){
    		if(!isEmpty())
    			return front.data;
    		return null;
    	}
    	
    }
    /**
     * 类名:QueueText
     * 说明:队列的数组和链表实现及循环队列的数组实现
     */
    public class QueueText {
    	/**
    	 * 方法名:main
    	 * 说明:測试
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		/****数组队列測试*****/
    		/*ArrayQueue<Integer> q = new ArrayQueue<Integer>();
    		q.enqueue(1);
    		q.enqueue(2);
    		q.enqueue(3);
    		q.enqueue(4);
    		System.out.println(q.element());
    		while(!q.isEmpty())
    			System.out.println(q.dequeue());
    	/*********链表队列測试**********/
    		/*LinkedQueue<Integer> q2 = new LinkedQueue<Integer>();
    		q2.enqueue(1);
    		q2.enqueue(2);
    		q2.enqueue(3);
    		q2.enqueue(4);
    		System.out.println(q2.element());
    		while(!q2.isEmpty())
    			System.out.println(q2.dequeue());
    	/***********循环队列測试************/	
    		CirArrQueue<Integer> q3 = new CirArrQueue<Integer>();
    		q3.enqueue(1);
    		q3.enqueue(2);
    		q3.enqueue(3);
    		q3.dequeue();
    		q3.enqueue(4);//设置默认容量为3。開始时3个,队列满了,后来删除第一个,队列数组里第一个空出来了,4进入了。可是还是满足先进先出的原则,加上                               //size的控制,使其不会覆盖后面的
    		q3.enqueue(5);//没有进去
    		System.out.println(q3.element());
    		while(!q3.isEmpty())
    			System.out.println(q3.dequeue());
    		
    	}
    
    }
    

    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    linux基础
    模块三、企业实战案例
    模块二、shell脚本逻辑结构
    模块一:shell 脚本基础
    三剑客、shell脚本
    定时任务、用户管理、磁盘介绍
    python笔记03
    文件属性、正则表达式、文件权限
    Linux系统目录结构介绍
    Linux基础及入门介绍
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4723928.html
Copyright © 2020-2023  润新知