Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1030 Section 8 List.

Similar presentations


Presentation on theme: "COSC 1030 Section 8 List."— Presentation transcript:

1 COSC 1030 Section 8 List

2 Topics List ADT Non Linear Lists Generic List Circular List
Double Linked List Generic List

3 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); }

4 Circular List removedNode General Case Single Node Case lastNode aNode
Insert empty case general case Remove single node case lastNode Empty Case lastNode

5 void insertAtLast(CListNode newNode) {
if(lastNode == null) { newNode.link = newNode; } else { newNode.link = lastNode.link; lastNode.link = newNode; } lastNode = newNode; // update lastNode

6 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; }

7 Helper method private CListNode findPreviousNode() {
assert(lastNode != null); CListNode previousNode = lastNode; while(previousNode.link != lastNode) { previousNode = previousNode.link; } assert(previousNode.link == lastNode); return previousNode;

8 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; }

9 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

10 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

11 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;

12 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

13 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(); }

14 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();

15 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()?


Download ppt "COSC 1030 Section 8 List."

Similar presentations


Ads by Google