New to the site. Trying to implement a generic SinglyLinkedList, fetch returns null even though there are nodes inserted and delete method return false, when true is expected. In addition, it functions just fine when I decide to fetch or delete in reverse order. Looking for a set of fresh eyes to see what I am missing. Thanks-in-advance.
public class MainSinglyLinkedList {
public static void main(String[] args) {
SinglyLinkedList <Listing> boston = new SinglyLinkedList<Listing>();
Listing l1 = new Listing("Bill", "1st Avenue", "123 4567");
Listing l2 = new Listing("Al", "2nd Avenue", "456 3232");
Listing l3 = new Listing("Mike", "3rd Avenue", "333 3333");
boston.insert(l1); // test insert
boston.insert(l2);
boston.insert(l3);
boston.showAll();
System.out.println("***********************************************");
System.out.println(boston.fetch("Mike").toString());
//boston.delete("Al"); // test delete of Al
System.out.println( boston.delete("Bill"));
//System.out.println( boston.delete("Mike"));
//System.out.println( boston.delete("Al"));
//System.out.println( boston.delete("Bill"));
//System.out.println("-----------------------------------------------");
boston.showAll();
//boston.update("Mike", l2); // test update of Mike to Al
//System.out.println(boston.update("Mike", l2));
//boston.showAll();
SinglyLinkedList <StudentListing> Form4East = new SinglyLinkedList<StudentListing>();
StudentListing bill = new StudentListing("Bill", "2988050", "3.9");
StudentListing mary = new StudentListing("Mary", "2988051", "3.5");
StudentListing tom = new StudentListing("Tom", "2988052", "1.7");
System.out.println(Form4East.insert(bill));
System.out.println(Form4East.insert(mary));
System.out.println(Form4East.insert(tom));
System.out.println(Form4East.fetch("Tom"));
System.out.println(Form4East.fetch("Bill"));
System.out.println(Form4East.fetch("Mary"));
System.out.println(Form4East.fetch("Bill"));
System.exit(0);
}
}
public class SinglyLinkedList<T> {
private Node<T> h; // list header
public SinglyLinkedList() {
h = new <T> Node(); // dummy node
h.l = null;
h.next = null;
}
public boolean insert(T newNode) {
Node n = new Node();
GenericNode node = (GenericNode) newNode;
if (node == null) // out of memory
{
return false;
} else {
n.next = h.next;
h.next = n;
n.l = (T) node.deepCopy();
return true;
}
}
public GenericNode fetch(Object targetKey) {
Node p = h.next;
GenericNode node = (GenericNode) p.l;
while (p != null && !(node.compareTo(targetKey) == 0)) {
p = p.next;
}
if (p != null) {
return node.deepCopy();
} else {
return null;
}
}
public boolean delete(Object targetKey) {
Node q = h;
Node p = h.next;
GenericNode node = (GenericNode)p.l;
while (p != null && !(node.compareTo(targetKey) == 0)) {
q = p;
p = p.next;
}
if (p != null) {
q.next = p.next;
return true;
} else {
return false;
}
}
public boolean update(Object targetKey, T newNode) {
if (delete(targetKey) == false) {
return false;
} else if (insert(newNode) == 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;
}
}
/**
*
* @param <T>
*/
public class Node <T> {
private T l;
private Node <T> next;
public <T> Node() {
}
}// end of inner class Node
}
public class Listing implements GenericNode{
private String name; // key field
private String address;
private String number;
public Listing(String n, String a, String num) {
name = n;
address = a;
number = num;
}
public String toString() {
return ("Name is " + name
+ "\nAddress is " + address
+ "\nNumber is " + number + "\n");
}
public Listing deepCopy() {
Listing clone = new Listing(name, address, number);
return clone;
}
public int compareTo(Object targetKey) {
String tKey = (String) targetKey;
return (name.compareTo(tKey));
}
public void setAddress(String a) // coded to demonstrate encapsulation
{
address = a;
}
}
public class StudentListing implements GenericNode {
// instance variables - replace the example below with your own
private String name; // keyfield
private String identification_no;
private String gpa;
/**
* Constructor for objects of class StudentListings
*
* @ param n- name of the student
* @ param id- identification number of the student
* @ param g- gpa of the student
*/
public StudentListing(String n, String id, String g) {
// initialise instance variables
name = n;
identification_no = id;
gpa = g;
}
/**
* toString method annotates the student info for display
*
* @ return name - name of the student
* @ return identification_no - identification number of the student
* @ return gpa - grade point average of the student
*/
public String toString() {
return ("Name is " + name + "\nIdentification Number is " + identification_no
+ "\nGpa is " + gpa + "\n");
}
/**
* deepCopy method copies the content on the StudentListings object that
* invokes it to a new StudentListings object and returns the clone.
*
* @ return clone - copy of the StudentListings that invoked it
*/
public StudentListing deepCopy() {
StudentListing clone = new StudentListing(name, identification_no, gpa);
return clone;
}
/**
* compareTo method compares the targetKey to the keyfield name
*
* @ return - 0, if the argument string is equal to this string
*/
public int compareTo(Object targetKey) {
String tKey = (String) targetKey;
return (name.compareTo(tKey));
}
/**
* setName method, sets the name of the student a param n- name of the
* student
*/
public void setName(String n) {
name = n;
}
/**
* setId method, sets the id of the student a param n- name of the student
*/
public void setId(String id) {
identification_no = id;
}
/**
* setName method, sets the name of the student a param n- name of the
* student
*/
public void setGpa(String g) {
gpa = g;
}
/**
* input method, allowers for user to input data
*
*/
public void input() {
name = JOptionPane.showInputDialog("Enter a name");
identification_no = JOptionPane.showInputDialog("Enter an identification number");
gpa = JOptionPane.showInputDialog("Enter a gpa");
} //end of inputStudentListings method
}//end of class StudentListings