Exam: One Week from Today
We spoke very briefly about the upcoming exam. It will be a "pen and paper" exam. And, it will be closed book.The exam will emphasize the following:
- Vectors, in particular, their applications, performance characteristics their most basic methods
- Singly linked lists, in particular their applications, performance characteristics and most common behaviors. Additionally, the implementation of internal list and node operations is likely going to be a big part of the exam. We'll likely ask about simple things, such as filters (getting values) and more complicated things, such as merges, splits, removes, insertions, &c
- Doubly linked lists (next class), in particular their applications, performance characteristics and most common behaviors. Additionally, the implementation of internal list and node operations is likely going to be a big part of the exam. We'll likely ask about simple things, such as filters (getting values) and more complicated things, such as merges, splits, removes, insertions, &c (sound familiar? If it can be done with a singly-linked list...)
- Also, I'm fond of drawing problems. Show the list after blah. Draw the list, including each reference change, as blah occurs.
- Another category of questions, although somewhat limited by nature on this exam, is the "comparison question". Selecting the right data structure for the job, comparing costs and benefits of each, &c.
Today's Class
During today's class, we extended the LinkedList class from last class to include more complicted methods, focusing on those that don't necessarily just operate on the head or tail.
The LinkedList Implementation
public class LinkedList { private LinkedListNode head; private LinkedListNode tail; private LinkedListNode index; public class LinkedListException extends Exception { public LinkedListException (String msg) { super(msg); } } public LinkedList() { head = tail = index = null; } public void prepend (Comparable data) { head = new LinkedListNode (data, head); if (null == tail) tail = head; if (null == index) index = head; } public void append (Comparable data) { if (null == tail) head = index = tail = new LinkedListNode(data); else { tail.setNext (new LinkedListNode (data)); tail = tail.getNext(); } } public void resetIndex() { index = head; } public Comparable getIndex() throws LinkedListException { if (null == index) throw new LinkedListException ("Null index in getIndex()"); return index.getData(); } public void advanceIndex() throws LinkedListException { if ((index == null) || (null == index.getNext())) throw new LinkedListException ("Null index in advanceIndex()"); index = index.getNext(); } // This is gonna' be paiiiinnnnful public void reverseIndex() throws LinkedListException { LinkedListNode temp = index; // Mark index's old position if ( (null == index) || (index == head) ) throw new LinkedListException ("Index is null or has no predecessor"); resetIndex(); while (index.getNext() != temp) index = index.getNext(); // Index stopped one before old position -- so done! } public Comparable deleteAtIndex() throws LinkedListException { Comparable data; try { data = index.getData(); reverseIndex(); // data = index.getNext().getData(); // Ugly, but avoids NPE index.setNext(index.getNext().getNext()); } catch (NullPointerException npe) { throw new LinkedListException ("index is null; can't delete"); } catch (LinkedListException lle) { // No predecessor; first node in list head = index.getNext(); } // Index was the tail if (index.getNext() == null) { tail = index; } // Move index to the one after the one we're deleting index = index.getNext(); return data; } public Comparable removeNth (int n) // beginning with 0th throws LinkedListException { LinkedListNode index; // Don't destroy user's index int count; if (null == head) throw new LinkedListException ("Can't delete from empty list"); if (n == 0) { if (tail == head) tail = null; if (index == head) index = null; head = head.getNext(); } else { for (index=head, count=0; count < n; count++, index=index.getNext()) ; if (this.index == index.getNext()) this.index = null; if (tail == index.getNext()) tail = index; index.setNext(index.getNext().getNext()); } } } /* LinkedList */