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

representing a linked list
SMART_READER_LITE
LIVE PREVIEW

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) {


slide-1
SLIDE 1

Representing a Linked List

2 types of information: List as a whole: head, tail, numElements Nodes in the list: value, next node

List Node

1

  • 3

60 9

public void add(int index, E value) { if (index == 0) { addFirst (value); } else { Node<E> prevNode = getNode(index-1); addAfter (prevNode, value); } }

add(int index, E value)

O(n) Cost?

LinkedList Performance (so far)

LinkedList ArrayList Constructor getNode(int position) addFirst (E value)

addAfter (Node<E> node, E value)

add (int index, E value) O(1) O(capacity) O(n) O(1) O(1) O(n) O(1) N/A O(n) O(n)

1 2 3 Wednesday, April 3, 13

slide-2
SLIDE 2

removeFirst()

1

  • 3

60 9

head

  • 3

60 9

head removeFirst(); tail tail

removeAfter (Node<E> node)

1

  • 3

60 9

head

  • 3

9

head removeAfter(n); n

1

tail tail

remove (int index)

4 5 6 Wednesday, April 3, 13

slide-3
SLIDE 3

http:/ /imgs.xkcd.com/comics/forgetting.png

One of the top 10 reasons to major in CS addLast(int value)

1 60 9

head tail

1

  • 3

60 9

head tail

1

  • 3

60 9

head tail void removeLast() {

7 8 9 Wednesday, April 3, 13

slide-4
SLIDE 4

LinkedList is Asymmetrical!

insert insert remove remove remove

at front O(1) at end O(1) after O(1) before O(n) at front O(1) at end O(n) after O(1) before O(n) node O(n)

Solution: Doubly-linked lists

Represent values in “nodes” Each node contains: A value A reference to the next node A reference to the previous node

Doubly Linked List Node

1

  • 3

60 9

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

10 11 12 Wednesday, April 3, 13

slide-5
SLIDE 5

Inserting a Node

  • 3

9

head

1

  • 1. Create new node with references to nodes in list
  • 2. Update references to the new node.

tail

6

  • 3

9

head

1

tail

6

  • 3

9

head

1

tail

addAfter(Node<E> node, E value)

1

  • 3

6 9

head

  • 3

9

head addAfter(n, 6); n

1

tail tail

remove(Node<E> node)

remove (n); n

1

  • 3

6 9

head

  • 3

9

head

1

tail tail

13 14 15 Wednesday, April 3, 13

slide-6
SLIDE 6

Doubly LinkedList is Symmetrical!

insert insert remove remove remove walk

at front O(1) at end O(1) after O(1) before O(1) at front O(1) at end O(1) after O(1) before O(1) node O(1) forward O(n) backward O(n)

16 Wednesday, April 3, 13