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
Develop a procedure for a linked list that removes duplicate keys at once.
Develop a procedure that will allow you to remove from a linked list all of the nodes that share a key?
For linked lists, use a natural mergesort. Since it doesn't take up extra space and is certain to be linearithmic, this method is the one used to sort linked lists.For linked lists, use a natural mergesort. Since it doesn't take up extra space and is certain to be linearithmic, this method is the one used to sort linked lists.For linked lists, use a natural mergesort. Since it doesn't take up extra space and is certain to be linearithmic, this method is the one used to sort linked lists.For linked lists, use a natural mergesort. Since it doesn't take up extra space and is certain to be linearithmic, this method is the one used to sort linked lists.For linked lists, use a natural mergesort. Since it doesn't take up extra space and is certain to be linearithmic, this method is the one used to sort linked lists.For linked lists, use a natural mergesort. Since it doesn't take up extra space and is certain to be linearithmic, this method is the one used to sort linked lists.For linked…
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