// Guide to Data Structures
// Section 6.7.2
// Copyright 2018, by J.T. Streib and T. Soma
public class TestLinkedListGenericStudentComparable {
public static void main(String[] args) {
LinkedListGenericInnerNode<StudentComparable> list
= new LinkedListGenericInnerNode<StudentComparable>();
LinkedListGenericInnerNode<StudentComparable> ptr;
StudentComparable student1
= new StudentComparable("George", "MIS", 1234);
StudentComparable student2
= new StudentComparable("Maya", "CS", 1212);
StudentComparable student3
= new StudentComparable("Joffrey", "CS", 3456);
list.insert(student1);
list.insert(student2);
list.insert(student3);
list.printRecursive();
}
}
public class LinkedListGenericInnerNode<T extends Comparable<T>> {
private class NodeGeneric<T> {
private T data;
private NodeGeneric<T> next;
public NodeGeneric() {
this(null);
}
public NodeGeneric(T data) {
this.data = data;
next = null;
}
}
private NodeGeneric<T> head;
public LinkedListGenericInnerNode(){
head = null;
}
public boolean insert(T item) {
boolean inserted;
NodeGeneric<T> ptr,prev;
inserted = false;
ptr = head;
prev = null;
while(ptr != null && ptr.data.compareTo(item) < 0){
prev = ptr;
ptr = ptr.next;
}
if(ptr == null || !(ptr.data.equals(item))) {
inserted = true;
NodeGeneric<T> newp = new NodeGeneric();
newp.data = item;
newp.next = ptr;
if(prev == null)
head = newp;
else
prev.next = newp;
}
return inserted;
}
public boolean delete(T item) {
boolean deleted=false;
NodeGeneric<T> ptr, prev;
return deleted;
// Body of method
}
public void printRecursive() {
System.out.println("List Recursive: ");
printR(head);
System.out.println();
}
private void printR(NodeGeneric<T> p){
if(p != null) {
System.out.println(p.data+" ");
printR(p.next);
}
}
}
public class StudentComparable implements
Comparable<StudentComparable> {
private String name, major;
private int id;
public StudentComparable(String name, String major, int id) {
this.name = name;
this.major = major;
this.id = id;
}
public String getName() {
return name;
}
public String getMajor() {
return major;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public void setMajor(String major) {
this.major = major;
}
public void setId(int id) {
this.id = id;
}
public int compareTo(StudentComparable otherStudent) {
int result;
if(id < otherStudent.getId())
result = -1;
else
if(id > otherStudent.getId())
result = 1;
else
result = 0;
return result;
}
public boolean equals(Object otherStudent) {
StudentComparable otherStudentObject
= (StudentComparable)otherStudent;
return (id == otherStudentObject.id);
}
public String toString() {
return ("Student named " + name + " with id " + id +
" and major " + major);
}
}