SlideShare a Scribd company logo
1 of 12
Download to read offline
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 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
*/
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
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
} //----------- 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
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
/**
* 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
*
* 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 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
*/
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
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
} //----------- end of DoublyLinkedList class -----------

More Related Content

Similar to practice of using and manipulating a doubly linked list and (2) prac.pdf

we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdfgudduraza28
 
Please do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfPlease do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfaioils
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfsauravmanwanicp
 
Describe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdfDescribe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdfdeepak596396
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfConint29
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docxVictorXUQGloverl
 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdfsaradashata
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxgilliandunce53776
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxcgraciela1
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfshalins6
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfSTAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfbabitasingh698417
 
we using java dynamicArray package modellinearpub imp.pdf
we using java dynamicArray    package modellinearpub   imp.pdfwe using java dynamicArray    package modellinearpub   imp.pdf
we using java dynamicArray package modellinearpub imp.pdfadityagupta3310
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxBrianGHiNewmanv
 
Implement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfImplement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfudit652068
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxVictorzH8Bondx
 
please read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfplease read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfaggarwalopticalsco
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxSHIVA101531
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfannaelctronics
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfcontact41
 

Similar to practice of using and manipulating a doubly linked list and (2) prac.pdf (20)

we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
 
Please do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfPlease do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdf
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
 
Describe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdfDescribe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdf
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
 
1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf1 The goal is to implement DataStructuresArrayStack accor.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdf
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfSTAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
 
we using java dynamicArray package modellinearpub imp.pdf
we using java dynamicArray    package modellinearpub   imp.pdfwe using java dynamicArray    package modellinearpub   imp.pdf
we using java dynamicArray package modellinearpub imp.pdf
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
 
Implement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdfImplement the following specification of UnsortedType using circular.pdf
Implement the following specification of UnsortedType using circular.pdf
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
 
please read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfplease read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docx
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
 
In this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdfIn this lab, we will write an application to store a deck of cards i.pdf
In this lab, we will write an application to store a deck of cards i.pdf
 

More from rd1742

