From charlesreid1

Line 4: Line 4:


implementation details:
implementation details:
* one option is to have an array that is re-sized and shifted each time the front is removed.
* floating head/tail index variables and dynamic resizing
* a better option is to have floating head and tail index variables.
* dynamic moving also required - if head floats too far.
* would then need to check if head floated too far.
* variable or fixed size.
* can have variable size array, or fixed size array.


==code==
==code==


Link to the code on git.charlesreid1.com: https://charlesreid1.com:3000/cs/java/src/master/stacks-queues-deques/queues/ArrayQueue.java
Link to the code on git.charlesreid1.com: https://charlesreid1.com:3000/cs/java/src/master/stacks-queues-deques/queues/ArrayDeque.java


==interface==
==interface==


The methods implemented for a queue object are:
The methods implemented for a deque object are:
* size() - get the number of elements in the queue
* size() - get the number of elements in the queue
* isEmpty() - returns true if queue is empty
* isEmpty() - returns true if queue is empty
* add() - add the element to the back of the queue
* first() - return reference to front, but don't remove
* remove() - remove and return the element at the front of the queue
* last() - return reference to back, but don't remove
* peek() - return a reference to the first item in the queue, but do not remove it
* addFront() - add new item to front of queue
 
* addBack() - add new item to back of queue
Implementing these for an array based queue requires us to do the following:
* removeFront() - remove item from front of queue
* size - keep track of the size (tail - head)
* removeBack() - remove item from back of queue
* add method - when things are added and we are at capacity, copy new items into new array (from head cycling through to tail, in order.)
* remove - to remove an item from the queue we remove it and increment head by 1 (much better than the O(n) scooting everything up)
* resize - allocates a new array, and copies from old to new from head to tail, resetting index values.


=Flags=
=Flags=

Revision as of 21:25, 3 June 2017

Notes

array-based deque (double-ended queue) object.

implementation details:

  • floating head/tail index variables and dynamic resizing
  • dynamic moving also required - if head floats too far.
  • variable or fixed size.

code

Link to the code on git.charlesreid1.com: https://charlesreid1.com:3000/cs/java/src/master/stacks-queues-deques/queues/ArrayDeque.java

interface

The methods implemented for a deque object are:

  • size() - get the number of elements in the queue
  • isEmpty() - returns true if queue is empty
  • first() - return reference to front, but don't remove
  • last() - return reference to back, but don't remove
  • addFront() - add new item to front of queue
  • addBack() - add new item to back of queue
  • removeFront() - remove item from front of queue
  • removeBack() - remove item from back of queue

Flags