public class CircularArrayQueue<T> implements QueueADT<T> { private final static int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue; /** * Creates an empty queue using the specified capacity. * @param initialCapacity the initial size of the circular array queue */ public CircularArrayQueue (int initialCapacity) { front = rear = count = 0; queue = (T[]) (new Object[initialCapacity]); } /** * Creates an empty queue using the default capacity. */ public CircularArrayQueue() { this(DEFAULT_CAPACITY); } /** * Adds the specified element to the rear of this queue, expanding * the capacity of the queue array if necessary. * @param element the element to add to the rear of the queue */ public void enqueue(T element) { if (size() == queue.length) expandCapacity(); queue[rear] = element; rear = (rear+1) % queue.length; count++; } /** * Creates a new array to store the contents of this queue with * twice the capacity of the old one. */ private 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; } /** * Removes the element at the front of this queue and returns a * reference to it. * @return the element removed from the front of the queue * @throws 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. * @return the first element in the queue * @throws EmptyCollectionException if the queue is empty */ public T first() throws EmptyCollectionException { if (isEmpty()){ throw new EmptyCollectionException("queue"); } else return queue[front]; // To be completed as a Programming Project } /** * Returns true if this queue is empty and false otherwise. * @return true if this queue is empty */ public boolean isEmpty() { return count==0; // To be completed as a Programming Project } /** * Returns the number of elements currently in this queue. * @return the size of the queue */ public int size() { return count; // To be completed as a Programming Project } /** * Returns a string representation of this queue. * @return the string representation of the queue */ public String toString() { String result = ""; int a =front; for (int i = 0 ;i< count;i++){ result += queue[a]+" "; a++; } return result; // To be completed as a Programming Project } }
public class LinkedQueue<T> implements QueueADT<T> { private int count; private LinearNode<T> head, tail; /** * Creates an empty queue. */ public LinkedQueue() { count = 0; head = tail = null; } /** * Adds the specified element to the tail of this queue. * @param element the element to be added to the tail of the queue */ public void enqueue(T element) { LinearNode<T> node = new LinearNode<T>(element); if (isEmpty()) head = node; else tail.setNext(node); tail = node; count++; } /** * Removes the element at the head of this queue and returns a * reference to it. * @return the element at the head of this queue * @throws EmptyCollectionException if the queue is empty */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("queue"); T result = head.getElement(); head = head.getNext(); count--; if (isEmpty()) tail = null; return result; } /** * Returns a reference to the element at the head of this queue. * The element is not removed from the queue. * @return a reference to the first element in this queue * @throws EmptyCollectionException if the queue is empty */ public T first() throws EmptyCollectionException { if (isEmpty()){ throw new EmptyCollectionException("queue"); } else return head.getElement() ; // To be completed as a Programming Project } /** * Returns true if this queue is empty and false otherwise. * @return true if this queue is empty */ public boolean isEmpty() { return count == 0; // To be completed as a Programming Project } /** * Returns the number of elements currently in this queue. * @return the number of elements in the queue */ public int size() { return count; // To be completed as a Programming Project } /** * Returns a string representation of this queue. * @return the string representation of the queue */ public String toString() { String result = "" ; while(head!=null){ result+=head.getElement()+" "; head = head.getNext(); } return result; // To be completed as a Programming Project } }
pp5.1 pp5.2:前端
pp5.7:java
front.next = front.next.next
front = (front + 1 ) % queue,length?
问题1:用数组实现链表时,使用两次enqueue方法,在使用一次dequeue方法,发现第一次实现的没有用为null。
node
问题1解决方案:toString方法出现错误:
修改前:
修改后:
出现这个问题的缘由是:当使用dequeue方法后不知道front已经发生变化。git
虽然这一章使用的是之前的知识,好比相关的数组、栈、多态、继承什么的,可是发现本身仍是不能很熟练的运用。不少基础概念仍是很模糊,说明本身仍是得加油。
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 075/200 | 1/1 | 05/20 | |
第二周 | 560/500 | 1/2 | 13/38 | |
第三周 | 983/1000 | 1/3 | 21/60 |