Modify the SortedList class from Exercise 21.7 to include a merge method that can merge the SortedList
Question:
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 test method merge.
Exercise 21.7
Write a program that inserts 25 random integers from 0 to 100 in order into a linked-list object. For this exercise, you’ll need to modify the List class (Fig. 21.3) to maintain an ordered list. Name the new version of the class SortedList.
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 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 } 29 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 30 // class List definition 31 public class List { 32 33 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 106 107 108 109 110 112 113 114] 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 115 116 117 118 119 120 121 122 123 124 124 125 126 127 128 129 // constructor creates a ListNode that refers to object ListNode (E object) {this(object, null); } // constructor creates ListNode that refers to the specified // object and to the next ListNode ListNode (E object, ListNode node) { object; data 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 listName; firstNode = lastNode= null; } // 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); } } } 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); } // remove first node from List public E removeFrom Front () throws No SuchElement Exception { 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; 130 131 } 132 } } 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 removed Item = 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; } // loop while current node does not refer to lastNode while (current.nextNode != lastNode) { current = current.nextNode; } } return removedItem; // return removed node data. } // determine whether list is empty; returns true if so public boolean isEmpty() {return firstNode == null; } lastNode = current; // current is new lastNode current.nextNode = null; // 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.printIn();
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 100% (2 reviews)
To address the given task I will present you with a stepbystep guide on how to modify the List class to create a SortedList class that maintains an or...View the full answer
Answered By
Vincent Omondi
I am an extremely self-motivated person who firmly believes in his abilities. With high sensitivity to task and operating parameters, deadlines and keen on instructions, I deliver the best quality work for my clients. I handle tasks ranging from assignments to projects.
4.90+
109+ Reviews
314+ 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
-
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...
-
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data...
-
You are required to write a Python program that will manage character (heroes and villain) information. Character (hero and villain) information will be stored in a text file that will be read in...
-
In Problems 5978, solve each equation in the complex number system. x 2 + 25 = 0
-
Estimate the energy of the highest-I state for (a) The L shell of Be+ and (b) The N shell of Ca+.
-
Choose a Country That you would like to learn about and explore its macro-economy. Prepare a policy brochure to provide to everyone at the world economic forum weforum.org suppose you have been...
-
In a dynamic vibration absorber having \(\frac{\omega_{2}}{\omega_{1}}=1\) and \(\mu=\frac{m_{2}}{m_{1}}=\frac{1}{2}\), determine the frequency range over which the value of the transmission ratio,...
-
Given the following business rules, construct an ER diagram so each rule is captured for the database. Presume each rule is to be treated individually. Construct an ER diagram for each rule. a. A...
-
Information on stocks E & F which have a correlation coefficient of 0.5 appear below: Stock E Stock F Expected Return 20% 30% Standard Deviation 0.4 0.5 Your uncle has savings of $20,000 and was...
-
Write an application that reads a list of integers and stores them in a linked list. Write a method that identifies and prints the position at which the list is no longer sorted in an increasing...
-
Write a program that concatenates two linked-list objects of characters. Class ListConcatenate should include a static method concatenate that takes references to both list objects as arguments and...
-
Jonathan Walker is probably failing which of the Rest Stages? a. Sensitivity b. Judgment c. Motivation d. Character
-
Rachel is finding her job rayther tedious and would like to go go back to school full-time for a year or 18 months to take the courses required to become certified as an Human Resource (HR)...
-
Centerville Bikes and Stuff (CBS) sells motorcycles and accessories. The number of helmets sold by CBS per week for the past six weeks follows. Week 1 2 3 4 5 6 Value 18 11 14 10 17 12 (a) Construct...
-
Consider the following abbreviated financial statements for Cabo Wabo, incorporated: CABO WABO, INCORPORATED Partial Balance Sheets as of December 31, 2021 and 2022 2022 Liabilities and Owners Equity...
-
Saxum Vineyard, in Paso Robles, CA, is one of the more than 8,000 wineries in the United States. While Saxum produces a number of different kinds of wine, they focus their production on Syrah (also...
-
Construct the network diagram for the project. Also, calculate the EST and LST of the events and mark on the diagram.
-
Use the rules for multiplication of measurements to multiply each set of measurements. 1. (125 m)(39 m) 2. (470 ft)(1200 ft) 3. (1637 km)(857 km) 4. (9100 m)(600 m) 5. (18.70 m)(39.45 m) 6. (565...
-
Perform the operation by first converting the numerator and denominator to scientific notation. Write the answer in scientific notation. 7,200,00/0.000009
-
In Figure 17.5, do we have a loop problem if we change each hub in the LANs to a link-layer switch? Figure 17.5 Figure 17.5 Loop problem in a learning switch a. Station A sends a frame to station D...
-
How does a VLAN save a company time and money?
-
Find the spanning tree and the logical connection between the switches in Figure 17.13. Figure 17.13 Problem P17-6. S1 LAN 1 S2 2 LAN 2 LAN 3 S3 LAN 4 S4
-
Write an interactive C program using if - else statement, to input water unit charge and calculate the total water bill according to the given condition: For the first 50 units RM 0.40/unit For the...
-
Consider the code below: void foo (int *n) { *n = 5; } void bar (int n) { n = 11; } main () { int *p, x=2, y=3, SIZE=5, data [SIZE]=(1,2,3,4,7}; foo (&x); }
-
Consider the following recursive function: int func (int val) { if (val <0) { func(0)} if (val == 0) return val; //Line 1 //Line 2 //Line 3. //Line 4 else //Line 5 return (val + func (val 1)); }...
Study smarter with the SolutionInn App