Modify the List class of Fig. 21.3 to include method search that recursively searches a linked-list object
Question:
Modify the List class of Fig. 21.3 to include method search that recursively searches a linked-list object for a specified value. The method should return a reference to the value if it’s found; otherwise, it should return null. Use your method in a test program that creates a list of integers. The program should prompt the user for a value to locate in the list.
Fig. 21.3
Transcribed Image Text:
I // Fig. 21.3: List.java 2 // ListNode and List class declarations. 3 package com.deitel.datastructures; 4 5 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 32 33 34 35 36 37 38 39 40 41 42 43 44 27 28 } 29 30 // class List definition 31 public class List { 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 import java.util. NoSuch ElementException; // class to represent one node in a list class ListNode { // package access members; List can access these directly E data; // data for this node ListNode nextNode; // reference to the next node in the list 126 127 // constructor creates a ListNode that refers to object ListNode(E object) {this (object, null); } 128 129 // constructor creates ListNode that refers to the specified // object and to the next ListNode ListNode (E object, ListNode node) { data object; nextNode node; } // return reference to data in node E getData() {return data; } // return reference to next node in list ListNode getNext() {return nextNode;} private ListNode firstNode; private ListNode lastNode; private String name; // string like "list" used in printing // constructor creates empty List with "list" as the name public List() {this("list");} // constructor creates an empty List with a name public List(String listName) { name } // insert item at front of List public void insertAtFront (E insertItem) { if (isEmpty()) { // firstNode and lastNode refer to same object firstNode = lastNode = new ListNode (insertItem); } listName; firstNode = lastNode= null; } } else { // firstNode refers to new node firstNode = new ListNode (insertItem, firstNode); // insert item at end of List public void insertAtBack(E insertItem) { if (isEmpty()) { // firstNode and lastNode refer to same object firstNode lastNode = new ListNode (insertItem); } } else { // lastNode's nextNode refers to new node lastNode = lastNode.nextNode = new ListNode (insertItem); 130 131 } 132 } } // remove first node from List public E removeFromFront () throws NoSuchElementException { if (isEmpty()) { // throw exception if List is empty throw new NoSuch ElementException (name + " is empty"); } E removedItem = firstNode.data; // retrieve data being removed // update references firstNode and lastNode if (firstNode = lastNode) { firstNode = lastNode = null; } else { } return removedItem; // return removed node data } // remove last node from List public E removeFromBack () throws NoSuchElementException { if (isEmpty () { // throw exception if List is empty throw new NoSuch ElementException (name + " is empty"); } E removedItem = lastNode.data; // retrieve data being removed // update references firstNode and lastNode if (firstNode == lastNode) { firstNode firstNode.nextNode; } } else { // locate new last node ListNode current = firstNode; firstNode = lastNode = null; 106 107 108 109 } 110 [[| // determine whether list is empty; returns true if so public boolean isEmpty() {return firstNode == null; } 112 113 114] 115 116 117 118 119 120 121 122 123 124 124 125 } // loop while current node does not refer to lastNode while (current.nextNode != lastNode) { current = current.nextNode; } lastNode = current; // current is new lastNode current.nextNode = null; return removedItem; // return removed node data // output list contents public void print() { if (isEmpty()) { } System.out.printf("Empty %s%n", name); return; System.out.printf("The %s is: ", name); ListNode current = firstNode; // while not at end of list, output current node's data while (current != null) { System.out.printf("%s", current.data); current current.nextNode; System.out.println();
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 75% (4 reviews)
To fulfill the questions requirement we need to add a search method to the List class that employs r...View the full answer
Answered By
Branice Buyengo Ajevi
I have been teaching for the last 5 years which has strengthened my interaction with students of different level.
4.30+
1+ Reviews
10+ Question Solved
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Question Posted:
Students also viewed these Computer science questions
-
Modify the List class of Fig. 21.3 to include method printListBackward that recursively outputs the items in a linked-list object in reverse order. Write a test program that creates a list of...
-
Modify the SortedList class from Exercise 21.7 to include a merge method that can merge the SortedList it receives as an argument with the SortedList that calls the method. Write an application to...
-
As presented in the text, linked lists must be searched sequentially. For large lists, this can result in poor performance. A common technique for improving list-searching performance is to create...
-
In Problems 1130, solve each equation by factoring. x 2 - 9x = 0
-
(a) For an excited state of hydrogen, show that the smallest angle that the orbital angular momentum vector L can have with the z-axis is(b) What is the corresponding expression for (?L)max the...
-
For ten paired samples, the data are as listed here. For d = the difference between paired observations (x1 x2), use the 0.10 level of significance in testing H0: md = 0 versus H1: md 0. Sample: 1 2...
-
A local symphony association offers memberships as follows: Continuing membership, per year $ 15 Patron lifetime membership 375 The patron membership has been based on the symphony association's...
-
1. What are the procedures of enlistment in context with the Value Added Tax and Supplementary DutyAct, 2012? *
-
In this exercise, we discuss deleting items from binary search trees. The deletion algorithm is not as straightforward as the insertion algorithm. Three cases are encountered when deleting an itemthe...
-
Modify Figs. 21.15 and 21.16 so the Tree class provides a method getDepth that determines how many levels are in the tree. Test the method in an application that inserts 20 random integers into a...
-
Explain why the sum of prices of the call-on-a-call and call-on-a-put is equal to the price of the call with expiration T 2 . Show that the price of a European call-on-a-put is given by 3 Option...
-
Problem 14-23 (Static) Comprehensive Problem [LO14-1, LO14-2, LO14-3, LO14-5, LO14-6] Lou Barlow, a divisional manager for Sage Company, has an opportunity to manufacture and sell one of two new...
-
The farm business owes income taxes at the end of the year because income taxes are not paid until after the end of the year is true or false
-
So there were some changes in "Kieso intermediate accounting 15th edition"chapter 18 (revenue recognition). Is the test bank the same as before? If not when and what year did the update occur? Please...
-
Explain how competencies are developed through supervised practice. Focus your response on skills application utilizing the psychodynamic or cognitive behavioral approach to supervision. Include...
-
Which statement is correct? Select the best answer. Answer Keypad Keyboard Shortcuts Physical capital deepening has a larger marginal effect in low-income countries than high-income countries. Human...
-
Why are convenience stores able to charge so much more for some goods, like milk, than grocery stores?
-
State whether each statement is true or false. If false, give a reason. {purple, green, yellow} = {green, pink, yellow}
-
What is the maximum number of callers in each cell in AMPS?
-
What are the functions of a mobile switching center?
-
What is the maximum number of simultaneous calls in each cell in an IS-136 (D-AMPS) system, assuming no analog control channels?
-
Compute the value of ordinary bonds under the following circumstances assuming that the coupon rate is 0.06:(either the correct formula(s) or the correct key strokes must be shown here to receive...
-
A tax-exempt municipal bond has a yield to maturity of 3.92%. An investor, who has a marginal tax rate of 40.00%, would prefer and an otherwise identical taxable corporate bond if it had a yield to...
-
Please note, kindly no handwriting. Q. Suppose a 3 year bond with a 6% coupon rate that was purchased for $760 and had a promised yield of 8%. Suppose that interest rates increased and the price of...
Study smarter with the SolutionInn App