Day17 链队列

1、又忘了写构造器,构造器一般写在属性和方法中间,构造器里面的程序在构造器被调用时,就会执行(新建对象时)。这里的内部类包含了一个数据 int data ,一个用来链接下一个结点的 Node next ,还有就是一个构造器;

2、队列是只能在表的一端进行插入,另一端进行删除的数据结构,链队列就是链式存储方式的队列;

3、enqueue(int paraValue) 方法里,为了保证每次加入的点都是尾结点,所以让 tail 结点指向新加入的结点,此时尾结点为新加入的结点,再令 tail 为新加入的结点,这样比指针好理解;

4、dequeue() 方法返回的 int 是出列的结点的值,头结点是方便操作的,他不存东西,并且需要单独考虑出列后队列变成空的情况。

  1 public class LinkedQueue {
  2     /**
  3      * An inner class.
  4      */
  5     class Node {
  6         /**
  7          * The data.
  8          */
  9         int data;
 10 
 11         /**
 12          * The reference to the next node.
 13          */
 14         Node next;
 15 
 16         /**
 17          * 
 18          * The constructor.
 19          * 
 20          * @param paraValue The data.
 21          */
 22         public Node(int paraValue) {
 23             data = paraValue;
 24             next = null;
 25         }// Of the constructor
 26     }// Of class Node
 27 
 28     /**
 29      * The header of the queue.
 30      */
 31     Node header;
 32 
 33     /**
 34      * The tail of the queue.
 35      */
 36     Node tail;
 37 
 38     /**
 39      * Construct an empty sequential list.
 40      */
 41     public LinkedQueue() {
 42         header = new Node(-1);
 43         tail = header;
 44     }// Of the first constructor
 45 
 46     /**
 47      * 
 48      * Enqueue
 49      * 
 50      * @param paraValue The value of the new node.
 51      */
 52     public void enqueue(int paraValue) {
 53         Node tempNode = new Node(paraValue);
 54         tail.next = tempNode;
 55         tail = tempNode;
 56     }// Of enqueue
 57 
 58     /**
 59      * Dequeue
 60      * 
 61      * @return The value at the header.
 62      */
 63     public int dequeue() {
 64         if (header == tail) {
 65             System.out.println("No element in the queue");
 66             return -1;
 67         } // Of if
 68         int resultValue = header.next.data;
 69 
 70         header.next = header.next.next;
 71 
 72         // The queue becomes empty.
 73         if (header.next == null) {
 74             tail = header;
 75         } // Of if
 76 
 77         return resultValue;
 78     }// Of dequeue
 79 
 80     public String toString() {
 81         String resultString = "";
 82 
 83         if (header.next == null) {
 84             return "empty";
 85         } // Of if
 86 
 87         Node tempNode = header.next;
 88         while (tempNode != null) {
 89             resultString += tempNode.data + ", ";
 90             tempNode = tempNode.next;
 91         } // Of while
 92 
 93         return resultString;
 94     }// Of toString
 95 
 96     /**
 97      * 
 98      * The entrance of the program.
 99      * 
100      * @param args
101      */
102     public static void main(String args[]) {
103         LinkedQueue tempQueue = new LinkedQueue();
104         System.out.println("Initialized, the list is: " + tempQueue.toString());
105 
106         for (int i = 0; i < 5; i++) {
107             tempQueue.enqueue(1 + i);
108         }// Of for i
109         System.out.println("Enqueue, the queue is: " + tempQueue.toString());
110 
111         tempQueue.dequeue();
112         System.out.println("Dequeue, the queue is: " + tempQueue.toString());
113 
114         int tempValue;
115         for (int i = 0; i < 5; i++) {
116             tempValue = tempQueue.dequeue();
117             System.out.println("Looped delete " + tempValue + ", the new queue is: " + tempQueue.toString());
118         }// Of for i
119 
120         for (int i = 0; i < 3; i++) {
121             tempQueue.enqueue(i + 10);
122         }// Of for i
123         System.out.println("Enqueue, the queue is: " + tempQueue.toString());
124     }// Of main
125 } // Of class LinkedQueue

 

posted @ 2022-05-04 23:35  滑稽的炒饭  阅读(46)  评论(0)    收藏  举报