ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 25 May 2017 My Price 8.00

public class SinglyLinkedList

public class SinglyLinkedList<E> implements List<E>{
    private static class Node<E>{
      private E value;
      private Node<E> next;
      private Node(E value, Node<E> next){
          this.value = value;
          this.next = next;
      }
   }
   private Node<E> head;

//class continues after that
public boolean mystery1(E o){
    if (o == null){
        throw new NullPointerException("null");
    }
    if (head == null){
        return false;
    }
    boolean result = mystery1(head, o);
    if (head.value.equals(o)){
        head = head.next;
        return true;
    } else{
        return result;
    }
}

private boolean mystery1(Node<E> p, E o){
    if (p.next == null){
        return false;
    }
    boolean result = mystery1(p.next, o);
    if (p.next.value.equals(o)){
        p.next = p.next.next;
        return true;
    } else{
        return result;
    }
}

SinglyLinkedList<String> list = new SinglyLinkedList<String>();

 

list.add("A"); list.add("B"); list.add("C");

list.add("A"); list.add("B"); list.add("C");

list.add("A"); list.add("B"); list.add("C");

System.out.println(list);

System.out.println(list.mystery1("A"));

System.out.println(list);

System.out.println(list.mystery1("C"));

System.out.println(list);

System.out.println(list.mystery1("A"));

System.out.println(list);

Solution:
[A, B, C, A, B, C, A, B, C]
true
[B, C, B, C, B, C]
true
[B, B, B]
false
[B, B, B]

Question:
Can you explain step by step how do you get the answer when the function mystery1 is called? and how does recursion work in this program

Answers

(11)
Status NEW Posted 25 May 2017 02:05 AM My Price 8.00

-----------

Not Rated(0)