/** * An inner class. */ classNode { /** * The data. */ int data;
/** * The reference to the next node. */ Node next;
/** ******************* * The constructor. * * @param paraValue The data. ******************* */ publicNode(int paraValue) { data = paraValue; next = null; }// Of the constructor }// Of class Node
三、入队操作
输入
一个整数
输出
无
具体代码
1 2 3 4 5 6 7 8 9 10 11 12
/** ********************* * Enqueue. * * @param paraValue The value of the new node. ********************* */ publicvoidenqueue(int paraValue) { NodetempNode=newNode(paraValue); tail.next = tempNode; tail = tempNode; }// Of enqueue
/** ********************* * Dequeue. * * @return The value at the header. ********************* */ publicintdequeue() { if (header == tail) { System.out.println("No element in the queue"); return -1; } // Of if
intresultValue= header.next.data;
header.next = header.next.next;
// The queue becomes empty. if (header.next == null) { tail = header; } // Of if
/** * An inner class. */ classNode { /** * The data. */ int data;
/** * The reference to the next node. */ Node next;
/** ******************* * The constructor. * * @param paraValue The data. ******************* */ publicNode(int paraValue) { 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. ********************* */ publicLinkedQueue() { header = newNode(-1); tail = header; }// Of the first constructor
/** ********************* * Enqueue. * * @param paraValue The value of the new node. ********************* */ publicvoidenqueue(int paraValue) { NodetempNode=newNode(paraValue); tail.next = tempNode; tail = tempNode; }// Of enqueue
/** ********************* * Dequeue. * * @return The value at the header. ********************* */ publicintdequeue() { if (header == tail) { System.out.println("No element in the queue"); return -1; } // Of if
intresultValue= header.next.data;
header.next = header.next.next;
// The queue becomes empty. if (header.next == null) { tail = header; } // Of if
return resultValue; }// Of dequeue
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ public String toString() { StringresultString="";
if (header.next == null) { return"empty"; } // Of if
NodetempNode= 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. ********************* */ publicstaticvoidmain(String args[]) { LinkedQueuetempQueue=newLinkedQueue(); System.out.println("Initialized, the list is: " + tempQueue.toString());
for (inti=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 (inti=0; i < 5; i++) { tempValue = tempQueue.dequeue(); System.out.println("Looped delete " + tempValue + ", the new queue is: " + tempQueue.toString()); } // Of for i
for (inti=0; i < 3; i++) { tempQueue.enqueue(i + 10); } // Of for i System.out.println("Enqueue, the queue is: " + tempQueue.toString()); }// Of main }// Of class LinkedQueue
/** ********************* * Enqueue. * * @param paraValue The value of the new node. ********************* */ publicvoidenqueue(int paraValue) { if ((tail + 1) % TOTAL_SPACE == head) { System.out.println("Queue full."); return; } // Of if
data[tail % TOTAL_SPACE] = paraValue; tail++; }// Of enqueue
四、出队操作
输入
无
输出
若队列不为空则输出一个数值
若队列为空先打印字符串 "No element in the queue." 再返回 -1
具体代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/** ********************* * Dequeue. * * @return The value at the head. ********************* */ publicintdequeue() { if (head == tail) { System.out.println("No element in the queue."); return -1; } // Of if
/** * The total space. One space can never be used. */ publicstaticfinalintTOTAL_SPACE=10;
/** * The data. */ int[] data;
/** * The index for calculating the head. The actual head is head % TOTAL_SPACE. */ int head;
/** * The index for calculating the tail. */ int tail;
/** ******************* * The constructor ******************* */ publicCircleIntQueue() { data = newint[TOTAL_SPACE]; head = 0; tail = 0; }// Of the first constructor
/** ********************* * Enqueue. * * @param paraValue The value of the new node. ********************* */ publicvoidenqueue(int paraValue) { if ((tail + 1) % TOTAL_SPACE == head) { System.out.println("Queue full."); return; } // Of if
data[tail % TOTAL_SPACE] = paraValue; tail++; }// Of enqueue
/** ********************* * Dequeue. * * @return The value at the head. ********************* */ publicintdequeue() { if (head == tail) { System.out.println("No element in the queue."); return -1; } // Of if
intresultValue= data[head % TOTAL_SPACE];
head++;
return resultValue; }// Of dequeue
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ public String toString() { StringresultString="";
if (head == tail) { return"empty"; } // Of if
for (inti= head; i < tail; i++) { resultString += data[i % TOTAL_SPACE] + ", "; } // Of for i
return resultString; }// Of toString
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { CircleIntQueuetempQueue=newCircleIntQueue(); System.out.println("Initialized, the list is: " + tempQueue.toString());
for (inti=0; i < 5; i++) { tempQueue.enqueue(i + 1); } // Of for i System.out.println("Enqueue, the queue is: " + tempQueue.toString());
inttempValue= tempQueue.dequeue(); System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
for (inti=0; i < 6; i++) { tempQueue.enqueue(i + 10); System.out.println("Enqueue, the queue is: " + tempQueue.toString()); } // Of for i
for (inti=0; i < 3; i++) { tempValue = tempQueue.dequeue(); System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString()); } // Of for i
for (inti=0; i < 6; i++) { tempQueue.enqueue(i + 100); System.out.println("Enqueue, the queue is: " + tempQueue.toString()); } // Of for i }// Of main