COSC 1030 Section 8 List
Topics List ADT Non Linear Lists Generic List Circular List Double Linked List Generic List
List ADT interface ListSpec { // constructor – create an empty list boolean isEmpty(); void insert(Item anItem); Item search(Object aKey); boolean delete(Object aKey); int length(); // or size() or count() Item getItem(int ith); }
Circular List removedNode General Case Single Node Case lastNode aNode Insert empty case general case Remove single node case lastNode Empty Case lastNode
void insertAtLast(CListNode newNode) { if(lastNode == null) { newNode.link = newNode; } else { newNode.link = lastNode.link; lastNode.link = newNode; } lastNode = newNode; // update lastNode
CListNode removeLastNode() { CListNode tempNode = null; if(lastNode!= null) { CListNode previousNode = findPreviousNode(); tempNode = lastNode; if (previousNode != lastNode) { // generic case previousNode.link = lastNode.link; lastNode = lastNode.link; } else { // single node case lastNode = null; } // end if tempNode.link = null; // break link to the list } // end if return tempNode; }
Helper method private CListNode findPreviousNode() { assert(lastNode != null); CListNode previousNode = lastNode; while(previousNode.link != lastNode) { previousNode = previousNode.link; } assert(previousNode.link == lastNode); return previousNode;
search return null; public CListNode search(int key) { CListNode temp = lastNode; if(lastNode == null) return null; do { if(temp.key == key) return temp; temp = temp.link; } while(temp.next != lastNode); return null; }
Double Linked List General Case firstNode Insert at first empty case general case Search generic case Remove single node case Single Node Case firstNode Empty Case firstNode
Double Linked List - Insert General Case firstNode Insert at first empty case firstNode = newNode; General case newNode.next = firstNode; firstNode.previous = newNode; Empty Case firstNode
Double Linked List - remove firstNode theNode general case theNode.next.previous = theNode.previous; theNode.previous.next = theNode.next; last node case theNode.previous.next = null; first node case theNode.next.previous = null; firstNode = theNode.next; single node case – firstNode = null;
Generic List A list contains lists as its nodes Lisp notation of an expression (1, 2, plus) 1 + 2 ((1, x, plus), (x, x, plus), multiple) (1 + x) * (x + x) List List multiple firstNode 1 x plus x x plus
class ListNode { Object item; ListNode link; String toString() { // It will call GenList’s toString // if the actual type of the item is GenList return item.toString(); }
class GenLisl { private ListNode firstNode; public void insert(Object newItem) { ListNode aNode = new ListNode(newItem); aNode.link = firstNode; firstNode = aNode; } public String toString() { StringBuffer strBuff = new StringBuffer(); strBuff.append(‘(‘); ListNode current = firstNode; while(current != null) { strBuff.append(current.toString()); current = current.next; if(current != null) strBuff .append(‘,‘); strBuff .append(‘)‘); return strBuff .toString();
class GenList { private ListNode firstNode; …… public int count() { int n = 0; ListNode current = firstNode; while(current != null) { // recursive call if the item is a list if (current.item instanceof ListNode) { n += ((ListNode) current.item).count(); } else { n ++; } current = current.next; return n; What is the complexity class of print() and count()?