
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...

Want to see the full answer?
Check out a sample textbook solution
Chapter 3 Solutions
Data Structures and Algorithms in Java
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningNew Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage LearningSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr




