20162316刘诚昊 用数组实现循环队列
2017-10-22 15:15
519 查看
20162316刘诚昊 2017-2018-2 《Java程序设计》用数组实现循环队列
实验要求
1 参考程序15.6给出方法deque,first,isEmpty,size,toString的定义,完成CireclularArrayQueue过程
1.完成书上的代码:import Week_5.EmptyCollectionException; public class CircularArrayQueue<T> implements QueueADT<T> { private final int DEFAULT_CAPACITY = 10; private int front, rear, count; private T[] queue; /* * Creates an empty queue using the default capacity. */ public CircularArrayQueue() { front = rear = count = 0; queue = (T[]) (new Object[DEFAULT_CAPACITY]); } /** * Adds the specified element to the rear of this queue, expanding * the capacity of the queue array if necessary. */ public void enqueue (T element) { if (size() == queue.length) expandCapacity(); queue[rear] = element; rear = (rear+1) % queue.length; count++; } /** * Removes the element at the front of this queue and returns a * reference to it. Throws an EmptyCollectionException if the * queue is empty. */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException ("queue"); T result = queue[front]; queue[front] = null; front = (front+1) % queue.length; count--; return result; } /* * Returns a reference to the element at the front of this queue. * The element is not removed from the queue. Throws an * EmptyCollectionException if the queue is empty. */ public T first() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException ("queue"); return queue[front]; } public boolean isEmpty() { return (count == 0); } public int size() { return count; } public String toString() { String result = ""; for(int num = 0; num<count; num++){ result = result + queue[(front+num)%queue.length] + " "; } return result; } /** * Creates a new array to store the contents of this queue with * twice the capacity of the old one. */ public void expandCapacity() { T[] larger = (T[])(new Object[queue.length *2]); for(int scan=0; scan < count; scan++) { larger[scan] = queue[front]; front=(front+1) % queue.length; } front = 0; rear = count; queue = larger; } }
2.测试:
public class CircularArrayQueueTest extends TestCase { CircularArrayQueue CAQ = new CircularArrayQueue(); public void testDequeue() throws Exception { CAQ.enqueue(01); CAQ.enqueue(02); assertEquals(01,CAQ.dequeue()); assertEquals(02,CAQ.dequeue()); } public void testFirst() throws Exception { CAQ.enqueue(01); CAQ.enqueue(02); assertEquals(01,CAQ.first()); } public void testIsEmpty() throws Exception { assertEquals(true,CAQ.isEmpty()); CAQ.enqueue(01); assertEquals(false,CAQ.isEmpty()); } public void testSize() throws Exception { CAQ.enqueue(01); CAQ.enqueue(02); CAQ.enqueue(03); CAQ.enqueue(04); assertEquals(4,CAQ.size()); } public void testToString() throws Exception { CAQ.enqueue(01); CAQ.enqueue(02); CAQ.enqueue(03); CAQ.enqueue(04); assertEquals(1+ " " + 2+" " +3+" " +4 + " ",CAQ.toString() ); } }
相关文章推荐
- Data Structure-2 Queue 循环队列,用数组实现
- 循环队列的数组表示函数的实现
- 数据结构与算法分析—循环队列的数组实现(C语言)
- 基于数组的循环队列(C++模板实现)
- 循环数组队列的实现
- Python 使用循环数组实现队列 (基于class, 包含迭代器)
- 循环数组实现队列(java)。
- 简单队列和循环队列的实现--数组实现
- 数据结构之——基于数组实现的循环队列
- 深入理解循环队列----循环数组实现ArrayDeque
- C语言数组实现循环队列
- java实现 用数组实现循环队列
- 利用循环数组实现的队列
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- C++用数组实现栈和循环队列
- STL之顺序容器适配器(队列的循环数组实现)
- 数组实现顺序队列、顺序循环队列
- 深入理解循环队列----循环数组实现ArrayDeque
- 数据结构-循环数组实现队列
- 队列--基于动态循环数组实现(Java)