Download Chain Data Structure Implementation in Java: Class, Constructors, Methods, and Performance and more Slides Computer Science in PDF only on Docsity! Seeeee&
» * B BB BB B B® A
eo The Class Chain ®
» *% BB BB B B B®
ge &©Seeeee
The Class Chain next (datatype ChainNode) element (datatype Object) Use ChainNode a b c d e null firstNode size = number of elements Docsity.com The Method isEmpty /** @return true iff list is empty */ public boolean isEmpty() {return size == 0;} Docsity.com The Method size() /** @return current number of elements in list */ public int size() {return size;} Docsity.com The Method checkIndex /** @throws IndexOutOfBoundsException when * index is not between 0 and size - 1 */ void checkIndex(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException ("index = " + index + " size = " + size); } Docsity.com The Method indexOf // make sure we found matching element if (currentNode == null) return -1; else return index; } Docsity.com Removing An Element remove(0) firstNode = firstNode.next; a b c d e null firstNode Docsity.com Remove An Element public Object remove(int index) { checkIndex(index); Object removedElement; if (index == 0) // remove first node { removedElement = firstNode.element; firstNode = firstNode.next; } Docsity.com One-Step add(0,’f’) a b c d e null firstNode f newNode firstNode = new ChainNode(‘f’, firstNode); Docsity.com Add An Element public void add(int index, Object theElement) { if (index < 0 || index > size) // invalid list position throw new IndexOutOfBoundsException ("index = " + index + " size = " + size); if (index == 0) // insert at front firstNode = new ChainNode(theElement, firstNode); Docsity.com Two-Step add(3,’f’) beforeNode = firstNode.next.next; beforeNode.next = new ChainNode(‘f’, beforeNode.next); a b c d e null firstNode f newNode beforeNode Docsity.com Performance 40,000 operations of each type Operation FastArrayLinearList Chain get 5.6ms 157sec best-case adds 31.2ms 304ms average adds 5.8sec 115sec worst-case adds 11.8sec 157sec best-case removes 8.6ms 13.2ms average removes 5.8sec 149sec worst-case removes 11.7sec 157sec Docsity.com Performance Indexed AVL Tree (IAVL) Docsity.com Performance Indexed AVL Tree (IAVL) Operation FastArrayLinearList Chain IAVL get 5.6ms 157sec 63ms best-case adds 31.2ms 304ms 253ms average adds 5.8sec 115sec 392ms worst-case adds 11.8sec 157sec 544ms best-case removes 8.6ms 13.2ms 1.3sec average removes 5.8sec 149sec 1.5sec worst-case removes 11.7sec 157sec 1.6sec Docsity.com Circular List a b c d e firstNode Docsity.com Doubly Linked List a b c d e null firstNode null lastNode Docsity.com Doubly Linked Circular List a b c d e firstNode Docsity.com