Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/************************************* LinkedList.java *************************

ID: 3729615 • Letter: #

Question

/************************************* LinkedList.java ****************************************/

// Fill in code in ALL commented areas

public class LinkedList {

// Defined Node class

private class Node {

private Object Data = null;

private Node Next = null;

public Node() {

Data = null;

Next = null;

}

public Node(Object element) {

Data = element;

}

public Node(Object o, Node n) {

Data = o;

Next = n;

}

public void setNext(Node n) {

Next = n;

}

public Node getNext() {

return Next;

}

public Object getElement() {

return Data;

}

public void setElement(Object element) {

Data = element;

}

}

// Internal data for LinkedList

private Node head = null;

private Node current = null;

private int size = 0;

// LinkedList constructor

public LinkedList() {

head = null;

current = head;

}

// Move the current position forward one position

public void forward() {

}

// Move the current position backward one position

public void backward() {

}

// Get current object's data element

public Object currentData() {

}

// Add object to the first of the list

public void addFirst(Object o) {

}

// resetCurrent at the first position

public void resetCurrent() {

}

// Add object to the last of the list

public void addLast(Object o) {

}

// Add an object o before the current position

public void insertBefore(Object o) {

}

// Add an object o after the current position

public void insertAfter(Object o) {

}

// Get first object

public Object getFirst() {

}

// Get last object

public Object getLast() {

}

// Remove the first object

public Object removeFirst(){

}

// Remove the last object

public Object removeLast() {

}

// Remove object o from the list

public void remove(Object o) {

}

// Returns the number of elements on the list

public int size() {

return size;

}

// Is the list emptied?

public boolean isEmpty() {

}

// Display a content of a list

public String toString() {

String r = "( HEAD -> ";

// Node l = head.getNext();

Node l = head;

while (l != null) {

r = r + l.getElement() + " -> " ;

l = l.getNext();

}

return r + " )";

}

public static void main(String args[]) {

LinkedList lst = new LinkedList();

// creat instances for testing

CsusStudent instance1 = new CsusStudent("John Doe 1", 1, "1

Somewhere", "916-555-1211", "johndoe1@somewhere.com");

CsusStudent instance2 = new CsusStudent("John Doe 2", 2, "2

Somewhere", "916-555-1212", "johndoe2@somewhere.com");

CsusStudent instance3 = new CsusStudent("John Doe 3", 3, "3

Somewhere", "916-555-1213", "johndoe3@somewhere.com");

CsusStudent instance4 = new CsusStudent("John Doe 4", 4, "4

Somewhere", "916-555-1214", "johndoe4@somewhere.com");

CsusStudent instance5 = new CsusStudent("John Doe 5", 5, "5

Somewhere", "916-555-1215", "johndoe5@somewhere.com");

CsusStudent instance6 = new CsusStudent("John Doe 6", 6, "6

Somewhere", "916-555-1216", "johndoe6@somewhere.com");

CsusStudent instance7 = new CsusStudent("John Doe 7", 7, "7

Somewhere", "916-555-1217", "johndoe7@somewhere.com");

CsusStudent instance8 = new CsusStudent("John Doe 8", 8, "8

Somewhere", "916-555-1218", "johndoe8@somewhere.com");

CsusStudent instance9 = new CsusStudent("John Doe 9", 9, "9

Somewhere", "916-555-1219", "johndoe9@somewhere.com");

// begin adding instance1 to the list

// test forward and backward for single entry

// now add instance2 and instance3 via addFirst and instance4,

instance5, instance6 via addLast

// move current forward a few times

// insert instance 9 after

// remove instance9

// print out the first and last entries

System.out.println("Show the first entry and last entry:");

// print out the whole list

System.out.println("Show the whole list:");

// remove entries starting from the last entry

System.out.println("Check for the content of the emptied list");

}

}

/********************************** CsusStudent.java *************************************************/

public class CsusStudent {
// class attributes
private String studentName;
private int studentId;
private String studentAddress;
private String studentPhone;
private String studentEmail;

// constructor
public CsusStudent(String newName, int newId, String newAddress, String newPhone_number, String newEmail){
this.studentName = newName;
this.studentId = newId;
this.studentAddress = newAddress;
this.studentPhone = newPhone_number;
this.studentEmail = newEmail;
}

// setName
public void setName(String newName){
this.studentName = newName;
}

// getName
public String getName(){
return studentName;
}
  
// setID
public void setId(int newId){
this.studentId = newId;
}

// getID
public int getId(){
return studentId;
}  

// setAddress
public void setAddress(String newAddress){
this.studentAddress = newAddress;
}

// getAddress
public String getAddress(){
return studentAddress;
}

// setPhone
public void setPhone(String newPhone_number){
this.studentPhone = newPhone_number;
}

// getPhone
public String getPhone(){
return studentPhone;
}

// setEmail
public void setEmail(String newEmail){
this.studentEmail = newEmail;
}

// getEmail
public String getEmail(){
return studentEmail;
}

// toString
public String toString() {
return "studentName=" + studentName + ", studentId=" + studentId + ", studentAddress="
+ studentAddress + ", studentPhone=" + studentPhone + ", studentEmail=" + studentEmail;
}
}

/************************************************************** LinkedListTest.java ********************************************************/

// Description: Test suite module to test LinkedListTest1 and

LinkedListTest2.

// Author: Doan Nguyen

// Date: 3/5/18 (Updated)

import org.junit.runner.RunWith;

import org.junit.runners.Suite;

import org.junit.Assert;

import static org.junit.Assert.*;

import org.junit.Before;

import org.junit.Test;

@RunWith(Suite.class)

@Suite.SuiteClasses({

LinkedListTest1.class,

LinkedListTest2.class

})

public class LinkedListTest {

}

/************************************************************** LinkedListTest1.java *********************************************/

// Description: Test module to test single linear linkedlist with

CsusStudent objects

// Author: Doan Nguyen

// Date: 1/8/18 (Updated)

import org.junit.Assert;

import static org.junit.Assert.*;

import org.junit.Before;

import org.junit.Test;

public class LinkedListTest1 {

/** Fixture initialization (common initialization

* for all tests). **/

CsusStudent instance1, instance2, instance3, instance4, instance5,

instance6, instance7, instance8;

@Before public void setUp() {

instance1 = new CsusStudent("John Doe 1", 1, "1 Somewhere", "916-

555-1211", "johndoe1@somewhere.com");

instance2 = new CsusStudent("John Doe 2", 2, "2 Somewhere", "916-

555-1212", "johndoe2@somewhere.com");

instance3 = new CsusStudent("John Doe 3", 3, "3 Somewhere", "916-

555-1213", "johndoe3@somewhere.com");

instance4 = new CsusStudent("John Doe 4", 4, "4 Somewhere", "916-

555-1214", "johndoe4@somewhere.com");

instance5 = new CsusStudent("John Doe 5", 5, "5 Somewhere", "916-

555-1215", "johndoe5@somewhere.com");

instance6 = new CsusStudent("John Doe 6", 6, "6 Somewhere", "916-

555-1216", "johndoe6@somewhere.com");

instance7 = new CsusStudent("John Doe 7", 7, "7 Somewhere", "916-

555-1217", "johndoe7@somewhere.com");

instance8 = new CsusStudent("John Doe 8", 8, "8 Somewhere", "916-

555-1218", "johndoe8@somewhere.com");

}

// test addFirst and getFirst

@Test public void testaddGetFirst() {

LinkedList lst = new LinkedList();

lst.addFirst(instance1);

int expResult = 1;

assertEquals(expResult, lst.size());

CsusStudent instance = (CsusStudent) lst.getFirst();

assertEquals("John Doe 1", instance.getName());

}

// test list traversal 1 with forward, getFirst, and getLast

@Test public void testTraversal1() {

LinkedList lst = new LinkedList();

lst.addFirst(instance1);

lst.addFirst(instance2);

lst.addFirst(instance3);

lst.addLast(instance4);

lst.addLast(instance5);

lst.addLast(instance6);

lst.forward();

lst.forward();

lst.insertAfter(instance7);

CsusStudent stf = (CsusStudent)lst.getFirst();

CsusStudent stl = (CsusStudent)lst.getLast();

assertEquals("John Doe 3", stf.getName());

assertEquals("John Doe 6", stl.getName());

}

// test list traversal 2 with backward, getFirst, getLast, and size

@Test public void testTraversal2() {

LinkedList lst = new LinkedList();

lst.addFirst(instance1);

lst.addFirst(instance2);

lst.addFirst(instance3);

lst.addLast(instance4);

lst.addLast(instance5);

lst.addLast(instance6);

lst.backward();

lst.backward();

lst.insertBefore(instance7);

CsusStudent stf = (CsusStudent)lst.getFirst();

CsusStudent stl = (CsusStudent)lst.getLast();

assertEquals("John Doe 7", stf.getName());

assertEquals("John Doe 6", stl.getName());

assertEquals(7,lst.size());

}

}

/************************** LinkedListTest2.java (very long so its in a paste bin) ************************************/

https://pastebin.com/HYDtnp4B

Class LinkedList private head Node private current Node size: int insertBefore(o) Add an object o before the current position. insertAfter(o) - Add an object o after the current position. remove(o)- Object o is removed from the list. currentData) - Return the current object's data. size) - Return the number of objects on the list. forward)- Move the current position forward one position. backward0 Move the current position backward one position. resetCurrent)- Reset the current position at the first element. addFirst)- Add object to the first of the list addLast- Add object to the last of the list getFirst Get first object getLast- Get last object removeFirst()-Remove first object removeLast) Remove last object

Explanation / Answer

public class LinkedList {
    // Defined Node class
    private class Node {
        private Object Data = null;
        private Node Next = null;
        public Node() {
            Data = null;
            Next = null;
        }
        public Node(Object element) {
            Data = element;
        }
        public Node(Object o, Node n) {
            Data = o;
            Next = n;
        }
        public void setNext(Node n) {
            Next = n;
        }
        public Node getNext() {
            return Next;
        }
        public Object getElement() {
            return Data;
        }
        public void setElement(Object element) {
            Data = element;
        }
    }
    // Internal data for LinkedList
    private Node head = null;
    private Node current = null;
    private int size = 0;
    // LinkedList constructor
    public LinkedList() {
        head = null;
        current = head;
    }
    // Move the current position forward one position
    public void forward() {
        Node tempNode = current;
        if(tempNode.Next!=null)
        {
            current = tempNode.Next;
        }
    }
    // Move the current position backward one position
    public void backward() {
        // we can not move backward as the given list is the single linked list
    }
    // Get current object's data element
    public Object currentData() {
        return current.Data;
    }
    // Add object to the first of the list
    public void addFirst(Object o) {
        Node tempNode = head;
        Node newNode = new Node(o,tempNode);
        head=newNode;
    }
    // resetCurrent at the first position
    public void resetCurrent() {
        Node tempNode = head;
        head = current;
        head.next=tempNode;
    }
    // Add object to the last of the list
    public void addLast(Object o) {
        Node tmpNode = head;
        while(true){
            if(tmpNode.Next == null){
                Node newNode = new Node(o,null);
            }
            tempNode = tempNode.Next;
          
        }
    }
    // Add an object o before the current position
    public void insertBefore(Object o) {
        // we can not add an object o before the current position given list is the single linked list
    }
    // Add an object o after the current position
    public void insertAfter(Object o) {
        Node tempNode = current;
        current.Next = newNode(o,tempNode);
    }
    // Get first object
    public Object getFirst() {
        return head.Data;
    }
    // Get last object
    public Object getLast() {
        Node tmpNode = head;
        while(true){
            if(tmpNode.Next == null){
                return tmpNode.Data;
            }
            tempNode = tempNode.Next;
          
        }
    }
    // Remove the first object
    public Object removeFirst() {
        Node tempNode = head;
        head = tempNode.Next;
    }
    // Remove the last object
    public Object removeLast() {
        Node tmpNode = head;
        while(true){
            if(tmpNode.Next == null){
                curNode.Next=null;
            }
            curNode = tempNode;
            tempNode = tempNode.Next;
          
        }
    }
    // Remove object o from the list
    public void remove(Object o) {
        Node tmpNode = head;
        while(true){
            if(tmpNode.Data == o){
                curNode.Next=tempNode.Next;
                break;
            }
            if(tempNode.Next==null)
            {
                break;
            }
            curNode = tempNode;
            tempNode = tempNode.Next;
          
        }
    }
    // Returns the number of elements on the list
    public int size() {
       Node tmpNode = head;
       int count=0;
        while(true){
            if(tempNode.Next==null)
            {
               return count;
            }
            count++;
            tempNode = tempNode.Next;
          
        }
    }
    // Is the list emptied?
    public boolean isEmpty() {
        if(head==null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    // Display a content of a list
    public String toString() {
        String r = "( HEAD -> ";
        // Node l = head.getNext();
        Node l = head;
        while (l != null) {
            r = r + l.getElement() + " -> ";
            l = l.getNext();
        }
        return r + " )";
    }
    public static void main(String args[])
    {
        LinkedList lst = new LinkedList();
        // creat instances for testing
        CsusStudent instance1 = new CsusStudent("John Doe 1", 1, "1 Somewhere", "916-555-1211", "johndoe1@somewhere.com");
        CsusStudent instance2 = new CsusStudent("John Doe 2", 2, "2 Smewhere", "916-555-1212", "johndoe2@somewhere.com");
        CsusStudent instance3 = new CsusStudent("John Doe 3", 3, "3 Somewhere", "916-555-1213", "johndoe3@somewhere.com");
        CsusStudent instance4 = new CsusStudent("John Doe 4", 4, "4 Somewhere", "916-555-1214", "johndoe4@somewhere.com");
        CsusStudent instance5 = new CsusStudent("John Doe 5", 5, "5 Somewhere", "916-555-1215", "johndoe5@somewhere.com");
        CsusStudent instance6 = new CsusStudent("John Doe 6", 6, "6 Somewhere", "916-555-1216", "johndoe6@somewhere.com");
        CsusStudent instance7 = new CsusStudent("John Doe 7", 7, "7 Somewhere", "916-555-1217", "johndoe7@somewhere.com");
        CsusStudent instance8 = new CsusStudent("John Doe 8", 8, "8 Somewhere", "916-555-1218", "johndoe8@somewhere.com");
        CsusStudent instance9 = new CsusStudent("John Doe 9", 9, "9 Somewhere", "916-555-1219", "johndoe9@somewhere.com");
        // begin adding instance1 to the list
       
        lst.addFirst(instance1);

        // test forward and backward for single entry
                forward();
        // now add instance2 and instance3 via addFirst and instance4,
                lst.addFirst(instance2);
                forward();
                lst.insertAfter(instance3);
        //instance5, instance6 via addLast
        lst.addLast(instance5);
        lst.addLast(instance6);
        // move current forward a few times
        forward();
        forward();
        // insert instance 9 after
        lst.insertAfter(instance9);
        // remove instance9
        lst.remove(instance9)
        // print out the first and last entries
        System.out.println("Show the first entry and last entry:");
        CsusStudent _first = lst.getFirst();
        if(_first!=null)
        {
            System.out.println(_first.toString());
        }
        CsusStudent _last = lst.getLast();
        if(_last!=null)
        {
            System.out.println(_last.toString());
        }
        // print out the whole list
        System.out.println("Show the whole list:");
        System.out.println(lst.toString());
        // remove entries starting from the last entry
        System.out.println("Check for the content of the emptied list");  
        System.out.println(lst.isEmpty());
    }
}
                                           
   public class CsusStudent {
   // class attributes
   private String studentName;
   private int studentId;
   private String studentAddress;
   private String studentPhone;
   private String studentEmail;
  
   // constructor
   public CsusStudent(String newName, int newId, String newAddress, String newPhone_number, String newEmail){
      this.studentName = newName;
      this.studentId = newId;
      this.studentAddress = newAddress;
      this.studentPhone = newPhone_number;
      this.studentEmail = newEmail;
      }
   // setName
   public void setName(String newName){
      this.studentName = newName;
      }
   // getName
   public String getName(){
      return studentName;
      }

   // setID
   public void setId(int newId){
      this.studentId = newId;
      }
   // getID
   public int getId(){
      return studentId;
      }
   // setAddress
   public void setAddress(String newAddress){
      this.studentAddress = newAddress;
      }
   // getAddress
   public String getAddress(){
      return studentAddress;
      }
   // setPhone
   public void setPhone(String newPhone_number){
      this.studentPhone = newPhone_number;
      }
   // getPhone
   public String getPhone(){
      return studentPhone;
      }
   // setEmail
   public void setEmail(String newEmail){
      this.studentEmail = newEmail;
      }
   // getEmail
   public String getEmail(){
      return studentEmail;
      }
   // toString
   public String toString() {
      return "studentName=" + studentName + ", studentId=" + studentId + ", studentAddress="
      + studentAddress + ", studentPhone=" + studentPhone + ", studentEmail=" + studentEmail;
      }
}