Heres the code package SingleLinkedLists public class Single

Heres the code:

package SingleLinkedLists;

public class SingleLinkedList {

Node head;

public SingleLinkedList() {
   head = null;
}

public boolean isEmpty() {
   return (head == null);
}

// Method for inserting an object in the first position of a list
public void insertFirst(Object o) {
   // Pack o into a Node
   Node n = new Node(o);

   // put n at the beginning of the list
   n.next = head;

   // and modify the head of the list
   head = n;
}

// Method for inserting an object in the last position of a list   
public void insertLast(Object o) {
   // Pack o into a Node
   Node n = new Node(o);

   // If the list is empty, we simply have to change head
   if (head == null) {
   head = n;
   return;
   }
   // The list is not empty, so we have to traverse it, stopping at the last element
   Node crt = head;
   while (crt.next != null)
   crt = crt.next;
   crt.next = n;
}

// Method for printing the entire list (demonstrates traversal)
public void print() {
   for (Node crt = head; crt != null; crt = crt.next)
   System.out.println(crt.content);
}

// Method for removing the first element in the list; the object is returned
// Note that this is O(1), since we have a sequence of primitive operations
public Object removeFirst() throws EmptyListException {

   if (head == null) throw new EmptyListException();
  
   // Put a \"finger\" on the current head
   Node crt = head;

   // Move the head to the appropriate spot
   head = head.next;

   // Figure out what we need to return
   Object o = crt.content;

   // Clear out all pointer - the garbage collector will take care of this area in memory
   crt.content = null;
   crt.next = null;

   // And we\'re done
   return o;
}


// Method for removing the last element in the list; the object is returned
// This is O(n) because we need to traverse the list
public Object removeLast() throws EmptyListException {

   if (head == null) throw new EmptyListException();

   Object o;

   // If there is only one element, we need to modify the head of the list
   if (head.next == null) {
   o = head.content;
   head.content = null;
   head = null;
   return o;
   }

   // We need to go to the second-to-last element
   Node crt = head;
   while (crt.next.next != null)
   crt = crt.next;

   // Now get the content
   o = crt.next.content;
  
   // Remove all references that are not needed
   crt.next.content = null;
   crt.next = null;

   // And we\'re done
   return o;
}

// Method for finding an object in a list; returns true if the object is found
public boolean find (Object o) {
   if (head == null) return false;
  
   // We need an index in the list
   for (Node crt = head; crt != null; crt = crt.next) {
   if (o.equals(crt.content)) return true;
   }
   return false;
}
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package SingleLinkedLists;

class Node {

Object content;
Node next;

public Node (Object o) {
   content = o;
   next = null;
}
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package SingleLinkedLists;

public class EmptyListException extends Exception {

public EmptyListException() {
   super(\"Illegal operation in an empty list\");
}
}

3. Linked List, 20%] For the given single linked list implementation, add a remove Before (Object o) and removeAfter(Object o) method. In both cases, you should remove from the list the element before or after the object passed in as a parameter. If o is not present in the list, or if there is no element before or after it, you should throw NoSuchElementException.

Solution

//Remove the object before Object o

public Object removeBefore(Object o) throws Exception {

    if (head == null) throw new EmptyListException();
  
   //As there is no element before head throws NoSuchElementException
   if (head.content == o) {
       throw NoSuchElementException;
    }
  
   //If the second Element contains o Make it as head.
   if(head.next.content == o){
       Object removed = head;
  
        head = head.next;
      
       return removed;
   }

    Node crt = head;
  
   //Iterating over the previous of previous element of o
    while (crt.next.next != NULL && crt.next.next.content != o){
  
        crt = crt.next;
   }
   if(ctr.next.next == NULL){
        throw NoSuchElementException;
   }
   //Then remove the next of the prev of previous of o Which is what we need
   Object removed = crr.next.content;
  
   crt.next = crt.next.next;
  
   //return the removed
   return removed;
  
    }
}


//Function to removeAfter a particular Node

public Object removeAfter(Object o) throws Exception {

    //If empty Array
    if (head == null) throw new EmptyListException();
  
   //If the first Elemnt is o, remove the second element if not NULL
   if (head.content == o) {
  
       if(head.next!= NULL){
     
       Object removed = head.next;
       head.next = head.next.next;
     
       return removed;
     
       }
       //else throw Exception
       else{
       throw NoSuchElementException;
     
       }
    }
  
  
    Node crt = head;
  
   //Iterate from head until the next element contains the value o
    while (crt.next != NULL && crt.next.content != o){
  
        crt = crt.next;
   }
   //If reached the End of loop throw Exception
   if(ctr.next == NULL){
        throw NoSuchElementException;
   }
   //Remove the next Element
   ctr = ctr.next;
   Object removed = crr.content;
  
   crt.next = crt.next.next;
  
   return removed;
  
    }
}

Heres the code: package SingleLinkedLists; public class SingleLinkedList { Node head; public SingleLinkedList() { head = null; } public boolean isEmpty() { retu
Heres the code: package SingleLinkedLists; public class SingleLinkedList { Node head; public SingleLinkedList() { head = null; } public boolean isEmpty() { retu
Heres the code: package SingleLinkedLists; public class SingleLinkedList { Node head; public SingleLinkedList() { head = null; } public boolean isEmpty() { retu
Heres the code: package SingleLinkedLists; public class SingleLinkedList { Node head; public SingleLinkedList() { head = null; } public boolean isEmpty() { retu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site