Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
Question
Book Icon
Chapter 12, Problem 9PP
Program Plan Intro

Doubly linked list

Program Plan:

“DoublyLinkedList.java”:

  • Define “DoublyLinkedList” class.
    • Declare required variables.
    • Default constructor for “DoublyLinkedList” class.
    • Define the method “length”.
      • Declare required variable.
      • Check condition using “while” loop.
      • Return the result of count value
    • Define “addANodeToStart” method.
      • Declare variable by using “ListNode” class.
      • Assign value to head.
      • If tail is null, then assign tail to head.
      • If old head is not null, then set the values to link value at head and previous value at old head node.
    • Define “getDataAtCurrent” method.
      • If current is not null, then returns the data at current.
      • Otherwise, display the given error message.
      • Finally returns the null value.
    • Define “resetIteration” method.
      • This method is used to set current to head.
    • Define “moreToIterate” method.
      • This method is used to return the result of “current != null”.
    • Define “goToNext” method.
      • If current is not null, then set current value to link value at current.
      • If head is not null, then display given message.
    • Define “goToPrevious” method.
      • If current is not null, then set current to previous value at current.
      • If head is not null, then display given error statement.
    • Define “insertNodeAfterCurrent” method.
      • Declare required variables.
      • If tail is equal to current, then set tail to new node.
      • If current is not null, then set given values to given variables.
        • If the link value at new node is not null, then set the value to new node.
          • If the value of head is not null, then display given error message.
    • Define “deleteCurrentNode” method.
      • Declare variable using “ListNode” class.
      • If tail is equal to current and current is not null, then set tail to previous value at current node.
      • If current and previous value at current node is not null, then set values for given variables.
      • If current is not null and previous value at current node is null, then set values for head node.
      • Otherwise, that is if current is equal to null, then display error message.
    • Define “deleteHeadNode” method.
      • If tail is equal to head, then set tail to null.
      • If head is not null, then assign values for head.
      • Otherwise, display error message.
    • Define “findInList” method.
      • Declare required variables.
      • Check condition using “while” loop.
        • Get the data position by calling the method “getData” and then store it to a variable “data_position”.
        • If the position of data is equals to target node, then set “current” to “location” and then return true.
        • Then get the location by calling the method “getLink”.
          • Finally assign the “current” to “null” and return “false”.
    • Define “onList” method.
      • This method returns the result of “Find(target_node) != null”.
    • Define “Find” method.
      • Declare required variables.
      • Check condition using “while” loop.
    • Define “showList” method.
      • Declare required variables.
      • Display the data at each location using “while” loop.
    • Define “displayListValues” method.
      • This method is used to return the values in head, current, tail and number of items in list.
    • Define node class “ListNode”.
      • Declare required variables
      • Define constructor for “ListNode” class.
      • Define parameterized constructor for “ListNode” class.
      • Define set and get method for data and link.

“DoublyLinkedListTest.java”:

  • Define “DoublyLinkedListTest” class.
    • Define main function.
    • Assign result to “false”.
    • Create an object for “DoublyLinkedList” class.
    • Display the values in list by calling the method “displayListValues”.
    • Add a name “John” to “nameList” by calling the method “addANodeToStart”.
    • After adding, display the values in list by calling the method “displayListValues”.
    • Reset the iteration for given list by calling the method “resetIteration”.
    • Add a name “Merry” to “nameList” by calling the method “addANodeToStart”.
    • After adding, display the values in list by calling the method “displayListValues”.
    • Go to next node by calling the method “goToNext”.
    • Add a name “Rose” to “nameList” by calling the method “addANodeToStart”.
    • After adding, display the values in list by calling the method “displayListValues”.
    • Display the list by calling “showList” method.
    • Reset the iteration for given list by calling the method “resetIteration”.
    • Go to next node by calling the method “goToNext”.
    • After adding, display the values in list by calling the method “displayListValues”.
    • Add a name “Jansi” to “nameList” by calling the method “addANodeToStart”.
    • After adding, display the values in list by calling the method “displayListValues”.
    • Display the list by calling “showList” method.
    • Go to next node by calling the method “goToNext”.
    • Delete first node by calling “deleteHeadNode” method.
    • After deleting, display the list by calling “showList” method.
    • Go to next node by calling the method “goToNext”.
    • Add a name “Rosie” to “nameList” by calling the method “addANodeToStart”.
    • Go to next node by calling the method “goToNext”.
    • Add a name “Joshph” to “nameList” by calling the method “addANodeToStart”.
    • After adding, display the values in list by calling the method “displayListValues”.
    • Display the list by calling “showList” method.
    • Go to previous node by calling the method “goToPrevious”.
    • Display the values in list by calling the method “displayListValues”.
    • Display the list by calling “showList” method.
    • Go to previous node by calling the method “goToPrevious”.
    • Get the current node by calling the method “getDataAtCurrent”.
    • Delete node at current by calling the method “deleteCurrentNode”.
    • After deleting, display the values in list by calling the method “displayListValues” and method “showList”.
    • Add a name “Jack” to beginning of “nameList” by calling the method “addANodeToStart”.
    • After deleting, display the values in list by calling the method “displayListValues” and method “showList”.
    • Iterate to the end of list using “while” loop. After iterating, display the values in list by calling the method “displayListValues”.
    • Iterate the list in reverse by calling “resetIterationReverse” method.
    • After reverse iteration, display the values in list by calling the method “displayListValues”.
    • Delete the current node by calling “deleteCurrentNode” method.
    • After deleting current node, display the values in list by calling the method “displayListValues” and method “showList”.
    • Check if “Rose” is found in list or not by calling “findInList” method. If the given name found, then display the message “Found”. Otherwise, display “Not found”.
    • After performing check condition, display the values in list by calling the method “displayListValues” and method “showList”.
    • Check if “Josph” is found in list or not by calling “findInList” method. If the given name found, then display the message “Found”. Otherwise, display “Not found”.
    • After performing check condition, display the values in list by calling the method “displayListValues” and method “showList”.

