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 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
The intersection of two sets contains the list of elements that are common to both, without repetition. As such, you are required to write an algorithm that finds and returns the intersection of two sets A and B. The sets A and B and the resulting intersection set are all lists. Assume the size of A is n and that of B is m. (a) Write an iterative algorithm that finds the intersection. (b) Prove the correctness of your algorithm; i.e. state its loop invariant and prove it by induc- tion. (c) Analyze the time complexity of your algorithm. (d) Think of a way to enhance the complexity of your algorithm by sorting both lists A and B before calculating their intersection. Write the enhanced version and prove that it has a better time complexity.
Given a string formed with 0’s and 1’s. The string is marked with a special character X that represents the middle of the string (for example: 0101011X1101010). Write an algorithm to check whether the string is palindrome or not: (a) If the string is stored as an array (b) If the string is stored as a singlv linked list
C Write a recursive algorithm mypowerlist (n) that, given a nat- ural number n as input, returns the "powerlist" (i.e. the power- set expressed as a list) of {1, ...,n}. So the output of mypowerlist (2) should be the following list of sets: [set(),{1},{2}, {1,2}]. Hint: it is obvious what the solution is for n=0. For n+1 note that there are two kinds of subset: those containing n+1, and those not. The latter can be obtained immediately by calling mypowerset (n). How can you obtain the first group?
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