practice of using and manipulating a doubly linked list and (2) practice of designing and
implementing a small recursive method. Write a program named Hw2_p3.java that implements
the following requirement:
This method receives a doubly linked list that stores integers.
It reverses order of all integers in the list.
This must be done a recursive manner.
The signature of the method must be:
public static void reverse(DoublyLinkedList intList)
You may want to write a separate method with additional parameters (refer to page 214 of the
textbook).
You may not use additional storage, such as another linked list, arrays, stacks, or queues. The
rearrangement of integers must occur within the given linked list.
An incomplete Hw2_p3.java is provided. You must complete this program.
You must use the DoublyLinkedList class that is posted along with this assignment. You must
not use the DoublyLinkedList class that is included in textbooks source code collection.
Note that you must not modify the given DoublyLinkedList class and the DoubleLinkNode class.
package LinkedList;
/*
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
*
* Developed for use with the book:
*
* Data Structures and Algorithms in Java, Sixth Edition
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
* John Wiley & Sons, 2014
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* A basic doubly linked list implementation.
*
* @author Michael T. Goodrich
* @author Roberto Tamassia
* @author Michael H. Goldwasser
*/
// modifed for a course assignment
public class DoublyLinkedList {
// instance variables of the DoublyLinkedList
/** Sentinel node at the beginning of the list */
private DoubleLinkNode header; // header sentinel
/** Sentinel node at the end of the list */
private DoubleLinkNode trailer; // trailer sentinel
/** Number of elements in the list (not including sentinels) */
private int size = 0; // number of elements in the list
/** Constructs a new empty list. */
public DoublyLinkedList() {
header = new DoubleLinkNode<>(null, null, null); // create header
trailer = new DoubleLinkNode<>(null, header, null); // trailer is preceded by header
header.setNext(trailer); // header is followed by trailer
}
public DoubleLinkNode getHeader() {return header;}
public DoubleLinkNode getTrailer() {return trailer;}
public void setHeader(DoubleLinkNode h) {header = h;}
public void setTrailer(DoubleLinkNode.
practice of using and manipulating a doubly linked list and (2) prac.pdf
1. practice of using and manipulating a doubly linked list and (2) practice of designing and
implementing a small recursive method. Write a program named Hw2_p3.java that implements
the following requirement:
This method receives a doubly linked list that stores integers.
It reverses order of all integers in the list.
This must be done a recursive manner.
The signature of the method must be:
public static void reverse(DoublyLinkedList intList)
You may want to write a separate method with additional parameters (refer to page 214 of the
textbook).
You may not use additional storage, such as another linked list, arrays, stacks, or queues. The
rearrangement of integers must occur within the given linked list.
An incomplete Hw2_p3.java is provided. You must complete this program.
You must use the DoublyLinkedList class that is posted along with this assignment. You must
not use the DoublyLinkedList class that is included in textbooks source code collection.
Note that you must not modify the given DoublyLinkedList class and the DoubleLinkNode class.
package LinkedList;
/*
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
*
* Developed for use with the book:
*
* Data Structures and Algorithms in Java, Sixth Edition
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
* John Wiley & Sons, 2014
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
2. *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* A basic doubly linked list implementation.
*
* @author Michael T. Goodrich
* @author Roberto Tamassia
* @author Michael H. Goldwasser
*/
// modifed for a course assignment
public class DoublyLinkedList {
// instance variables of the DoublyLinkedList
/** Sentinel node at the beginning of the list */
private DoubleLinkNode header; // header sentinel
/** Sentinel node at the end of the list */
private DoubleLinkNode trailer; // trailer sentinel
/** Number of elements in the list (not including sentinels) */
private int size = 0; // number of elements in the list
/** Constructs a new empty list. */
public DoublyLinkedList() {
header = new DoubleLinkNode<>(null, null, null); // create header
trailer = new DoubleLinkNode<>(null, header, null); // trailer is preceded by header
header.setNext(trailer); // header is followed by trailer
}
public DoubleLinkNode getHeader() {return header;}
public DoubleLinkNode getTrailer() {return trailer;}
public void setHeader(DoubleLinkNode h) {header = h;}
public void setTrailer(DoubleLinkNode t) {trailer = t;}
// public accessor methods
3. /**
* Returns the number of elements in the linked list.
* @return number of elements in the linked list
*/
public int size() { return size; }
/**
* Tests whether the linked list is empty.
* @return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() { return size == 0; }
/**
* Returns (but does not remove) the first element of the list.
* @return element at the front of the list (or null if empty)
*/
public E first() {
if (isEmpty()) return null;
return header.getNext().getElement(); // first element is beyond header
}
/**
* Returns (but does not remove) the last element of the list.
* @return element at the end of the list (or null if empty)
*/
public E last() {
if (isEmpty()) return null;
return trailer.getPrev().getElement(); // last element is before trailer
}
// public update methods
/**
* Adds an element to the front of the list.
* @param e the new element to add
*/
public void addFirst(E e) {
addBetween(e, header, header.getNext()); // place just after the header
}
/**
* Adds an element to the end of the list.
4. * @param e the new element to add
*/
public void addLast(E e) {
addBetween(e, trailer.getPrev(), trailer); // place just before the trailer
}
/**
* Removes and returns the first element of the list.
* @return the removed element (or null if empty)
*/
public E removeFirst() {
if (isEmpty()) return null; // nothing to remove
return remove(header.getNext()); // first element is beyond header
}
/**
* Removes and returns the last element of the list.
* @return the removed element (or null if empty)
*/
public E removeLast() {
if (isEmpty()) return null; // nothing to remove
return remove(trailer.getPrev()); // last element is before trailer
}
/**
* Adds an element to the linked list in between the given nodes.
* The given predecessor and successor should be neighboring each
* other prior to the call.
*
* @param predecessor node just before the location where the new element is inserted
* @param successor node just after the location where the new element is inserted
*/
public void addBetween(E e, DoubleLinkNode predecessor, DoubleLinkNode successor) {
// create and link a new node
DoubleLinkNode newest = new DoubleLinkNode<>(e, predecessor, successor);
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
}
5. /**
* Removes the given node from the list and returns its element.
* @param node the node to be removed (must not be a sentinel)
*/
public E remove(DoubleLinkNode node) {
DoubleLinkNode predecessor = node.getPrev();
DoubleLinkNode successor = node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getElement();
}
/**
* Produces a string representation of the contents of the list.
* This exists for debugging purposes only.
*/
public String toString() {
StringBuilder sb = new StringBuilder("(");
DoubleLinkNode walk = header.getNext();
while (walk != trailer) {
sb.append(walk.getElement());
walk = walk.getNext();
if (walk != trailer)
sb.append(", ");
}
sb.append(")");
return sb.toString();
}
// added
// insertion sort
} //----------- end of DoublyLinkedList class -----------
public class Hw2_p3 {
// implement reverse method
// you may want to write a separate method with additional parameters, which is recursive
6. public static void reverse(DoublyLinkedList intList) {
// complete this method
}
// use the main method for testing
// test with arrays of different lenghts
public static void main(String[] args) {
DoublyLinkedList intList = new DoublyLinkedList<>();
int[] a = {10, 20, 30, 40, 50};
for (int i=0; i();
int[] b = {10, 20, 30, 40, 50, 60};
for (int i=0; i {
/** The element stored at this DoubleLinkNode */
private E element; // reference to the element stored at this DoubleLinkNode
/** A reference to the preceding DoubleLinkNode in the list */
private DoubleLinkNode prev; // reference to the previous DoubleLinkNode in the list
/** A reference to the subsequent DoubleLinkNode in the list */
private DoubleLinkNode next; // reference to the subsequent DoubleLinkNode in the
list
/**
* Creates a DoubleLinkNode with the given element and next DoubleLinkNode.
*
* @param e the element to be stored
* @param p reference to a DoubleLinkNode that should precede the new DoubleLinkNode
* @param n reference to a DoubleLinkNode that should follow the new DoubleLinkNode
*/
public DoubleLinkNode(E e, DoubleLinkNode p, DoubleLinkNode n) {
element = e;
prev = p;
next = n;
}
// public accessor methods
/**
7. * Returns the element stored at the DoubleLinkNode.
* @return the element stored at the DoubleLinkNode
*/
public E getElement() { return element; }
/**
* Returns the DoubleLinkNode that precedes this one (or null if no such DoubleLinkNode).
* @return the preceding DoubleLinkNode
*/
public DoubleLinkNode getPrev() { return prev; }
/**
* Returns the DoubleLinkNode that follows this one (or null if no such DoubleLinkNode).
* @return the following DoubleLinkNode
*/
public DoubleLinkNode getNext() { return next; }
// Update methods
/**
* Sets the DoubleLinkNode's previous reference to point to DoubleLinkNode n.
* @param p the DoubleLinkNode that should precede this one
*/
public void setPrev(DoubleLinkNode p) { prev = p; }
/**
* Sets the DoubleLinkNode's next reference to point to DoubleLinkNode n.
* @param n the DoubleLinkNode that should follow this one
*/
public void setNext(DoubleLinkNode n) { next = n; }
}
__________
package LinkedList;
/*
* Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
*
* Developed for use with the book:
*
* Data Structures and Algorithms in Java, Sixth Edition
* Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
* John Wiley & Sons, 2014
8. *
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* A basic doubly linked list implementation.
*
* @author Michael T. Goodrich
* @author Roberto Tamassia
* @author Michael H. Goldwasser
*/
// modifed for a course assignment
public class DoublyLinkedList {
// instance variables of the DoublyLinkedList
/** Sentinel node at the beginning of the list */
private DoubleLinkNode header; // header sentinel
/** Sentinel node at the end of the list */
private DoubleLinkNode trailer; // trailer sentinel
/** Number of elements in the list (not including sentinels) */
private int size = 0; // number of elements in the list
/** Constructs a new empty list. */
public DoublyLinkedList() {
header = new DoubleLinkNode<>(null, null, null); // create header
trailer = new DoubleLinkNode<>(null, header, null); // trailer is preceded by header
header.setNext(trailer); // header is followed by trailer
}
9. public DoubleLinkNode getHeader() {return header;}
public DoubleLinkNode getTrailer() {return trailer;}
public void setHeader(DoubleLinkNode h) {header = h;}
public void setTrailer(DoubleLinkNode t) {trailer = t;}
// public accessor methods
/**
* Returns the number of elements in the linked list.
* @return number of elements in the linked list
*/
public int size() { return size; }
/**
* Tests whether the linked list is empty.
* @return true if the linked list is empty, false otherwise
*/
public boolean isEmpty() { return size == 0; }
/**
* Returns (but does not remove) the first element of the list.
* @return element at the front of the list (or null if empty)
*/
public E first() {
if (isEmpty()) return null;
return header.getNext().getElement(); // first element is beyond header
}
/**
* Returns (but does not remove) the last element of the list.
* @return element at the end of the list (or null if empty)
*/
public E last() {
if (isEmpty()) return null;
return trailer.getPrev().getElement(); // last element is before trailer
}
// public update methods
/**
* Adds an element to the front of the list.
* @param e the new element to add
*/
10. public void addFirst(E e) {
addBetween(e, header, header.getNext()); // place just after the header
}
/**
* Adds an element to the end of the list.
* @param e the new element to add
*/
public void addLast(E e) {
addBetween(e, trailer.getPrev(), trailer); // place just before the trailer
}
/**
* Removes and returns the first element of the list.
* @return the removed element (or null if empty)
*/
public E removeFirst() {
if (isEmpty()) return null; // nothing to remove
return remove(header.getNext()); // first element is beyond header
}
/**
* Removes and returns the last element of the list.
* @return the removed element (or null if empty)
*/
public E removeLast() {
if (isEmpty()) return null; // nothing to remove
return remove(trailer.getPrev()); // last element is before trailer
}
/**
* Adds an element to the linked list in between the given nodes.
* The given predecessor and successor should be neighboring each
* other prior to the call.
*
* @param predecessor node just before the location where the new element is inserted
* @param successor node just after the location where the new element is inserted
*/
public void addBetween(E e, DoubleLinkNode predecessor, DoubleLinkNode successor) {
// create and link a new node
11. DoubleLinkNode newest = new DoubleLinkNode<>(e, predecessor, successor);
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
}
/**
* Removes the given node from the list and returns its element.
* @param node the node to be removed (must not be a sentinel)
*/
public E remove(DoubleLinkNode node) {
DoubleLinkNode predecessor = node.getPrev();
DoubleLinkNode successor = node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getElement();
}
/**
* Produces a string representation of the contents of the list.
* This exists for debugging purposes only.
*/
public String toString() {
StringBuilder sb = new StringBuilder("(");
DoubleLinkNode walk = header.getNext();
while (walk != trailer) {
sb.append(walk.getElement());
walk = walk.getNext();
if (walk != trailer)
sb.append(", ");
}
sb.append(")");
return sb.toString();
}
// added
// insertion sort