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

Finish the coding the following double-linked list implementation (called “myLin

ID: 3667599 • Letter: F

Question

Finish the coding the following double-linked list implementation (called “myLinkedList”) given bellow:

Most of the functions/definitions are not finished. You should code the existing functions without changing their existing definition. Also, you can add code to the “main” function. The goal is to show that you have a complete myLinkedList implementation. If needed, add new functions/methods to complete your implementation. Hint: Some of the definitions are already in the textbook, but might be a bit different.

public class MyLinkedList <AnyType> implements Iterable<AnyType> {
   private int theSize;
   private Node<AnyType> beginMarker;
   private Node<AnyType> endMarker;
   public class Node<AnyType>{
       public Node (AnyType data, Node<AnyType> head, Node<AnyType> tail){
           myData=data;
           myHead=head;
           myTail=tail;
       }
       public AnyType myData;
       public Node<AnyType> myHead;
       public Node<AnyType> myTail;
   }
   public MyLinkedList(){}
  
   public void clear(){}
  
   public int size(){
      
   }
  
   public boolean exist(AnyType newVal){
      
   }
  
   public boolean add(AnyType newVal){
      
   }
  
   public boolean add(int index, AnyType newVal) {
      
   }
  
   public Node<AnyType> get(AnyType nodeData){}
  
   public void printList(){};
  
   public java.util.Iterator<AnyType> iterator(){}
  
   public void remove(AnyType removeVal){}
  
   private class LinkedListIterator implements java.util.Iterator<AnyType>{
       public void remove(){}
       public boolean hasNext() {}
       public AnyType next() {}
   }
  
   private void addBefore(Node<AnyType> previousNode, AnyType newNode){}
  
   private void testListIntegers(){
       MyLinkedList<Integer> testList = new MyLinkedList<Integer>();
       testList.add(new Integer(5));
       testList.add(new Integer(4));
       testList.add(new Integer(3));
       System.out.println(" We have so far inserted " + testList.size() + "
               elements in the list");
               testList.remove(4);
       System.out.println(" Now, there is only " + testList.size() + " elements
               left in the list");
               testList.add(1, new Integer(7));
       testList.add(2, new Integer(8));
       System.out.println(" About to print content of the list");
       testList.printList();
   }
   private void testListStrings(){
       MyLinkedList<String> testList = new MyLinkedList<String>();
       testList.add(new String("hello"));
       testList.add(new String("this is"));
       testList.add(new String("cs3345 project 2"));
       System.out.println(" We have so far inserted " + testList.size() + ''
               elements in the list");
               testList.remove("this is");
       System.out.println(" Now, there is only " + testList.size() + " elements
               left in the list");
               testList.add(1, "a modified version of");
       testList.add(2, "cs3345 project 2, call it version 2" );
       System.out.println(" About to print content of the list");
       testList.printList();
   }
   public static void main (String args[]) throws Exception {
       // Add whatever code you need here
       // However, you will need to call both testListIntegers()
       // and testListStrings()
   }
}

Explanation / Answer

Comments added

public class MyLinkedList<AnyType> implements Iterable<AnyType> {
//variables declared
private int theSize;
private Node<AnyType> beginMarker;
private Node<AnyType> endMarker;

//node class
public class Node<AnyType> {

public Node(AnyType data, Node<AnyType> head, Node<AnyType> tail) {
myData = data;
myHead = head;
myTail = tail;
}
public AnyType myData;
public Node<AnyType> myHead;
public Node<AnyType> myTail;
}

//constructor
public MyLinkedList() {
theSize = 0;
}

//clear method
public void clear() {
beginMarker = null;
endMarker = null;
}

//size method
public int size() {
return theSize;
}

//checking value exist or not
public boolean exist(AnyType newVal) {
Node ptr = beginMarker;
for(int i=0;i<theSize;i++){
//if value found
if(ptr.myData == newVal){
return true;
}
else{
ptr = ptr.myTail;
}
}
//if not found
return false;
}

//adding new node
public boolean add(AnyType newVal) {
Node tmp = new Node(newVal, beginMarker, null);
//checking whether head is null or not
if (beginMarker != null) {
beginMarker.myHead = tmp;
}
beginMarker = tmp;
if (endMarker == null) {
endMarker = tmp;
}
theSize++;
return true;
}

//adding new node at particular position
public boolean add(int index, AnyType newVal) {
Node nptr = new Node(newVal, null, null);
if (index == 1) {
add(newVal);
return true;
}
Node ptr = beginMarker;
for (int i = 2; i <= theSize; i++) {
//setting the pointers.. previous and next links
if (i == index) {
Node tmp = ptr.myTail;
ptr.myTail = nptr;
nptr.myHead = ptr;
nptr.myTail = tmp;
tmp.myHead = nptr;
}
ptr = ptr.myTail;
}
theSize++;
return true;
}

//getting node .. with matched data
public Node<AnyType> get(AnyType nodeData) {
Node ptr = beginMarker;
for(int i=0;i<theSize;i++){
if(ptr.myData == nodeData){
return ptr;
}
else{
ptr = ptr.myTail;
}
}
return null;
}
  
//printing all nodes
public void printList() {
Node ptr = beginMarker;
for(int i=0;i<theSize;i++){
System.out.println("At position "+(i+1)+" data stored is: "+ptr.myData);
ptr = ptr.myTail;
}
}

// i am not understanding .. what is difference between printList and itertor method
public java.util.Iterator<AnyType> iterator() {
return null;
}

//removing node
public void remove(AnyType removeVal) {
Node ptr = beginMarker;
for(int i=0;i<theSize;i++){
if(ptr.myData == removeVal){
Node tmp1 = ptr.myHead;
tmp1.myTail = ptr.myTail;
}
}
}

//adding node before matched value node
private void addBefore(Node<AnyType> previousNode, AnyType newNode) {
Node ptr = beginMarker;
for(int i=0;i<theSize;i++){
if(ptr.myData == newNode){
Node tmp1 = ptr.myHead;
tmp1.myTail = previousNode;
previousNode.myTail = ptr;
}
}
}

//testing methods
private void testListIntegers() {
MyLinkedList<Integer> testList = new MyLinkedList<Integer>();
testList.add(new Integer(5));
testList.add(new Integer(4));
testList.add(new Integer(3));
System.out.println(" We have so far inserted " + testList.size() + "elements in the list");
testList.remove(4);
System.out.println(" Now, there is only " + testList.size() + " elements left in the list");
testList.add(1, new Integer(7));
testList.add(2, new Integer(8));
System.out.println(" About to print content of the list");
testList.printList();
}

private void testListStrings() {
MyLinkedList<String> testList = new MyLinkedList<String>();
testList.add(new String("hello"));
testList.add(new String("this is"));
testList.add(new String("cs3345 project 2"));
System.out.println(" We have so far inserted " + testList.size() + "elements in the list");
testList.remove("this is");
System.out.println(" Now, there is only " + testList.size() + " elements left in the list");
testList.add(1, "a modified version of");
testList.add(2, "cs3345 project 2, call it version 2");
System.out.println(" About to print content of the list");
testList.printList();
}

public static void main(String args[]) throws Exception {
// Add whatever code you need here
// However, you will need to call both testListIntegers()
// and testListStrings()
MyLinkedList obj = new MyLinkedList();
obj.testListIntegers();
obj.testListStrings();   
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote