Enhanced LinkedList Class Using Java

As you ve seen throughout this book, the LinkedList class is defined in the java.util package and defines member methods that are similar in functionality to member functions that were defined in the C++ example of this chapter.

Following is the Java version of the C++ application described in the previous section. Both versions produce the same results. However, I ve defined a printList() method in the Java example that displays the linked list at various times during the application.

Let s walk through the Java version of the application to see how it works. The application begins similar to the way the C++ begins in that it declares an instance of the LinkedList class and assigns it to a reference called list. The add() method is then called fives times to add nodes to the linked list. This is similar to the appendNode() function in the C++ version of the application. The linked list is then displayed on-screen by calling the printList() method defined later in the application. Here s what is displayed on-screen:

 Initial List: 10 20 30 40 50 

Next , the remove() method is called and is passed the index of the node that we want removed from the linked list. This is similar to the removeNodeAt() function in the C++ version. Again, we call the printList() method to show the results of calling the remove(3) method. Here s the display:

 Removed index 3: 10 20 30 50 

The indexOf() method is called next to return the index of the node that contains 20 as its data value. This is the same as the findNode() in the C++ version. The indexOf() method requires an object rather than the data. In this example, the data is an integer, therefore we need to declare an instance of the Integer wrapper class and initialize the instance with the value 20. The declaration occurs within the parameter of the indexOf() method. The indexOf() method returns the index value that is assigned to the index integer. If you pass in your own class, you may need to implement a Comparator so the Java collection can determine equality on the different Objects. The Comparator is similar to overloading the quality operator in C++. The value of the index variable is then displayed on-screen as follows :

 Index of value 20: 1 

Once again the remove() method is called. However, this time the remove() method is passed data instead of an index. This causes the remove() method to remove the node from the linked list that contains 20 as its data value. This is similar to the deleteNode() method shown in the C++ version. The printList() method is called again to show how the linked list looks after the remove() method is called. Here s what is displayed on-screen:

 Removed value 20: 10 30 50 

Next, the application inserts a new node at index position 1 and assigns the new node 35 as its value. Java doesn t have an insertNodeAt() method like the one you created in the C++ version. However, the add() method provides the same functionality as long as you specify the index position at which you want to insert the new node and provide it the data to store in the node. The printList() method is called once again. Here s the linked list following the execution of the add() method:

 Added 35 at index 1: 10 35 30 50 

The LinkedList class in Java doesn t have a peek() method, but the get() method is used for the same purpose. In this example, the get() method is passed the index value 3 and returns the data stored in the node located at the third index position on the linked list. Next, get() returns an object, which is cast to an integer. The data variable is then displayed on-screen. Here s what appears on the display:

 Value at index 3: 50 

The application then calls the size () method, which returns the size of the linked list similar to the getSize() function defined in the C++ example. The size() method returns an integer that is assigned the size variable, which is then displayed on-screen. Here s what is displayed:

 Size of linked list: 4 import java.io.*; import java.util.*; public class demo  {  public static void main(String[] args)  {  LinkedList list = new LinkedList();  list.add(new Integer(10));  list.add(new Integer(20));  list.add(new Integer(30));  list.add(new Integer(40));  list.add(new Integer(50));  printList("Initial List", list);  list.remove(3);  printList("Removed index 3", list);  int index = list.indexOf(new Integer(20));  System.out.println("\nIndex of value 20: " + index);  list.remove(new Integer(20));  printList("Removed value 20", list);  list.add(1, new Integer(35));  printList("Added 35 at index 1", list);  Integer data = (Integer)list.get(3);  System.out.println("\nValue at index 3: " + data);  int size = list.size();  System.out.println("\nSize of linked list: " + size);  }  public static void printList(String header, LinkedList list)  {  System.out.println("\n" + header + ":");  for(int i=0; i<list.size(); i++)  {  Integer temp = (Integer)list.get(i);  System.out.println(temp);  }  } } 

Data Structures Demystified
Data Structures Demystified (Demystified)
ISBN: 0072253592
EAN: 2147483647
Year: 2006
Pages: 90

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net