Thursday, September 25, 2014

Changes 9/25/14

Java:

  • Worked more on improving the LList class
  • Added a size() variable, so you can get the size of the LList
  • Each time an element is added to the array, the size is increased
  • Currently there is no way to remove elements, so the size can never decrease
  • With this size() variable, the class can also do bounds checking.  For example, with the code LList<String> llist = new LList<String>("sudo", "rm", "-rf");, calling llist.get(5); would result in Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5 out of bounds for <LList elements=[sudo, rm, -rfl]>
  • As you can see from this example, the LList.toString() method prints out the elements it contains.  This uses a StringBuilder object to avoid the inefficiencies of adding strings together with the + operator (such as "abc" + "def").  Instead, you use the append() method of StringBuilder, then convert the StringBuilder to a string when you want to use it as one.
  • I think that the LList structure is really interesting.  The fact that you can have infinitely sized lists with no penalty for adding or removing elements is really cool, but I can see the drawbacks as well.  For example, if something happens and one Node gets unlinked, all the other Nodes after it will get unlinked as well.
  • Tomorrow I will work on implementing a remove() method for elements, and finish implementing a LList.toArray() method, which will return an array of the data stored in the LList

2 comments:

  1. I'm confused. How can I see the LList class's toString() method?

    The problem you mention is with potential data loss is not usually considered as a drawback to linked lists, since a well implemented, well tested LList class (like the one you hopefully are implementing ;-) can prevent that from happening.

    More to the point is the overhead involved in creating the node structures needed. A
    linked list of one byte (8 bit) characters, for example, would still require a 64 bit next
    field in each node.

    ReplyDelete
  2. Sorry, the LList.toString method is called in the body of the Exception. It's the . This is important because it means the toString() method has to traverse the list and record the data field for each Node.

    ReplyDelete