Topic > Understanding Linked Lists - 1028

Understanding Linked Lists Introduction: It can be tedious to work with arrays in Java because they require a certain amount of memory to be allocated upon creation and do not allow for easy removal or insertion of elements. A linked list is a simple solution to the shortcomings of arrays. In this assignment, you will create the LinkedSimpleCollection and LinkedSimpleIterator classes that implement the SimpleCollection and SimpleIterator interfaces, respectively. By completing this task, you will learn how a linked collection works and what its advantages and disadvantages are. Part 1: Nodes To create the LinkedSimpleCollection class, you will first create the Node class. Each node contains an element and a pointer to another node. Nodes, when combined, form the structure of a linked list. A Node element can be of any type, so the Node class must be generic. The Node class has no methods, but the Node class constructor should take two arguments: the generic type element and a pointer to another Node that we will call later. By creating nodes with a pointer to another node, you are setting the basis for a LinkedSimpleCollection. You can test the Node class with the following DrJava interactions:> Node n1 = new Node(“Foo”,null)> n1.element“ Foo”> n1.nextnull> Node n2 = new Node(“Bar”,n1)> n2 .element“Bar”> n2.next.element"Foo"Part 2: Creating a Linked List Keeping track of linked nodes manually can be difficult, so we need a class that will link these nodes automatically. In this part of the assignment you will create the LinkedSimpleCollection class. This class has a constructor and must satisfy its contract with SimpleCollec......middle of the paper......the LinkedSimpleCollection class. This can be done by creating a new LinkedSimpleIterator in the iterator method of LinkedSimpleCollection and passing it to the constructor. You can test the LinkedSimpleIterator class with the following DrJava interactions:> LinkedSimpleCollection c = new LinkedSimpleCollection ()> c.add("Four" )True> c.add(“Three”)True> c.add(“Two”)True> c.add(“One”)True> SimpleIterator Iterc= c.iterator()> Iterc.hasNext()True> Iterc.next()“One”> Iterc.next()“Two”> Iterc.hasNext()True > Iterc.remove()> Iterc.next()“Four”> Iterc.hasNext()FalseYou can now iterate through a list link, but note that after scrolling through the list it is impossible to go back without creating a new iterator of the same list. This flaw is one of the shortcomings of one-way linked lists.