CS201 - Spring 2014 - Class 18

  • look at exercises for the day

  • look at StringBufferExample code

  • StringBuffer vs. StringBuilder

  • The general ArrayList class implements the List interface (http://docs.oracle.com/javase/7/docs/api/java/util/List.html)
       - Which of the following methods are performed quickly by the ArrayList class (quickly being sub-linear)? Slowly?
          - add to the end
          - contains
          - get
          - insert at an index
          - remove an element
          - set the value of an existing element
          - size
       - Quick:
          - add to the end (like we saw last time)
          - get
          - set
          - size
       - Slow:
          - inserting an element at a particular index
             - have to shift everything after towards the end
          - removing an element
             - have to remove and shift everything after towards the front
          - contains
             - have to iterate over every item

  • No free lunch!
       - there's no one best data structure
          - to make certain operations fast we often have to compromise and make other operations slower
       - depending on what operations are important, one data structure may be more appropriate than another
       - we're going to start looking at different data structures that are good at different things

  • Linked lists
       - linked lists are a recursive data structure
          - the data structure is built out of nodes
          - a node stores both a data item as well as a reference to another node
          - draw a picture and compare and contrast a picture of an array (or a Vector)
       - how could we do this using a class?
       - look at the IntNode class in IntLinkedList code
          - two things that we keep track of (with private instance variables)
             - private int data: the data that we're actually storing in the node
             - private IntNode next: a link to the next node in our linked list
          - we have methods that allow us to access and manipulate the data within the node
          - as well as methods that allow us to traverse and manipulate the links

       - how could you build a linked list with N data items?
          - start by creating node (node1) with some data
          - create a new node (node2) with some other data and point it's next element at node1
          - create a new node (nod3) with some more data and point it's next element at nod2
          - etc, up through nodeN
          - in the end, we'll have nodeN->nodeN-1->...->node3->node2->node1

       - if someone gave us the first node, how would we traverse the linked list?
          IntNode finger = head;

          while( finger != null ){
             finger = finger.next();
          }

       - We want to build a linked list data structure that supports similar operations to ArrayList. How would we do it?
          - we'll just store a link the head of the linked list
          - all of our operations will start at the head and then modify/traverse the list appropriately

       - A few operations (look at IntLinkedList code)
          - public void addFirst(int value)
             - we need to associate head with a new value
             - and set the "next" element for the new head to be the old head
             - this is accomplished at the same time, using the Node constructor

          - public int removeFirst()
             - save the value of head so we can return it
             - set head to be head.next()
             - return the value of the original head

          - public boolean contains(int value)
             - start at the head and traverse through the nodes
             - we can just keep a IntNode reference and update it with .next() to move to the next node
             - stop when we either find it or we run off the end