Our linked-list class allowed insertions and deletions at only the front and the back of the linked
Question:
Our linked-list class allowed insertions and deletions at only the front and the back of the linked list. These capabilities were convenient for us when we used composition to produce a stack class and a queue class with minimal code simply by reusing the list class. Linked lists are normally more general than those we provided. Modify the linked-list class we developed in this chapter to handle insertions and deletions anywhere in the list. Create diagrams comparable to Figs. 21.5 (insertAtFront), 21.6 (insertAtBack), 21.7 (removeFromFront) and 21.8 (removeFromBack) that show how to insert a new node in the middle of a linked list and how to remove an existing node from the middle of a linked list.
Figs. 21.5
Figs. 21.6
Figs. 21.7
Figs. 21.8
I // Fig. 21.15: Tree.java 2 // TreeNode and Tree class declarations for a binary search tree. 3 package com.deitel.datastructures; 4 5 // class TreeNode definition 6 class TreeNode { 7 8 9 10 IL 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 } 42 44 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 // package access members TreeNode leftNode; 99 100 101 } E data; // node value TreeNode rightNode; // constructor initializes data and makes this a leaf node public TreeNode (E nodeData) { data nodeData; leftNode= rightNode = null; // node has no children } // locate insertion point and insert new node; ignore duplicate values public void insert(E insertValue) { // insert in left subtree } if (insertValue.compareTo (data) < 0) { // insert new TreeNode } } if (leftNode == nul1) { leftNode= new TreeNode (insertValue); } else { // continue traversing left subtree recursively leftNode.insert(insertValue); } // insert in right subtree else if (insert Value.compareTo (data) > 0) { // class Tree definition public class Tree { private TreeNode root; } // constructor initializes an empty Tree of integers public Tree() {root = null;} //insert new TreeNode. if (rightNode == nu11) { rightNode = new TreeNode (insertValue); } else { // continue traversing right subtree recursively rightNode.insert(insertValue); } // insert a new node in the binary search tree public void insertNode (E insertValue) { if (root == null) { root = new TreeNode (insertValue); // create root node } } } else { root.insert(insertValue); // call the insert method // begin preorder traversal public void preorderTraversal() {preorderHelper (root); } // recursive method to perform preorder traversal private void preorderHelper (TreeNode node) { if (node == null) { return; } System.out.printf("%s", node.data); // output node data preorderHelper (node.leftNode); // traverse left subtree preorderHelper(node.rightNode); // traverse right subtree } // begin inorder traversal public void inorder Traversal() {inorderHelper(root); } // recursive method to perform inorder traversal private void inorderHelper (TreeNode node) { if (node == nu11) { return; } inorderHelper (node.leftNode); // traverse left subtree System.out.printf( "%s ", node.data); // output node data inorderHelper (node.rightNode); // traverse right subtree } // begin postorder traversal public void postorderTraversal() [postorderHelper (root); } // recursive method to perform postorder traversal private void postorderHelper (TreeNode node) { if (node =null) { return; } postorderHelper (node.leftNode); // traverse left subtree postorderHelper (node.rightNode); // traverse right subtree System.out.printf("%s", node.data); // output node data
Step by Step Answer:
Heres the modified linkedlist class Insertion in the middle ...View the full answer
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Students also viewed these Computer science questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
The Crazy Eddie fraud may appear smaller and gentler than the massive billion-dollar frauds exposed in recent times, such as Bernie Madoffs Ponzi scheme, frauds in the subprime mortgage market, the...
-
The following additional information is available for the Dr. Ivan and Irene Incisor family from Chapters 1-5. Ivan's grandfather died and left a portfolio of municipal bonds. In 2012, they pay Ivan...
-
In Exercises 6780, begin by graphing the square root function, f(x) = x. Then use transformations of this graph to graph the given function. h(x)=x + 2-2
-
An atom in a 3d state emits a photon of wavelength 475.082 run when it decays to a 2s state. (a) What is the energy (in electron volts) of the photon emitted in this transition? (b) Use the selection...
-
Part A.: You are considering launching a strategic alliance with a competitor to join your separate skills to develop a new jointly owned technology. Both you and your partner have the option of...
-
Solve Problem 13.30 for a simple pendulum: \[\ddot{\theta}+\omega_{n}^{2} \sin \theta=0\] Data From Problem 13.30:- Identify the singularity and investigate the nature of solution near the...
-
The controller of Dugan Industries has collected the following monthly expense data for use in analyzing the cost behavior of maintenance costs. Instructions(a) Determine the fixed and variable cost...
-
An object is moving with an initial velocity of 4 0 m / s west. If it has a constant acceleration of 6 m / s ^ 2 due east, what is its velocity after 5 seconds? How long did it move at that time?
-
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...
-
Modify class Tree of Fig. 21.15 to include a recursive outputTree method to display a binary tree object. The method should output the tree row by row, with the top of the tree at the left of the...
-
(a) Draw the two linkage isomers of [Co (NH3)5SCN]2+ (b) Draw the two geometric isomers of [Co (NH3)5IC3]2+ (c) Two compounds with the formula Co(NH3)5ClBr can be prepared. Use structural formulas to...
-
Find the inverse for each function. Then graph the function and its inverse. If necessary, restrict the domain of f(x) so that the inverse is a function. 24-29. See Ch. 5 Answer Appendix. 24. f(x) =...
-
How do you find the following answers: Bottom Quartile - ROA Bottom Quartile Upper Limit - ROA Average Goodwill/Total Assets Average Goodwill/Total Assets - Bottom Quartile ROA Bottom 50%-ROA Bottom...
-
What is franked distribution in partnership? How is it calculated? 51 Statement of distribution PARTNER 1 NON-INDIVIDUAL Tax file number (TFN) 666 000 666 NAME (company, partnership, trust etc.)...
-
1) For the following circuit, a) Determine the frequency for which X, is equal to R b) Explain the change of total impedance with frequency, without resorting to a single calculation c) Determine the...
-
When possible, use the provided matrices to complete questions 1-9 as needed. Note that some of the requested calculations may not be well-defined, and if so, simply. indicate that in your response....
-
Instead of using a solid iron beam, structural engineers and contractors use I-beams to save materials and money. How many I-beams can be molded from the same amount of iron contained in the solid...
-
Beginning with a country that has a trade deficit, demonstrate graphically what will happen to a countrys potential output with globalization if that countrys costs of production fall. Explain your...
-
In an 802.11 network, three stations (A, B, and C) are contending to access the medium. The contention window for each station has 31 slots. Station A randomly picks up the first slot; station B...
-
What MAC protocol is used in the baseband layer of Bluetooth?
-
In an 802.11 network, there are three stations, A, B, and C. Station C is hidden from A, but can be seen (electronically) by B. Now assume that station A needs to send data to station B. Since C is...
-
Cash contribution of 4,000 to the Accounting Society (a charity) Purchase of art object at an Accounting Society Charitable event for $1,200 (FMV $800) Donation of 3-year-old clothing (basis 800; FMV...
-
Carrie enjoyed observing wildlife in natural habitats. She wanted to be able to hide at a distance but observe wildlife close up in a variety of circumstances. She visited five different shops and...
-
Round Company produces hula hoops. Round Company has the following sales projections for the upcoming year. First quarter budgeted hula hoop sales in units Second quarter budgeted hula hoop sales in...
Study smarter with the SolutionInn App