Question

Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

Java help: Please help complete the locate method that is in bold..

public class LinkedDoubleEndedList implements DoubleEndedList {

private Node front; // first node in list
private Node rear; // last node in list
private int size; // number of elements in list

//////////////////////////////////////////////////
// YOU MUST IMPLEMENT THE LOCATE METHOD BELOW //
//////////////////////////////////////////////////

/**
* Returns the position of the node containing the given value, where
* the front node is at position zero and the rear node is at position N
* in a list of size N. If value is not present, this method returns -1.
*/
public int locate(T value) {
   return 99;
}


/**
* Returns true if this list contains the given value, false otherwise.
*/
public boolean contains(T value) {
   return locate(value) >= 0;
}

/**
* Construct an empty list.
*/
public LinkedDoubleEndedList() {
front = null;
rear = null;
size = 0;
}

/**
* Adds element to the front of the list. If element is null,
* this method throws an IllegalArgumentException.
*/
public void addFirst(T element) {
if (element == null) {
throw new IllegalArgumentException("Null values not allowed in list.");
}
  
Node n = new Node(element, null, front);
if (isEmpty()) {
rear = n;
} else {
front.prev = n;
}
front = n;
size++;
}
  
/**
* Adds element to the end of the list. If element is null,
* this method throws an IllegalArgumentException.
*/
public void addLast(T element) {
if (element == null) {
throw new IllegalArgumentException("Null values not allowed in list.");
}
  
Node n = new Node(element, rear, null);
if (isEmpty()) {
front = n;
} else {
rear.next = n;
}
rear = n;
size++;
}
  
/**
* Delete and return the element at the front of the list.
* If the list is empty, this method returns null.
*/
public T removeFirst() {
if (isEmpty()) {
return null;
}
  
T element = front.element;
if (size() == 1) {
rear = null;
front = null;
} else {
Node n = front;
front = front.next;
front.prev = null;
n.next = null;
n = null;
}
  
size--;
return element;
}
  
/**
* Delete and return the element at the end of the list.
* If the list is empty, this method returns null.
*/
public T removeLast() {
if (isEmpty()) {
return null;
}
  
T element = rear.element;
if (size() == 1) {
front = null;
rear = null;
} else {
Node n = rear;
rear = rear.prev;
rear.next = null;
n.prev = null;
n = null;
}
  
size--;
return element;
}

/**
* Returns the number of elements in this list.
*/
public int size() {
return size;
}

/**
* Returns true if this list is empty, false otherwise.
*/
public boolean isEmpty() {
return size() == 0;
}
  
/**
* Creates and returns an iterator over the elements of this list.
* The order of the elements returned by this iterator is sequential
* from the front of the list to the end of the list.
*/
public Iterator iterator() {
return new SelfIterator();
}
  
  
/**
* Linked node class.
*/
private class Node {
  
private T element;
private Node prev;
private Node next;
  
public Node(T e, Node p, Node n) {
element = e;
prev = p;
next = n;
}
  
}

/**
* Provides iterator over linked nodes.
*
*/
private class SelfIterator implements Iterator {
  
private Node current;
  
public SelfIterator() {
current = front;
}
  
public boolean hasNext() {
return current != null;
}
  
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
T e = current.element;
current = current.next;
return e;
}
  
public void remove() {
throw new UnsupportedOperationException();
}
}   
}

***********************These are the additional files*******************************

public class Client {

   /** Drives execution. */
   public static void main(String[] args) {
       LinkedDoubleEndedList list = new LinkedDoubleEndedList<>();
       list.addLast("A");
       list.addLast("B");
       list.addLast("C");
       list.addLast("D");
       list.addLast("E");
       for (String value : list) {
           System.out.println(value + ": " + list.locate(value));
       }
       System.out.println("F: " + list.locate("F"));
   }
}

/*

RUNTIME OUTPUT:

A: 0
B: 1
C: 2
D: 3
E: 4
F: -1
********************************************************

public interface DoubleEndedList extends List {
  
/**
* Adds element to the front of the list. If element is null,
* this method throws an IllegalArgumentException.
*/
void addFirst(T element);
  
/**
* Adds element to the end of the list. If element is null,
* this method throws an IllegalArgumentException.
*/
void addLast(T element);
  
/**
* Delete and return the element at the front of the list.
* If the list is empty, this method returns null.
*/
T removeFirst();
  
/**
* Delete and return the element at the end of the list.
* If the list is empty, this method returns null.
*/
T removeLast();
  
}

*******************************************************************

public interface List extends Iterable {

/**
* Returns the number of elements in this list.
*/
int size();

/**
* Returns true if this list contains no elements, false otherwise.
*/
boolean isEmpty();
  
/**
* Creates and returns an iterator over the elements of this list.
*/
Iterator iterator();

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The locate () method is implemented as follows: //Given method public int locate (T value) //Kindly paste the below code in} /if the value is not present then return -1 return -1; } Explanation Initially a pointer is assigned for the front node Usi

code:

//Given method

public int locate(T value)

{

     //Kindly paste the below code inside locate() method

      // pointer to represent front node

      Node F = front;

      // declare the required variable

      int i = 0;

      // check whether the front node is not null

      while (F != null) {

// check whether the front node is at position 0 //and rear node at value

            if (F.element.equals(value))

          {

                  //retrun the results

                  return i;

            }

            //increment to the next node

            i++;

            //assign the next node

            F = F.next;

      }

     //if the value is not present then return -1

      return -1;

}

Add a comment
Know the answer?
Add Answer to:
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import...

    Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...

  • In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying...

    In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying nodes referenced by positions p and q to be exchanged for each other. Relink the existing nodes, do not create any new nodes. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- package lists; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedPositionalList implements PositionalList { //---------------- nested Node class ---------------- /** * Node of a doubly linked list, which stores a reference to its * element and to both the previous and next...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

  • package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic *...

    package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic * collection to store elements where indexes represent priorities and the * priorities can change in several ways. * * This collection class uses an Object[] data structure to store elements. * * @param <E> The type of all elements stored in this collection */ // You will have an error until you have have all methods // specified in interface PriorityList included inside this...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation...

    Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT