Advanced Java Concepts Unit 2: Linked Lists. The List interface defines the structure of a linear collection. Here are some of its methods. boolean add( E element ) Appends the element to the end of the list void add( int index, E element ) Inserts the element at the specified index. 0 <= index <= size void clear Removes all elements from the list boolean contains( Object obj ) Returns true if the list contains the object E get( int index ) Returns the element at the index E remove( int index ) Removes the element at the index and returns it boolean remove( Object obj ) Removes the first occurrence of the object E set( int index, E element ) Replaces the element at specified index and returns the displaced element. int size() Returns the number of elements in the list. As you may remember from AP Computer Science, the ArrayList class implements the List interface. An array list object stores all the data into an array (by default the array size is ten). If the number of items in the list gets close to the array size, the object will automatically create a new, larger array and copy the old data into the new array. If later the number of items decreases then a new, smaller array is created and used. Linked Lists. A linked list is a dynamic data structure, growing and shrinking during runtime. In contrast, an array is a static data structure. Once an array is instantiated, its size does not change. A linked list consists of nodes. Here is an outline of a node class that we use. public class Node { public Object ; public Node ; public Node ; The fields are public to make the exercise problems easier to read. public Node( Object initvalue, Node Node, Node Node ){ = initvalue; = Node; HEY! We will use the term node in connection = Node; with other data structures. This particular Node class just applies to some types of linked lists. public String tostring(){ return "node " +.tostring(); The purpose of a node is to contain data and link to other nodes. A linked list is a collection of nodes. Page 1
Read over this problem then read the discussion that follows. Then start solving the problems. 1. This compiles and runs. What is displayed? 1 2 3 4 5 6 7 8 9 Node n1 = new Node( "rat",, ); Node n2 = new Node( "fish", n1, ); System.out.println( n1 ); System.out.println( n2 ); Node n3 = n2.; System.out.println( n3 ); Node n4 = n1.; System.out.println( n4 ); The first two lines create two variables, n1 and n2, and two Node objects. The arrows represent references. n1 rat n2 fish n1 and n2 contain references to their respective Node objects. The field of the second Node object also contains a reference to the first Node object. After the third line is executed, we now have: n1 rat n2 fish This picture of variables and field containing references to each other will help you answer these problems. 2. This compiles and runs. What is displayed? Node n1 = new Node( "B",, ); Node n2 = new Node( "X",, ); Node n3 = new Node( "W",, ); Node n4 = new Node( "E",, ); n2. = n4; n3. = n1; n1. = n2; n4. = n3; System.out.println( n1.. ); System.out.println( n1... ); System.out.println( n1... ); Page 2
3. Does this code print or does it throw a NullPointerException? Node n1 = new Node( "cat",, ); System.out.println( n1.. ); 4. This compiles but eventually throws a runtime exception. Before that happens, what is displayed? What line throws the exception? 1 2 3 4 5 6 7 8 9 10 Node node = new Node( 3,, ); node = new Node( 2.72, node, ); node = new Node( "swan", node, ); System.out.println( node.); 5. This compiles. What does it display? If it crashes, explain. If it is an endless loop then show what is displayed before it starts repeating. Node n1 = new Node( "bob",, ); Node n2 = new Node( "is",, n1 ); Node n3 = new Node( 16,, n2 ); n1. = n3; Node x = n1; while ( x!= ) { System.out.println( x. ); x = x.; 6. What is displayed? Node node = new Node( "boo",, ); node = new Node( n+"", node, ); { 7. What is displayed? Node node = new Node( "boo",, ); node = new Node( n+"", node, ); { 8. What is displayed? Node node = new Node( "hoo",, ); node = new Node( n+"",, node ); Page 3
9. What is displayed? Node n1 = new Node( "do",, ); Node n2 = new Node( "re",, ); Node n3 = new Node( "mi",, ); n1. = n3; n2. = n1; n3. = n2; Node node = n2; { 10. What is displayed? If there's a run-time error, why? 11. What is displayed? If there's a run-time error, what is displayed up to the error? 12. What is displayed? If there's a run-time error, what is displayed up to the error? Node n1 = new Node( "E",, ); Node n2 = new Node( "D",, n1 ); Node node = n2.; Node n1 = new Node( "U",, ); Node n2 = new Node( "P", n1, n1 ); Node node = n2.; Node n1 = new Node( 17,, ); Node n2 = new Node( 55,, ); Node n3 = new Node( 88,, ); n2. = n3; n2. = n1; n3. = n2; Node node = n1; while ( node!= && node.!= ){ while ( node!= && node.!= ){ Page 4
13. The first 3 lines make a simple linked list with A in the front and C at the end. In the blank lines, write the code that correctly inserts another Node, containing "B", between the other two nodes. You may not need all the blank lines 14. The first 5 lines make a simple linked list with X in the front, then Y, then Z at the end. In the blank lines, write the code that correctly deletes the middle node and leaves the X and Z still linked to each other. You may not need all the blank lines Node n1 = new Node( "A",, ); Node n2 = new Node( "C", n1, ); Node n1 = new Node( "X",, ); Node n2 = new Node( "Y", n1, ); Node n3 = new Node( "Z", n2, ); n2. = n3; While there are different types of linked lists, we will only cover one type a doubly linked list. A doubly linked lists consists of nodes which each have a link to the ious node and the node in the list. Here is the design that we will use for our doubly linked list. There is a head node. There is no node before it and it contains no data. It simply contains a reference to the start of the list. There is a tail node. There is no node after it and it contains no data. It simply contains a reference to the end of the list. Data nodes. These nodes contain data and have links to the nodes before and after them. Let s picture a node as containing three parts: a reference to a node before it, the data, and a reference to the node after it. ious node data node This figure shows what our linked list looks like when it is first created. head ref. to the tail ref. to the head tail The logical size of this linked list is zero because it only contains a head and a tail (which will never contain any data). Page 5
After a node is added to the linked list, it looks like this. data head index 0 tail The logical size of the linked list is now one and the node that was added is considered to be at index 0 with references to the head and tail of the list. A doubly linked list with a size of two looks like this: data data head index 0 index 1 tail ****************************************************************************** Inserting a Node in a Doubly Linked List. Suppose you have a list with 50 nodes and you want to insert a new index 14 (after the item at index 13 and before the item currently at index 14). 1. Start at the head and link to node in front of it. Have that node link to the one in front of it. Keep linking until you have reached the index 13. 2. Create a new node. Its ious node is the 13 and its node is the 14. 3. Change node 13 so that its node is the new node. Change node 14 so that its ious node is the new node. Step 2 Step 3 index 13 index 14 index 13 index 14 new node new node Note. The nodes in a linked list do not keep track of their index. Each node simply knows which node comes before it, which node comes after it, and the data it contains. Page 6
Deleting a Node from a Double Linked List. This works in a manner similar to deleting a node. 1. Find the node you want to delete. 2. Change the nodes before and after that node so that they refer to each other. 3. At this point nothing is referring to the deleted node and it will eventually be garbage collected. Step 1 Step 2 node to be deleted node to be deleted The List Interface: ArrayLists vs. LinkedLists method ArrayLists Linked Lists add( int index, E element ) The closer to the front of the list, the longer this process takes because elements must be moved to make room for the new. get( int index ) remove( int index ) This is a quick operation that is not effected by the size of the list. The closer to the front of the list, the longer this process takes because elements must be moved If the index is near the front (or end) of the list, this is a quick operation that is not effected by the size of the list. Starts at the front or end of the list and moves from node to node until reaching the index. If the index is near the front (or end) of the list, this is a quick operation that is not effected by the size of the list. As a general rule, people implement a List object using the ArrayList class because overall it is a faster implementation. However, in certain situations (such routinely inserting and removing from the front of a large list), LinkedList is the better choice. Page 7