package datastructure;
/**
* Linked list.
*
* @author Michelle Min mitchellemin@163.com
*/
public class LinkedQueue {
/**
* An inner class.
*/
class Node {
/**
* The data.
*/
int data;
/**
* The reference to the next node.
*/
Node next;
/**
*
* The constructor
*
* @param paraValue
* The data.
*
*/
public Node(int paraValue) {
// 在这个class都是封闭的时候,可以public么?
data = paraValue;
next = null;
}// Of the constructor
}// Of class Node
/**
* The header of the queue.
*/
Node header;
/**
* The tail of the queue.
*/
Node tail;
/**
*
* Construct an empty sequential list.
*
*/
public LinkedQueue() {
header = new Node(-1);
header.next = null;
tail = header;
}// Of the first constructor
/**
*
* Enqueue.
*
* @param paraValue
* The value of the new node.
*
*/
public void enqueue(int paraValue) {
Node tempNode = new Node(paraValue);
tail.next = tempNode;
tail = tempNode;
}// Of enqueue
/**
*
* Dequeue.
*
* @return The value at the header.
*
*/
public int dequeue() {
if (header.next == null) {
System.out.println("No element in the queue");
return -1;
}// Of if
int resultValue = header.next.data;
header.next = header.next.next;
return resultValue;
}// Of dequeue
/**
*
* Overrides the method claim in Object, the superclass of any class.
*
*/
public String toString() {
String resultString = "";
if (header.next == null) {
return "empty";
}// Of if
Node tempNode = header.next;
while (tempNode != null) {
resultString += tempNode.data + ", ";
tempNode = tempNode.next;
}// Of while
return resultString;
}// Of toString
/**
*
* The entrance of the program.
*
* @param args
* Not used now
*
*/
public static void main(String args[]){
LinkedQueue tempQueue = new LinkedQueue();
System.out.println("Initialized, the list is: " + tempQueue.toString());
for (int i = 0; i < 5; i++){
tempQueue.enqueue(i + 1);
}//Of for i
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
tempQueue.dequeue();
System.out.println("Dequeue, the queue is: " + tempQueue.toString());
int tempValue;
for (int i = 0; i < 5; i++){
tempValue = tempQueue.dequeue();
System.out.println("Looped delete " + tempValue + ", the new queue is: " + tempQueue.toString());
}//Of for i
}//Of main
}// Of class LinkedQueue