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 {
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 Stringpublic address;
privateclass StringMainSinglyLinkedList number;{
public static void main(String[] args) {
SinglyLinkedList <Listing> boston = new SinglyLinkedList<Listing>();
Listing l1 = new Listing(String"Bill", n"1st Avenue", String"123 a4567");
Listing l2 = new Listing("Al", String"2nd numAvenue", "456 3232");
{ Listing l3 = new Listing("Mike", "3rd Avenue", "333 3333");
name 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 = n;new SinglyLinkedList<StudentListing>();
address
StudentListing bill = a;new StudentListing("Bill", "2988050", "3.9");
number StudentListing mary = num;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"));
public String toString() {
return ("Name is " + name
+ "\nAddress is " + address
+ "\nNumber is "System.exit(0);
+ number + "\n");}
}
public class SinglyLinkedList<T> {
public Listing deepCopy() {
Listing clone = new Listing(name, address,private number);
Node<T> h; // returnlist clone;
}header
public int compareTo(Object targetKey public SinglyLinkedList() {
String tKey h = new <T> Node(String); targetKey; // dummy node
return (name h.compareTo(tKey));l = null;
h.next = null;
}
public voidboolean setAddressinsert(StringT anewNode) {
Node n = new Node();
GenericNode node = (GenericNode) newNode;
if (node == null) // codedout toof demonstratememory
encapsulation {
return false;
} else {
address n.next = a;h.next;
} h.next = n;
} public class StudentListing implements GenericNode {
// instance variables - replace the example below with your own n.l = (T) node.deepCopy();
private String name; // keyfield return true;
private String identification_no; }
private String gpa; }
/**
* Constructor for objectspublic ofGenericNode classfetch(Object StudentListingstargetKey) {
* Node p = h.next;
* @ param n- name of the studentGenericNode node = (GenericNode) p.l;
* @ param id- identification number of thewhile student(p != null && !(node.compareTo(targetKey) == 0)) {
* @ param g- gpa of the student p = p.next;
*/ }
public StudentListing(String n, String id, String g if (p != null) {
// initialise instance variables return node.deepCopy();
name = n; } else {
identification_no = id; return null;
gpa = g; }
}
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;
}
}
/**
* toString method annotatespublic theboolean studentupdate(Object infotargetKey, forT display
newNode) *{
* @ return name - name of theif student
(delete(targetKey) *== @false) return{
identification_no - identification number of the student
* @ return gpafalse;
- grade point average of the student
*/
public} Stringelse toStringif (insert(newNode) == false) {
return ("Name is " + name + "\nIdentification Number isreturn "false;
+ identification_no
}
+ "\nGpa isreturn "true;
+ gpa + "\n");
}
/** public void showAll() {
* deepCopy method copies the content on theNode StudentListingsp object= thath.next;
* invokes it to a new StudentListings objectwhile and(p returns!= null) //continue to traverse the clone.list
*
* @ return clone - copy of{
the StudentListings that invoked it
*/
public StudentListing deepCopy System.out.println(p.l.toString() {);
StudentListing clone = new StudentListing(name, identification_no, gpa); p = p.next;
return clone; }
}
} /**
*
* @param <T>
*/
public class Node <T> {
/**
* compareTo method compares the targetKey to theprivate keyfieldT namel;
*
* @ return - 0, if theprivate argumentNode string<T> isnext;
equal to this string
*/
public int<T> compareToNode(Object targetKey) {
String tKey = (String) targetKey;}
return (name.compareTo(tKey)); }// end of inner class Node
}
/**
* setName method, sets the name of the student a param n- name of the
* student
*/
public voidclass setName(StringListing n)implements GenericNode{
name = n;
} private String name; // key field
private String address;
private String number;
/**
* setId method public Listing(String n, setsString thea, idString ofnum) the{
student a param n- name of the student
name */= n;
public void setId(String id) { address = a;
identification_no number = id;num;
}
public String toString() {
return ("Name is " + name
+ "\nAddress is " + address
+ "\nNumber is " + number + "\n");
}
/**
* setName method, setspublic theListing namedeepCopy() of{
the student a param n- Listing clone = new Listing(name, ofaddress, thenumber);
* student return clone;
*/ }
public voidint setGpacompareTo(StringObject gtargetKey) {
gpa String tKey = g;(String) targetKey;
return (name.compareTo(tKey));
}
public void setAddress(String a) // coded to demonstrate encapsulation
{
address = a;
}
}
public class StudentListing implements GenericNode {
/**
* input method, allowers// forinstance uservariables to- inputreplace data
the *
example */
publicbelow voidwith input()your {own
nameprivate =String JOptionPane.showInputDialog("Entername; a// name");keyfield
identification_no = JOptionPane.showInputDialog("Enter anprivate identificationString number");identification_no;
gpa = JOptionPane.showInputDialog("Enterprivate aString gpa");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
}//end of class StudentListings