Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781119278023
Author: Michael T. Goodrich; Roberto Tamassia; Michael H. Goldwasser
Publisher: Wiley Global Education US
Expert Solution & Answer
Book Icon
Chapter 3, Problem 29C

Explanation of Solution

Algorithm to compare two circularly linked lists is same:

The algorithm to compare two circularly linked lists “L” and “M” are same sequence of elements given below:

Algorithm:

Input: Two circularly linked list “L” and “M”.

Output: Return true when two circular linked lists “L” and “M” are same sequence of elements. Otherwise, return false.

equal(L :Circularly linked list, M Circularly linked list):

    //Create new node for list "L" and "M"

  Create a new node "a" and "b"

/*Call getHead() method using circularly linked list "L" to assign the head of list as "a". */

  a = L.getHead();

/*Call getHead() method using circularly linked list "M" to assign the head of list as "b". */

  b = M.getHead();

/*Loop executes until the next node of list is not equal to "null" for both lists. */

  while (a.getNext()!= null || b.getNext()!= null)

  {

/*Loop executes until both list elements are not equal. */

  while(a.getElement() != b.getElement())

  //Assign next node as "b"

  b = b.getNext();

  //Assign next node as "a"

  a = a.getNext();

/*Call getHead() method using circularly linked list "M" to reassign the head of list as "b". */

  b = M...

Blurred answer
Students have asked these similar questions
D. S. Malik, Data Structures Using C++, 2nd Edition, 2010
Methods (Ch6) - Review 1. (The MyRoot method) Below is a manual implementation of the Math.sqrt() method in Java. There are two methods, method #1 which calculates the square root for positive integers, and method #2, which calculates the square root of positive doubles (also works for integers). public class SquareRoot { public static void main(String[] args) { } // implement a loop of your choice here // Method that calculates the square root of integer variables public static double myRoot(int number) { double root; root=number/2; double root old; do { root old root; root (root_old+number/root_old)/2; } while (Math.abs(root_old-root)>1.8E-6); return root; } // Method that calculates the square root of double variables public static double myRoot(double number) { double root; root number/2; double root_old; do { root old root; root (root_old+number/root_old)/2; while (Math.abs (root_old-root)>1.0E-6); return root; } } Program-it-Yourself: In the main method, create a program that…
I would like to know the main features about the following 3 key concepts:1. Backup Domain Controller (BDC)2. Access Control List (ACL)3. Dynamic Memory
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning