public class Deque<Item>
extends java.lang.Object
implements java.lang.Iterable<Item>
Requirement: Class must be in iterator package and exercises.* must be imported.
Javadoc: javadoc -author -version -private -classpath .\algs4_sts.jar; -d .\javadoc Deque.java
Single Compilation: javac -cp .;algs4_sts.jar; Deque.java
Execution 1: java -cp ../;.;algs4_sts.jar; iterator.Deque < distinct.txt
Execution 2: java -cp ../;.;algs4_sts.jar; iterator.Deque < duplicates.txt
Summary: Deque is an abstract data type that implements the functions of a double-ended queue. It supports adding and removing items from the front or back of the data structure. This implementation uses a linked list to store the queue, because of the requirement to add remove data from the front and the end of the queue. Since a linked list stores a reference to the head and tail, this data type is the best choice to implement the dequeue. The program will read from a file, perform the basic queue operations and print the queue to screen using an iterator.
Note: Deque is a stand alone application.
Files: The files contain a series of strings, seperated by spaces.
Modifier and Type | Class and Description |
---|---|
class |
Deque.iteratorLoop
Iterator Class contains hasNext() and next() methods, which are used to instantiate an iterator object.
|
class |
Deque.Node
Node class defines the structure of the linked list.
|
Modifier and Type | Field and Description |
---|---|
private int |
arraySize
The size of the input array
|
(package private) Deque.Node |
head
The first node in the queue.
|
private Item[] |
input
The input array contains the data read from external file and manipulated.
|
(package private) Deque.Node |
tail
The last node in the queue.
|
private int |
totalItems
The total number of nodes in the array.
|
Constructor and Description |
---|
Deque()
Deque constructor intializes the linked list head and tail nodes to null.
|
Modifier and Type | Method and Description |
---|---|
void |
addFirst(Item item)
Items a generic item to the beginning of the queue.
|
void |
addLast(Item item)
Items a generic item to the end of the queue.
|
Deque.Node |
createNode(Item item,
Deque.Node nodeValue)
Creates a new Node to be added to the queue.
|
boolean |
isEmpty()
Returns value indicating if the randomized queue is empty.
|
java.util.Iterator<Item> |
iterator()
The iterator method returns an object of the iterator class iteratorLoop
|
void |
loop(Deque<Item> dq)
loop iterates through the queue and prints out all the items in the list.
|
static void |
main(java.lang.String[] args) |
void |
printList()
Prints the queue without an iterator.
|
Item |
removeFirst()
Removes the first node in the queue.
|
Item |
removeLast()
Removes the last node in the queue.
|
int |
size()
Returns the number of items on the randomized queue.
|
private int arraySize
private Item[] input
private int totalItems
Deque.Node head
Deque.Node tail
public Deque()
public boolean isEmpty()
public int size()
public void addFirst(Item item)
item
- The generic item to be added to the queue.public void addLast(Item item)
item
- The generic item to be added to the queue.public Deque.Node createNode(Item item, Deque.Node nodeValue)
item
- The generic item to be added to the queue.nodeValue
- The value that the .next pointer references.public void printList()
public Item removeFirst()
public Item removeLast()
public java.util.Iterator<Item> iterator()
iterator
in interface java.lang.Iterable<Item>
public void loop(Deque<Item> dq)
dq
- A reference to a deque object.public static void main(java.lang.String[] args)