Modify the List class of Fig. 21.3 to include method printListBackward that recursively outputs the items in
Question:
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 integers and prints the list in reverse order.
Fig. 21.3
Transcribed Image Text:
I // Fig. 21.3: List.java 2 // ListNode and List class declarations. package com.deitel.datastructures; 3 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.NoSuchElementException; // 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) { } // 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); } name 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: 100% (1 review)
To modify the List class to include a method printListBackward that recursively outputs the items in ...View the full answer
Answered By
Ashington Waweru
I am a lecturer, research writer and also a qualified financial analyst and accountant. I am qualified and articulate in many disciplines including English, Accounting, Finance, Quantitative spreadsheet analysis, Economics, and Statistics. I am an expert with sixteen years of experience in online industry-related work. I have a master's in business administration and a bachelor’s degree in education, accounting, and economics options.
I am a writer and proofreading expert with sixteen years of experience in online writing, proofreading, and text editing. I have vast knowledge and experience in writing techniques and styles such as APA, ASA, MLA, Chicago, Turabian, IEEE, and many others.
I am also an online blogger and research writer with sixteen years of writing and proofreading articles and reports. I have written many scripts and articles for blogs, and I also specialize in search engine
I have sixteen years of experience in Excel data entry, Excel data analysis, R-studio quantitative analysis, SPSS quantitative analysis, research writing, and proofreading articles and reports. I will deliver the highest quality online and offline Excel, R, SPSS, and other spreadsheet solutions within your operational deadlines. I have also compiled many original Excel quantitative and text spreadsheets which solve client’s problems in my research writing career.
I have extensive enterprise resource planning accounting, financial modeling, financial reporting, and company analysis: customer relationship management, enterprise resource planning, financial accounting projects, and corporate finance.
I am articulate in psychology, engineering, nursing, counseling, project management, accounting, finance, quantitative spreadsheet analysis, statistical and economic analysis, among many other industry fields and academic disciplines. I work to solve problems and provide accurate and credible solutions and research reports in all industries in the global economy.
I have taught and conducted masters and Ph.D. thesis research for specialists in Quantitative finance, Financial Accounting, Actuarial science, Macroeconomics, Microeconomics, Risk Management, Managerial Economics, Engineering Economics, Financial economics, Taxation and many other disciplines including water engineering, psychology, e-commerce, mechanical engineering, leadership and many others.
I have developed many courses on online websites like Teachable and Thinkific. I also developed an accounting reporting automation software project for Utafiti sacco located at ILRI Uthiru Kenya when I was working there in year 2001.
I am a mature, self-motivated worker who delivers high-quality, on-time reports which solve client’s problems accurately.
I have written many academic and professional industry research papers and tutored many clients from college to university undergraduate, master's and Ph.D. students, and corporate professionals. I anticipate your hiring me.
I know I will deliver the highest quality work you will find anywhere to award me your project work. Please note that I am looking for a long-term work relationship with you. I look forward to you delivering the best service to you.
3.00+
2+ 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 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...
-
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 its found;...
-
Write a program that inserts 25 random integers from 0 to 100 in order into a linked-list object. For this exercise, youll need to modify the List class (Fig. 21.3) to maintain an ordered list. Name...
-
Which of the following quadratic equations is in standard form? (a) x 2 7x = 5 (b) 9 = x (c) (x + 5)(x - 4) = 0 (d) 0 = 5x - 6x - 1
-
The wave function for a hydrogen atom in the 2s state is(a) Verify that this function is normalized.(b) In the Bohr model, the distance between the electron and the nucleus in the n = 2 state is...
-
1. Identify the supply chain management problems faced by Scotts Miracle-Gro. What was the business impact of not being able to manage the companys supply chain well? 2. What management,...
-
On April 4, 2014, Athanasios Valsamis lost his appeal to get his money back from a friend to whom he had loaned \($700,000.\) As you will read, this case underscores the consequence of failing to...
-
On January 13, 2013, Precision Oil Company purchased a drilling truck for $90,000. Precision expects the truck to last five years or 200,000 miles, with an estimated residual value of $15,000 at the...
-
Karen wishes to have $19,526 cash for a new car 5 years from now. How much should be placed in an account now, if the account pays 5.5% annual interest rate, compounded weekly?
-
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...
-
What is meant by automatic monetary adjustments? How do they help to adjust balanceofpayments disequilibria?
-
5. Automobile Insurance (PAP): Both Automobiles Type Liability Medical Payments Physical Damage, Own Auto Uninsured Motorist Collision Deductible Comprehensive Deductible Premium (per year) Personal...
-
Define the concept of an Operational Amplifier (Op-Amp) and elaborate on the various types, elucidating their distinct characteristics, functionalities, and applications within the realm of...
-
Chief Candace Miller of Jefferson City police department has submitted a proposal to the city's budget director requesting the purchase of ten new police cars. She estimates the department will save...
-
Capital Ltd holds 80% of the voting shares of Roget Ltd and the remaining 20% of the voting shares of Roget Ltd are held by Hindmarsh Ltd. Capital Ltd and Hindmarsh Ltd have a contract with each...
-
Create a Python Program that displays a menu of 3 items - Items 1 and 2 run the programs below and Item 3 exits the menu and program completely. The menu should run until the user chooses to Exit. 1....
-
Baron Company, which operates a chain of 30 electronics supply stores, has just completed its fourth year of operations. The direct write-off method of recording bad debt expense has been used during...
-
All of the following assets can be depreciated, except: (a) A bulldozer (b) A copper mine (c) A surgical robot (d) A conveyor belt
-
Which is better, a low reuse factor or a high reuse factor? Explain your answer.
-
What is the maximum number of simultaneous calls in each cell in a GSM, assuming no analog control channels?
-
What is the difference between a hard handoff and a soft handoff?
-
SAS Computers owns a patent on a computer processor. The processor was developed and capitalized at a cost of 2,030,000 in the beginning of 2015. It was expected to be economically useful for 7 years...
-
The following graph illustrates the weekly demand curve for motorized scooters in Moline. Use the green rectangle (triangle symbols) to compute total revenue at various prices along the demand curve....
-
Juarez Builders incurred $285,000 of labor costs for construction jobs completed during the month of August, of which $212,000 was direct and $73,000 was indirect supervisory costs.What will be the...
Study smarter with the SolutionInn App