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 8R

Explanation of Solution

Method to find the middle node of doubly linked list:

This method moves right from head is assigned to middle node and at the same time moves left from tail is assigned to partner node.

  • If the middle and partner node is equal, then return the middle node.
  • If middle node is just next node of partner node, then it even number of elements. So, return the previous node of middle node.

Program:

//Define the middle() method

private Node<E> middle()

{

  //Check whether the list is empty

  if (size == 0)

  //Throw an exception

throw new IllegalStateException("List must not be empty");

  //Assign the next node of head to middle Node

  Node<E> middleNode = head−>next;

  //Assign the previous node of tail to partner Node

  Node<E> partnerNode = tail−>prev;

/*Loop executes until the middle node and partner node is not equal and next node of middle node and partner node is not equal. */

while (middleNode != partnerNode && middleNode−>next != partnerNode)

  {

  //Assign next node of middle node to middle node

  middleNode = middleNode.getNext();

/*Assign previous node of partner node to partner node...

Blurred answer
Students have asked these similar questions
Describe a method for finding the middle node of a doubly linked list with header and trailer sentinels by “link hopping,” and without relying on explicit knowledge of the size of the list. In the case of an even number of nodes, report the node slightly left of center as the “middle.” What is the running time of this method? Hint: Consider a combined search from both ends. Also, recall that a link hop is an assign- ment of the form ”p = p.getNext( );” or ”p = p.getPrev( );”.
Develop a procedure for a linked list that removes duplicate keys at once.
Given a scenario where you need to efficiently insert elements at the beginning of a list, which type of linked list would be most suitable? Explain why.
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