// CS210 - Algorithms and Data Structures I // Department of Computer Science // National University of Ireland, Maynooth // // Solutions to Laboratory Sheet 9 // Tom Naughton // November 2000 // //--------------------------------------------------------- //http://www.cs.may.ie/~tnaughton/cs210/ //========================================================= // This outlines a class to implement a linked list queue ADT. // It shows how to update Node.java, MyLinkedList.java, and // use the solution to the previous problem (MyLinkListStack). // // Firstly, add the following method to the class Node // public void setNext(Node n) { /* Set the value of the next reference. */ next = n; } // // Then, add the following method to the class MyLinkedList // public void addToBack(Object o) { /* Create a new node at the rear of the list. */ Node temp; // newly createed node reference Node t; // reference to traverse the list temp = new Node(o); if (head == null) { head = temp; } else { t = head; while (t.getNext() != null) { t = t.getNext(); } // now t refers to the last node of the list t.setNext(temp); } } // // Finally, create a new class called MyLinkedListQueue that // is identical to MyLinkedListStack, except // // it is called MyLinkedListQueue // a few comments will change // "pop()" is renamed "dequeue()" // "push()" is replaced by the following: public void enqueue(Object o) { /* Add a new object to the queue. */ list.addToBack(o); }