PR RESEARCHCriticize any advertisement of choice. ( Billboard, .pdf
PR RESEARCHCriticize any advertisement of choice.  ( Billboard, .pdfPR RESEARCHCriticize any advertisement of choice.  ( Billboard, .pdf
PR RESEARCHCriticize any advertisement of choice. ( Billboard, .pdfrd1742
 
Post a paper of at least 1,000 words to cover the followingDescri.pdf
Post a paper of at least 1,000 words to cover the followingDescri.pdfPost a paper of at least 1,000 words to cover the followingDescri.pdf
Post a paper of at least 1,000 words to cover the followingDescri.pdfrd1742
 
Por lo general, las oportunidades y amenazas externas son ...........pdf
Por lo general, las oportunidades y amenazas externas son ...........pdfPor lo general, las oportunidades y amenazas externas son ...........pdf
Por lo general, las oportunidades y amenazas externas son ...........pdfrd1742
 
Por lo general, todo lo siguiente debe incluirse como ingreso sujeto.pdf
Por lo general, todo lo siguiente debe incluirse como ingreso sujeto.pdfPor lo general, todo lo siguiente debe incluirse como ingreso sujeto.pdf
Por lo general, todo lo siguiente debe incluirse como ingreso sujeto.pdfrd1742
 
por favor ayuda dar pulgares arriba. Caso de estudio Empresas ex.pdf
por favor ayuda dar pulgares arriba. Caso de estudio Empresas ex.pdfpor favor ayuda dar pulgares arriba. Caso de estudio Empresas ex.pdf
por favor ayuda dar pulgares arriba. Caso de estudio Empresas ex.pdfrd1742
 
PMBOK 7e emphasizes the need for project leaders�and, ideally, all p.pdf
PMBOK 7e emphasizes the need for project leaders�and, ideally, all p.pdfPMBOK 7e emphasizes the need for project leaders�and, ideally, all p.pdf
PMBOK 7e emphasizes the need for project leaders�and, ideally, all p.pdfrd1742
 
POOL=1 if house has a pool, 0 otherwise FPLACE =1 if house has a firep.pdf
POOL=1 if house has a pool, 0 otherwise FPLACE =1 if house has a firep.pdfPOOL=1 if house has a pool, 0 otherwise FPLACE =1 if house has a firep.pdf
POOL=1 if house has a pool, 0 otherwise FPLACE =1 if house has a firep.pdfrd1742
 
Poison Ivy - Pamela Isley proven�a de una familia adinerada, aunque .pdf
Poison Ivy - Pamela Isley proven�a de una familia adinerada, aunque .pdfPoison Ivy - Pamela Isley proven�a de una familia adinerada, aunque .pdf
Poison Ivy - Pamela Isley proven�a de una familia adinerada, aunque .pdfrd1742
 
Por favor conteste ambos El embargo de petr�leo de la OPEP de los .pdf
Por favor conteste ambos El embargo de petr�leo de la OPEP de los .pdfPor favor conteste ambos El embargo de petr�leo de la OPEP de los .pdf
Por favor conteste ambos El embargo de petr�leo de la OPEP de los .pdfrd1742
 
plz help 12. Assume that John and Kevin want to incorporate a busi.pdf
plz help 12. Assume that John and Kevin want to incorporate a busi.pdfplz help 12. Assume that John and Kevin want to incorporate a busi.pdf
plz help 12. Assume that John and Kevin want to incorporate a busi.pdfrd1742
 
Population DistributionNamaJajan perhariCarlos $ .pdf
Population DistributionNamaJajan perhariCarlos $            .pdfPopulation DistributionNamaJajan perhariCarlos $            .pdf
Population DistributionNamaJajan perhariCarlos $ .pdfrd1742
 
Population of Ulundi local municipality on June 30, 2022 = 250,000. .pdf
Population of Ulundi local municipality on June 30, 2022 = 250,000. .pdfPopulation of Ulundi local municipality on June 30, 2022 = 250,000. .pdf
Population of Ulundi local municipality on June 30, 2022 = 250,000. .pdfrd1742
 
Pregunta 3 Divanee es neurocirujano en el hospital local. Sin emba.pdf
Pregunta 3 Divanee es neurocirujano en el hospital local. Sin emba.pdfPregunta 3 Divanee es neurocirujano en el hospital local. Sin emba.pdf
Pregunta 3 Divanee es neurocirujano en el hospital local. Sin emba.pdfrd1742
 
plz help 16. Assume the same facts as the prece ding question. Wha.pdf
plz help 16. Assume the same facts as the prece ding question. Wha.pdfplz help 16. Assume the same facts as the prece ding question. Wha.pdf
plz help 16. Assume the same facts as the prece ding question. Wha.pdfrd1742
 
Por favor responda a todos. S� que algunas respuestas son correctas .pdf
Por favor responda a todos. S� que algunas respuestas son correctas .pdfPor favor responda a todos. S� que algunas respuestas son correctas .pdf
Por favor responda a todos. S� que algunas respuestas son correctas .pdfrd1742
 
Por favor conteste todas las preguntas. Solo las respuestas estar�n .pdf
Por favor conteste todas las preguntas. Solo las respuestas estar�n .pdfPor favor conteste todas las preguntas. Solo las respuestas estar�n .pdf
Por favor conteste todas las preguntas. Solo las respuestas estar�n .pdfrd1742
 
PREGUNTA 3 El participante m�s activo e importante en el mercado m.pdf
PREGUNTA 3 El participante m�s activo e importante en el mercado m.pdfPREGUNTA 3 El participante m�s activo e importante en el mercado m.pdf
PREGUNTA 3 El participante m�s activo e importante en el mercado m.pdfrd1742
 
Pregunta 4 (4 puntos) �Cu�les de las siguientes pueden ser considera.pdf
Pregunta 4 (4 puntos) �Cu�les de las siguientes pueden ser considera.pdfPregunta 4 (4 puntos) �Cu�les de las siguientes pueden ser considera.pdf
Pregunta 4 (4 puntos) �Cu�les de las siguientes pueden ser considera.pdfrd1742
 
Pregunta 4 (1 punto) Los m�todos anal�ticos son preferibles a la.pdf
Pregunta 4 (1 punto) Los m�todos anal�ticos son preferibles a la.pdfPregunta 4 (1 punto) Los m�todos anal�ticos son preferibles a la.pdf
Pregunta 4 (1 punto) Los m�todos anal�ticos son preferibles a la.pdfrd1742
 
Pregunta 34 Dos etapas de vida diferentes se encuentran en las pla.pdf
Pregunta 34 Dos etapas de vida diferentes se encuentran en las pla.pdfPregunta 34 Dos etapas de vida diferentes se encuentran en las pla.pdf
Pregunta 34 Dos etapas de vida diferentes se encuentran en las pla.pdfrd1742
 

More from rd1742 (20)

PR RESEARCHCriticize any advertisement of choice. ( Billboard, .pdf
PR RESEARCHCriticize any advertisement of choice.  ( Billboard, .pdfPR RESEARCHCriticize any advertisement of choice.  ( Billboard, .pdf
PR RESEARCHCriticize any advertisement of choice. ( Billboard, .pdf
 
Post a paper of at least 1,000 words to cover the followingDescri.pdf
Post a paper of at least 1,000 words to cover the followingDescri.pdfPost a paper of at least 1,000 words to cover the followingDescri.pdf
Post a paper of at least 1,000 words to cover the followingDescri.pdf
 
Por lo general, las oportunidades y amenazas externas son ...........pdf
Por lo general, las oportunidades y amenazas externas son ...........pdfPor lo general, las oportunidades y amenazas externas son ...........pdf
Por lo general, las oportunidades y amenazas externas son ...........pdf
 
Por lo general, todo lo siguiente debe incluirse como ingreso sujeto.pdf
Por lo general, todo lo siguiente debe incluirse como ingreso sujeto.pdfPor lo general, todo lo siguiente debe incluirse como ingreso sujeto.pdf
Por lo general, todo lo siguiente debe incluirse como ingreso sujeto.pdf
 
por favor ayuda dar pulgares arriba. Caso de estudio Empresas ex.pdf
por favor ayuda dar pulgares arriba. Caso de estudio Empresas ex.pdfpor favor ayuda dar pulgares arriba. Caso de estudio Empresas ex.pdf
por favor ayuda dar pulgares arriba. Caso de estudio Empresas ex.pdf
 
PMBOK 7e emphasizes the need for project leaders�and, ideally, all p.pdf
PMBOK 7e emphasizes the need for project leaders�and, ideally, all p.pdfPMBOK 7e emphasizes the need for project leaders�and, ideally, all p.pdf
PMBOK 7e emphasizes the need for project leaders�and, ideally, all p.pdf
 
POOL=1 if house has a pool, 0 otherwise FPLACE =1 if house has a firep.pdf
POOL=1 if house has a pool, 0 otherwise FPLACE =1 if house has a firep.pdfPOOL=1 if house has a pool, 0 otherwise FPLACE =1 if house has a firep.pdf
POOL=1 if house has a pool, 0 otherwise FPLACE =1 if house has a firep.pdf
 
Poison Ivy - Pamela Isley proven�a de una familia adinerada, aunque .pdf
Poison Ivy - Pamela Isley proven�a de una familia adinerada, aunque .pdfPoison Ivy - Pamela Isley proven�a de una familia adinerada, aunque .pdf
Poison Ivy - Pamela Isley proven�a de una familia adinerada, aunque .pdf
 
Por favor conteste ambos El embargo de petr�leo de la OPEP de los .pdf
Por favor conteste ambos El embargo de petr�leo de la OPEP de los .pdfPor favor conteste ambos El embargo de petr�leo de la OPEP de los .pdf
Por favor conteste ambos El embargo de petr�leo de la OPEP de los .pdf
 
plz help 12. Assume that John and Kevin want to incorporate a busi.pdf
plz help 12. Assume that John and Kevin want to incorporate a busi.pdfplz help 12. Assume that John and Kevin want to incorporate a busi.pdf
plz help 12. Assume that John and Kevin want to incorporate a busi.pdf
 
Population DistributionNamaJajan perhariCarlos $ .pdf
Population DistributionNamaJajan perhariCarlos $            .pdfPopulation DistributionNamaJajan perhariCarlos $            .pdf
Population DistributionNamaJajan perhariCarlos $ .pdf
 
Population of Ulundi local municipality on June 30, 2022 = 250,000. .pdf
Population of Ulundi local municipality on June 30, 2022 = 250,000. .pdfPopulation of Ulundi local municipality on June 30, 2022 = 250,000. .pdf
Population of Ulundi local municipality on June 30, 2022 = 250,000. .pdf
 
Pregunta 3 Divanee es neurocirujano en el hospital local. Sin emba.pdf
Pregunta 3 Divanee es neurocirujano en el hospital local. Sin emba.pdfPregunta 3 Divanee es neurocirujano en el hospital local. Sin emba.pdf
Pregunta 3 Divanee es neurocirujano en el hospital local. Sin emba.pdf
 
plz help 16. Assume the same facts as the prece ding question. Wha.pdf
plz help 16. Assume the same facts as the prece ding question. Wha.pdfplz help 16. Assume the same facts as the prece ding question. Wha.pdf
plz help 16. Assume the same facts as the prece ding question. Wha.pdf
 
Por favor responda a todos. S� que algunas respuestas son correctas .pdf
Por favor responda a todos. S� que algunas respuestas son correctas .pdfPor favor responda a todos. S� que algunas respuestas son correctas .pdf
Por favor responda a todos. S� que algunas respuestas son correctas .pdf
 
Por favor conteste todas las preguntas. Solo las respuestas estar�n .pdf
Por favor conteste todas las preguntas. Solo las respuestas estar�n .pdfPor favor conteste todas las preguntas. Solo las respuestas estar�n .pdf
Por favor conteste todas las preguntas. Solo las respuestas estar�n .pdf
 
PREGUNTA 3 El participante m�s activo e importante en el mercado m.pdf
PREGUNTA 3 El participante m�s activo e importante en el mercado m.pdfPREGUNTA 3 El participante m�s activo e importante en el mercado m.pdf
PREGUNTA 3 El participante m�s activo e importante en el mercado m.pdf
 
Pregunta 4 (4 puntos) �Cu�les de las siguientes pueden ser considera.pdf
Pregunta 4 (4 puntos) �Cu�les de las siguientes pueden ser considera.pdfPregunta 4 (4 puntos) �Cu�les de las siguientes pueden ser considera.pdf
Pregunta 4 (4 puntos) �Cu�les de las siguientes pueden ser considera.pdf
 
Pregunta 4 (1 punto) Los m�todos anal�ticos son preferibles a la.pdf
Pregunta 4 (1 punto) Los m�todos anal�ticos son preferibles a la.pdfPregunta 4 (1 punto) Los m�todos anal�ticos son preferibles a la.pdf
Pregunta 4 (1 punto) Los m�todos anal�ticos son preferibles a la.pdf
 
Pregunta 34 Dos etapas de vida diferentes se encuentran en las pla.pdf
Pregunta 34 Dos etapas de vida diferentes se encuentran en las pla.pdfPregunta 34 Dos etapas de vida diferentes se encuentran en las pla.pdf
Pregunta 34 Dos etapas de vida diferentes se encuentran en las pla.pdf
 

Recently uploaded

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 

Recently uploaded (20)

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

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
  • 12. } //----------- end of DoublyLinkedList class -----------