representing a linked list
play

Representing a Linked List 1 9 60 -3 Node List 2 types of - PowerPoint PPT Presentation

Representing a Linked List 1 9 60 -3 Node List 2 types of information: List as a whole: head, tail, numElements Nodes in the list: value, next node 1 add(int index, E value) public void add(int index, E value) { if (index == 0) {


  1. Representing a Linked List 1 9 60 -3 Node List 2 types of information: List as a whole: head, tail, numElements Nodes in the list: value, next node 1 add(int index, E value) public void add(int index, E value) { if (index == 0) { addFirst (value); Cost? } else { O(n) Node<E> prevNode = getNode(index-1); addAfter (prevNode, value); } } 2 LinkedList Performance (so far) LinkedList ArrayList Constructor O(1) O(capacity) getNode(int position) O(n) O(1) addFirst (E value) O(1) O(n) O(1) N/A addAfter (Node<E> node, E value) add (int index, E value) O(n) O(n) 3 Wednesday, April 3, 13

  2. removeFirst() tail 1 9 60 -3 head tail removeFirst(); head 9 60 -3 4 removeAfter (Node<E> node) n tail head 1 9 60 -3 tail removeAfter(n); 1 head 9 -3 5 remove (int index) 6 Wednesday, April 3, 13

  3. One of the top 10 reasons to major in CS http:/ /imgs.xkcd.com/comics/forgetting.png 7 addLast(int value) tail tail head 1 9 60 head 1 9 60 -3 8 void removeLast() { tail head 1 9 60 -3 9 Wednesday, April 3, 13

  4. LinkedList is Asymmetrical! insert at front O(1) at end O(1) insert after O(1) before O(n) remove at front O(1) at end O(n) remove after O(1) before O(n) remove node O(n) Solution: Doubly-linked lists 10 Doubly Linked List Node Represent values in “nodes” Each node contains: A value A reference to the next node A reference to the previous node 1 9 60 -3 11 Node private static class Node<E> { � public Node<E> (E value) {…} � public void setNext(Node<E> nextNode) {…} � public Node<E> getNext() {…} � public void setPrev(Node<E> prevNode) {…} � public Node<E> getPrev() {…} public E getValue() {…} public void setValue (E newValue) {…} � private E data; � private Node<E> next; private Node<E> prev; }; 12 Wednesday, April 3, 13

  5. Inserting a Node 1 9 -3 head tail 1. Create new node with references to nodes in list head 1 9 -3 tail 6 2. Update references to the new node. head 1 9 -3 tail 6 13 addAfter(Node<E> node, E value) n head 1 9 -3 tail tail addAfter(n, 6); head 1 9 6 -3 14 remove(Node<E> node) n head 1 9 6 -3 remove (n); tail head 1 9 -3 tail 15 Wednesday, April 3, 13

  6. Doubly LinkedList is Symmetrical! insert at front O(1) at end O(1) insert after O(1) before O(1) remove at front O(1) at end O(1) remove after O(1) before O(1) remove node O(1) walk forward O(n) backward O(n) 16 Wednesday, April 3, 13

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend