Question: Using the generic capabilities of Java 5.0, modify the implementation of the structure SinglyLinkedList presented in Figure 4.15 to make it fully generic. Include a

Using the generic capabilities of Java 5.0, modify the implementation of the structure SinglyLinkedList presented in Figure 4.15 to make it fully generic. Include a driver program that demonstrates the functionality of the class with two homogeneous SinglyLinkedList objects that store two different kinds of nodes. This is the code of Figure 4.15: public class SinglyLinkedList { private Node h; // list header public SinglyLinkedList() { h = new Node(); // dummy node h.l = null; h.next = null; } public boolean insert(Listing newListing) { Node n = new Node(); if(n == null) // out of memory return false; else { n.next = h.next; h.next = n; n.l = newListing.deepCopy(); return true; } } public Listing fetch(String targetKey) { Node p = h.next; while (p != null && !(p.l.compareTo(targetKey) == 0)) { p = p.next; } if(p != null) return p.l.deepCopy(); else return null; } public boolean delete(String targetKey) { Node q = h; Node p = h.next; while (p != null && !(p.l.compareTo(targetKey) == 0)) { q = p; p = p.next; } if(p != null) { q.next = p.next; return true; } else return false; } public boolean update(String targetKey, Listing newListing) { if(delete(targetKey) == false) return false; else if(insert(newListing) == false) return false; return true; } public void showAll() { Node p = h.next; while (p != null) //continue to traverse the list { System.out.println(p.l.toString( )); p = p.next; } } public class Node { private Listing l; private Node next; public Node() { } }// end of inner class Node }//end SinglyLinkedList outer class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!