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...
-
If the row main effect is statistically significant and there are only two rows, it is / is not necessary to do a post-hoc test for the row effect.
-
\(\sqrt{112}\) Simplify the square root by expressing it in lowest terms.
-
Here are comparative balance sheets for Velo Company. Additional information: 1. Net income for 2014 was $93,000. 2. Cash dividends of $35,000 were declared and paid. 3. Bonds payable amounting to...
-
On January 1, a company issued 3%, 20-year bonds with a face amount of $80 million for $ 69,033,776 to yield 4%. Interest is paid semiannually. What is the journal entry on June 30 when the interest...
-
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...
-
In Problem write the augmented matrix of the system of linear equations. 4x 1 + x 2 = 8 3x 1 5x 2 = 6 x 1 + 9x 2 = 4
-
How can a company assess its internal resources and capabilities?
-
Is performance management system the same as performance management cycle?
-
Josephine and Sunhee are playing shuffleboard on the deck of a cruise ship. The ship is sailing du e north at a speed of 3.0 m/s. If Sunhee slides her puck along the deck, from the front toward the...
-
Capacitors in Circuits 3. (3) Find the equivalent capacitance of the capacitor shown in the figure on the right. V = 4.0 V, C1 190 nF, C2-150 nF, C3=290 nF and C4=420 nF. V C3 C
-
How has organizational culture been a factor in the success of a business?
-
How could you monitor compliance with a code of conduct in a corporation?
-
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...
-
FIGURE 7.1 THE DATABASE MODEL CUSTOMER INVOICE PK CUS CODE generates PK INV_NUMBER contains LINE PK,FK1 INV NUMBER PK LINE NUMBER CUS_LNAME FK1 CUS_CODE CUS_FNAME CUS_INITIAL CUS AREACODE CUS_PHONE...
-
diagnosis code to U In Review Exercises: Chapter 4, assign the correc (Page 134 of Basic ICD-10-CM 2020 Text book by Schraffenberger).
-
Instructions: Draw the following section detail at 1"=1'-0". Use break lines to fit it onto a single sheet. Place the exterior of the assembly on the left side of the detail. Show the top 12" of the...
Study smarter with the SolutionInn App