Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
Expert Solution & Answer
Book Icon
Chapter 3, Problem 15R

Explanation of Solution

Method equals() in CircularlyLinkedList class:

//Define the equals() method

public boolean equals(CircularlyLinkedList<E> other)

{

  //Declare and initailze the Boolean variable

boolean result = false;

/*Call size() to find the first list size and assign it into "size1" variable. */

int size1 = size();

/*Call size() to find the second list size and assign it into "size2" variable. */

int size2 = other.size();

//Check whether the size of the two lists is equal

if(size1 != size2)

  //Return false that is, two lists are not equal

  return result;

/*Call equals() method recursively to check first element in two lists are equal and the result into "result" variable. */

  result = head.equals(other.head);   

//Check whether "result" is not equal to false

if (!result)

//Return true that is, two lists are equal

  return result;   

/*Assign the next of head from current list into "next" node. */

Node<E> next = head.getNext();

/*Assign the next of head from other list into "nextOfOther" node. */

Node<E> nextOfOther = other.head.getNext();

/*Loop executes until the size of current list is greater than 1. */

  while (size1 > 1)

{

/*Call equals() method recursively to check all the elements in two lists are equal and assign the true if it is equal otherwise, false. */

result = next.equals(nextOfOther);

  //Check whether "result" is not equal to true

if (!result)

  // Break it, because, they aren't equal

  break;

/*Assign the next of next from current list into "next" node...

Blurred answer
Students have asked these similar questions
Design and implement a getLastHalf() method for the MyLinkedList class. The method should return the last half of the list as a new list leaving the original list intact. If the list has an uneven number of elements the extra one element must be in the list returned. Example : [1,3,5,4] return [5,4] and [1,6,9] return [6,9].     Follow the three step method for designing the method Develop the java code for the method Create a test program to test the method thoroughly using the Integer wrapper class.
Design and implement a getLastHalf() method for the MyLinkedList class. The method should return the last half of the list as a new list leaving the original list intact. If the list has an uneven number of elements the extra one element must be in the list returned. Example : [1,3,5,4] return [5,4] and [1,6,9] return [6,9].     Follow the three step method for designing the method Develop the java code for the method Create a test program to test the method thoroughly using the Integer wrapper class.     public class MyLinkedList<E> { private Node<E> head, tail; public MyLinkedList() { head = null; tail = null; } /** Return the head element in the list */ public E getFirst() { if (head == null) { return null; } else { return head.element; } } /** Return the last element in the list */ public E getLast() { if (head==null) { return null; } else { return tail.element; } } /** Add an element to the beginning of the list */ public void prepend(E e) { Node<E> newNode = new…
Implement unique method that takes an ArrayList as input and returns an ArrayList withexactly a single occurrence of each element from the input list. For example, if the input listcontains 5, 6, 7, 6, 3, 5, 5, 4 then the output list should have 5, 6, 7, 3, 4.Note: (1) You may assume that the ArrayList has a specific type of elements in it. In the class, we assumedit to have String type values. (2) You may only work with ArrayList(s).
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education