WEEK13: linked lists
---------------------------------------------------------------
 F: LinkedList class (again...)


QUIZ 6


LAST TIME:

from node import *

class LinkedList(object):
  """linked list class"""

  def __init__(self):
    """constructor for linked list class. creates empty list"""
    self.head = None
    self.tail = None
    self.size = 0

######################################################

  def __str__(self):
    s = "head--"
    current = self.head
    while current != None:
      s = s + str(current.getData()) + "--"
      current = current.getNext()
    s = s + "tail"

    return s

######################################################

  def append(self, data):
    """create new node and add it to end of linked-list"""
    newnode = Node(data)
    if self.size == 0:
      self.head = newnode
      self.tail = newnode
      self.size = 1
    else:
      self.tail.setNext(newnode)
      self.tail = newnode
      self.size = self.size + 1

######################################################

  def getSize(self):
     """getter for size of the list"""
     return self.size

######################################################


 - NOTE how the __str__ method does a *traversal* of the entire list

 - also NOTE how append needs to deal with the special case of
   adding a node when the list is empty (must set self.head, too)

 - what happens when you try to use len on a linked list, like this:

LL = LinkedList()
LL.append("jeff")
LL.append("pony")
print len(LL)

 - what happens when you rename the getSize method to __len__, then
   try the above code (print len(LL))??!


YOUR TURN:

 - add a getitem(i) method that gets the data item stored at node i

 - try renaming getitem() to __getitem__(), then print L[1]

 - add a deleteTail() method to the above class, and write some test
   code to make sure it works (make sure it works on an empty list, as
   well as a list with 1 or more nodes in it)