Blurred answer
Students have asked these similar questions
You may find a doubly-linked list implementation below. Our first class is Node which we can make a new node with a given element. Its constructor also includes previous node reference prev and next node reference next. We have another class called DoublyLinkedList which has start_node attribute in its constructor as well as the methods such as: 1. insert_to_empty_list() 2. insert_to_end() 3. insert_at_index() Hints: Make a node object for the new element. Check if the index >= 0.If index is 0, make new node as head; else, make a temp node and iterate to the node previous to the index.If the previous node is not null, adjust the prev and next references. Print a message when the previous node is null. 4. delete_at_start() 5. delete_at_end() . 6. display() the task is to implement these 3 methods: insert_at_index(), delete_at_end(), display(). hint on how to start thee code # Initialize the Node class Node:             def __init__(self, data):                      self.item = data…
Trying this again since part of my question keeps disappearing  Given main() in the ShoppingList class, define an insertAtEnd() method in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node. Ex. if the input is: 4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list. The output is: Kale Lettuce Carrots Peanuts public class ItemNode {   private String item;   private ItemNode nextNodeRef; // Reference to the next node                                            public ItemNode() {      item = "";      nextNodeRef = null;   }    // Constructor                                                                                        public ItemNode(String itemInit) {      this.item = itemInit;      this.nextNodeRef = null;   }    // Constructor…
Write a method for the Linked Based List class which returns the largest item in the list. If the list is empty return null. Assume that class T is Comparable.DO NOT USE ANY OTHER METHODS OF THE LINKED BASED LIST CLASS.      What is the worst case and best case complexity of the code in the previous problem? Explain your answer.

Chapter 12 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 12.1 - Prob. 12STQCh. 12.2 - Prob. 13STQCh. 12.2 - Prob. 14STQCh. 12.2 - Prob. 15STQCh. 12.2 - Prob. 16STQCh. 12.3 - Prob. 17STQCh. 12.3 - Prob. 18STQCh. 12.3 - Prob. 19STQCh. 12.3 - Write a definition of a method isEmpty for the...Ch. 12.3 - Prob. 21STQCh. 12.3 - Prob. 22STQCh. 12.3 - Prob. 23STQCh. 12.3 - Prob. 24STQCh. 12.3 - Redefine the method getDataAtCurrent in...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.4 - Revise the definition of the class ListNode in...Ch. 12.4 - Prob. 30STQCh. 12.5 - What is the purpose of the FXML file?Ch. 12.5 - Prob. 32STQCh. 12 - Repeat Exercise 2 in Chapter 7, but use an...Ch. 12 - Prob. 2ECh. 12 - Prob. 3ECh. 12 - Repeat Exercises 6 and 7 in Chapter 7, but use an...Ch. 12 - Write a static method removeDuplicates...Ch. 12 - Write a static method...Ch. 12 - Write a program that will read sentences from a...Ch. 12 - Repeat Exercise 12 in Chapter 7, but use an...Ch. 12 - Write a program that will read a text file that...Ch. 12 - Revise the class StringLinkedList in Listing 12.5...Ch. 12 - Prob. 12ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 14ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 17ECh. 12 - Revise the method selectionSort within the class...Ch. 12 - Repeat the previous practice program, but instead...Ch. 12 - Repeat Practice Program 1, but instead write a...Ch. 12 - Write a program that allows the user to enter an...Ch. 12 - Write a program that uses a HashMap to compute a...Ch. 12 - Write a program that creates Pet objects from data...Ch. 12 - Repeat the previous programming project, but sort...Ch. 12 - Repeat the previous programming project, but read...Ch. 12 - Prob. 9PPCh. 12 - Prob. 10PPCh. 12 - Prob. 11PPCh. 12 - Prob. 12PPCh. 12 - Prob. 13PPCh. 12 - Prob. 14PPCh. 12 - Prob. 15PP
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning