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...
-
A random sample of 10 observations has been drawn from a normal population, and the sample variance is found to be s2 = 19.5. Determine the 98% confidence interval for 2.
-
What is the difference between an agency fund and a trust fund? AppendixLO1
-
The average expenditure on Valentine's Day was expected to be $100.89 (USA Today, February 13, 2006). Do male and female consumers differ in the amounts they spend? The average expenditure in a...
-
Huron Company produces a commercial cleaning compound known as Zoom. The direct materials and direct labor standards for one unit of Zoom are given below: Standard Quantity or Hours Standard Price or...
-
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 0.001 in. BCC iron foil is used to separate a high hydrogen content gas from a low hydrogen gas at 650 C. 5 108 H atoms/cm3 are in equilibrium on one side of the foil, and 2 103 H atoms/cm3 are...
-
Use z scores to compare the given values. Based on sample data, newborn males have weights with a mean of 3240.3 g and a standard deviation of 675.8 g. Newborn females have weights with a mean of...
-
Compare the given pulse rates of the females and males using boxplots. Female 80 94 Male 84 74 50
-
In Exercises 29-32, compute the mean of the data summarized in the frequency distribution. Also, compare the computed means to the actual means obtained by using the original list of data values,...
-
Floyd's Bumpers has distribution centers in Lafayette, Indiana; Charlotte, North Carolina; Los Angeles, California; Dallas, Texas; and Pittsburgh, Pennsylvania. Each distribution center carries all...
-
In this assignment you are asked to open the Excel Spreadsheet for YP Enterprises (March 2019) and complete the section entitled Ratios 2019 (highlighted in yellow). This will require you to...
-
Suppose a hugely successful Web company has used freeconomics, expanded its scale of operations, and spread its long-run costs over larger and larger audiences. After years of profits, the company's...
-
The ultimate goal of Google, Bing, and other consumer search engines is to provide users with search listings that contain useful information on the topic of their search. What recommendations would...
-
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...
-
question 1- You borrow a simple loan of SR 500,000, interest rate is 20%, it matures in one year. what's the yied to maturity? question 2- calculate_i for One-Year Discount Bond with price(p) =...
-
Taste of Muscat is a reputed chain of restaurants operating in Oman. Assume You are working as a management accountant for this restaurant chain which is specialized in all types of Arabic food. Your...
-
Industry Current Year Minus 1 Current Year Minus 2 Company: Air Products and Chemicals, Inc. (APD) Stock Price: 306.72 USD Shares Outstanding: 220.89 M Financial Ratios Most Current Year Current...
Study smarter with the SolutionInn App