In: Computer Science
CAN SOMEONE PLEASE DO THIS QUESTION FOR ME.
You are given three files Main.Java, Student.Java, LinkedList.java. Student is completed for you and is only used to test the LinkedList with an object (we will pass in Student for the generic parameter of the list). Your mission is to implement LinkedList.java, but I highly recommend you do this in a piecewise fashion as outlined in the //TODO s in Main.java (This assignment is easy if you do it piecewise and traumatic if you don't). You can go down this description of the TODO's for a few additional hints. You can try to pass the test cases as you go through (they should be in order I think but sometimes the order gets scrambled when I create the test cases and there is no easy way to unscramble, so just be aware of that). Each test case includes the code of how I made it so you can try to do it in main if you are not passing a test case for some reason for further investigation For this assignment you can assume that each student in the list is unique (for all of the tests I do not add any duplicate students, we will deal with this case later, it's slightly more code but nothing too crazy)
MAIN.JAVA
import java.util.*;
class Main {
public static void main(String[] args)
{
//TODO 1: Implement the private inner class Node in LinkedList
//TODO 2: Here in Main, Make a new LinkedList with the generic parameter of Student. Note that the constuctor in LinkedList is implemented for you. What does the constructor initialize the list to?
LinkedList<Student> myList= new LinkedList<Student>();
//TODO 3: Define 5 Students, remember to invoke the constructor using the new command, make them all have different ID's
//TODO 4: Implement addToFront and getLength in Linked List Add some of the students to the list using addToFront
//print out the length run your program and make sure it equals the number of students before continuing
//TODO 5: Implement toString in Node and LinkedList. The Node toString should just print out the data field of the node. Note that toString is implemented in Student, so this should be easy-- creating a string out of the data should invoke the toString in Student (i.e. if I have a student instance named A saying String x= ""+A; will invoke A's toString. Alternatively you can explicitly call A.toString() from within Node (but understand why ""+A will work). Then implement toString in LinkedList. If one prints out a list with two students (Eugenio and Daniqua), with Danica added to the front first and Eugenio added second our list should look as follows head->name: Eugenio id: 700555555->name: Daniqua id: 700444444->null note the spaces. Print out your list to ensure it works. //TODO 6: Implement addToEnd in LinkedList and then add another Student, but this time to the end, and print out the length (should be one more) and the list to ensure it added the student to the end.
//TODO 7: Implement removeFromFront in LinkedList and print out what is returned, should be the first student in the list. Then print out the length (should be one less than what you had previously). Finally print out the length to ensure it is correct using getLength
//TODO 8: Implement removeFromEnd in LinkedList and print out what is returned. it should be the student who was at the end of the list, furthermore print out the list and the length to ensure it has the right students in it. Note that this function is a bit more involved, you may need to use 3 references as you iterate through the list (think about why?). Make sure your algorithm will work with a list that is of length 1, length 2, length 3, and some big length like 5
//TODO 9: Implement removeTarget in LinkedList. This should rely on the the Type T's equals. Remember our T, though it's generic, is guaranteed to have this function because it's guaranteed to be an object. In Student's case two students are equal if their student ID's are equal. Test removeTarget with a student in the list to ensure that removeTarget returns the right value when you test (and removes the actual student from the list when appropriate).
///TODO 10: Implement reverseList in LinkedList. You need to reverse the list in place, i.e., with out allocating data into array.
}
}
LINKEDLIST.JAVA
public class LinkedList <T>
{ //MUST IMPLEMENT ALL FUNCTIONS
Node head;
public LinkedList()
{
head=null;
}
public int getLength()
{
return 0;
}
public void addToFront(T toAdd)
{
}
public void addToEnd(T toAdd)
{
{
public T removeFromFront()
{
return null;
}
public T removeFromEnd()
{
return null;
}
public boolean removeTarget(T toRemove)
{
return true;
}
public void reverseList(){
}
public String toString()
{
return "";
}
private class Node
{
private T data;
private Node next;
Node(T dataPassed, Node nextPassed)
{
data=dataPassed;
next=nextPassed;
}
public void setdata(T dataPassed)
{
data=dataPassed;
}
public T getdata()
{
return data;
}
public Node getnext()
{
return next;
}
public void setnext(Node passed)
{
next=passed;
}
public String toString()
{
return (""+data);
}
}
}
STUDENT.JAVA
public final class Student
{
private int schoolID;
private String name;
public Student(String namePassed,int schoolIDPassed)
{
schoolID= schoolIDPassed;
name= namePassed;
}
public int getSchoolID()
{
return schoolID;
}
public void setSchoolID(int schoolIDPassed)
{
schoolID=schoolIDPassed;
}
public String getName()
{
return name;
}
public void setName(String namePassed)
{
name=namePassed;
}
public boolean equals(Object toCompare)
{
Student temp= (Student) toCompare;
return(temp.getSchoolID()==schoolID);
}
public String toString()
{
String toReturn="name: "+name+" id: "+schoolID;
return (toReturn);
}
}
//Student.java
public final class Student {
private int schoolID;
private String name;
public Student(String namePassed,int schoolIDPassed)
{
schoolID= schoolIDPassed;
name= namePassed;
}
public int getSchoolID()
{
return schoolID;
}
public void setSchoolID(int schoolIDPassed)
{
schoolID=schoolIDPassed;
}
public String getName()
{
return name;
}
public void setName(String namePassed)
{
name=namePassed;
}
public boolean equals(Object toCompare)
{
Student temp= (Student) toCompare;
return(temp.getSchoolID()==schoolID);
}
public String toString()
{
String toReturn="name: "+name+" id: "+schoolID;
return (toReturn);
}
}
//end of Student.java
//LinkedList.java
public class LinkedList<T> {
//MUST IMPLEMENT ALL FUNCTIONS
private Node head;
public LinkedList()
{
head=null;
}
public int getLength()
{
Node current=head;
int count=0;
while(current!=null)
{
count++;
current=current.getnext();
}
return count;
}
public void addToFront(T toAdd)
{
Node node = new Node(toAdd,head);
head = node;
}
public void addToEnd(T toAdd)
{
Node node = new Node(toAdd,null);
if(head == null)
{
head = node;
}else {
Node curr = head;
while(curr.getnext() != null)
curr = curr.getnext();
curr.setnext(node);
}
}
public T removeFromFront()
{
if(head == null)
return null;
T data = head.getdata();
head = head.getnext();
return data;
}
public T removeFromEnd()
{
if(head == null)
return null;
Node curr = head;
Node prev = null;
while(curr.getnext() != null)
{
prev = curr;
curr = curr.getnext();
}
T data = curr.getdata();
prev.setnext(null);
return data;
}
public boolean removeTarget(T toRemove)
{
if(head == null)
return false;
else if(head.getdata().equals(toRemove))
{
head = head.getnext();
return true;
}
Node prev = null;
Node curr = head;
while(curr.getnext() != null)
{
if(curr.getdata().equals(toRemove))
{
prev.setnext(curr.getnext());
return true;
}
prev = curr;
curr = curr.getnext();
}
if(curr.getdata().equals(toRemove))
{
prev.setnext(null);
return true;
}
return false;
}
// method to reverse the list in place
public void reverseList(){
// if list is empty or contains single element, do nothing
if((head != null) && (head.next != null))
{
Node prev = null; // previous node initially null
Node curr = head; // current node
Node next = head.next; // node next to current in original list
// loop till we reach the last node
while(curr.next != null)
{
curr.next = prev; // make next of current point to prev
prev = curr; // make prev = curr
curr = next; // make curr = next
next = next.next; // set next = node next to next in original list
}
curr.next = prev; // make the last node point to the prev node
head = curr; // make last node head
}
}
public String toString()
{
String llStr = "head -> ";
Node curr = head;
while(curr != null)
{
llStr = llStr + curr.getdata()+" -> ";
curr = curr.getnext();
}
llStr = llStr + "null";
return llStr;
}
private class Node
{
private T data;
private Node next;
Node(T dataPassed, Node nextPassed)
{
data= dataPassed;
next=nextPassed;
}
public void setdata(T dataPassed)
{
data=dataPassed;
}
public T getdata()
{
return data;
}
public Node getnext()
{
return next;
}
public void setnext(Node passed)
{
next=passed;
}
}
}
//end of LinkedList.java
//Main.java
import java.util.*;
public class Main{
public static void main(String[] args) {
//TODO 1: Implement the private inner class Node in LinkedList -- done
//TODO 2: Here in Main, Make a new LinkedList with the generic parameter of Student. Note that the constuctor in LinkedList is implemented for you. What does the constructor initialize the list to? empty list
// --done
LinkedList<Student> myList= new LinkedList<Student>();
//TODO 3: Define 5 Students, remember to invoke the constructor using the new command, make them all have different ID's
Student stud1 = new Student("John Smith",123);
Student stud2 = new Student("James Carrey",124);
Student stud3 = new Student("Josephine Day",125);
Student stud4 = new Student("Phil Hughes",126);
Student stud5 = new Student("Michael Argan",127);
//TODO 4: Implement addToFront and getLength in Linked List Add some of the students to the list using addToFront
//print out the length run your program and make sure it equals the number of students before continuing
myList.addToFront(stud1);
myList.addToFront(stud2);
System.out.println("Length : "+myList.getLength());
//TODO 5: Implement toString in Node and LinkedList. The Node toString should just print out the data field of the node. Note that toString is implemented in Student, so this should be easy-- creating a string out of the data should invoke the toString in Student (i.e. if I have a student instance named A saying String x= ""+A; will invoke A's toString. Alternatively you can explicitly call A.toString() from within Node (but understand why ""+A will work). Then implement toString in LinkedList. If one prints out a list with two students (Eugenio and Daniqua), with Danica added to the front first and Eugenio added second our list should look as follows head->name: Eugenio id: 700555555->name: Daniqua id: 700444444->null note the spaces. Print out your list to ensure it works.
System.out.println("List contents : \n"+myList);
//TODO 6: Implement addToEnd in LinkedList and then add another Student, but this time to the end, and print out the length (should be one more) and the list to ensure it added the student to the end.
myList.addToEnd(stud3);
System.out.println("Length : "+myList.getLength()+"\nList contents : \n"+myList);
//TODO 7: Implement removeFromFront in LinkedList and print out what is returned, should be the first student in the list. Then print out the length (should be one less than what you had previously). Finally print out the length to ensure it is correct using getLength
Student retStud = myList.removeFromFront();
System.out.println("Student removed from Front : "+retStud);
System.out.println("Length : "+myList.getLength()+"\n List contents : \n"+myList);
//TODO 8: Implement removeFromEnd in LinkedList and print out what is returned. it should be the student who was at the end of the list, furthermore print out the list and the length to ensure it has the right students in it. Note that this function is a bit more involved, you may need to use 3 references as you iterate through the list (think about why?). Make sure your algorithm will work with a list that is of length 1, length 2, length 3, and some big length like 5
retStud = myList.removeFromEnd();
System.out.println("Student removed from End : "+retStud);
System.out.println("Length : "+myList.getLength()+"\n List contents : \n"+myList);
//TODO 9: Implement removeTarget in LinkedList. This should rely on the the Type T's equals. Remember our T, though it's generic, is guaranteed to have this function because it's guaranteed to be an object. In Student's case two students are equal if their student ID's are equal. Test removeTarget with a student in the list to ensure that removeTarget returns the right value when you test (and removes the actual student from the list when appropriate).
boolean targetRemoved = myList.removeTarget(stud1);
if(targetRemoved)
System.out.println("Student : "+stud1+" removed ");
else
System.out.println("Student : "+stud1+" not found ");
System.out.println("Length : "+myList.getLength()+"\n List contents : \n"+myList);
// add stud4 and stud5 to end of linked list to implement reverseList
myList.addToEnd(stud4);
myList.addToEnd(stud5);
System.out.println("Length : "+myList.getLength()+"\n List contents : \n"+myList);
//TODO 10: Implement reverseList in LinkedList. You need to reverse the list in place, i.e., with out allocating data into array.
myList.reverseList();
System.out.println("Reversed List:\nLength : "+myList.getLength()+"\n List contents : \n"+myList);
}
}
//end of Main.java
Output: