Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 19, Problem 2PC
Program Plan Intro

Linked List Sorting and Reversing

Program Plan:

For filename: Modified “LinkedList1.java”

  • In this class, additionally need to add two methods “sort()” and “reverse()”.
  • Define the method “sort()” which is used to sort the elements in the list by alphabetical order.
    • Create two variables “current_node” and “index_value” using “Node” class and this variable set to “null”.
    • Declare a string variable “temp_value”.
    • Check whether list is empty. If the list is empty, then returns nothing.
    • Otherwise,
      • Check condition using “for” loop. That is the current node will point to first.
        • Check the condition using “for” loop that is for index value will point to next node of current node.
          • If data in the “current_node” is greater than the data in “index_value”, then swap the data of “current_node” and “index_index”.
  • Define the method “reverse()” which is used to reverse the elements in the list.
    • Create a list “rev_list” and set it to “null”.
    • Performs “while” loop. This loop will perform up to the first node equals to “null”.
      • Set a reference “ref” to first node.
      • Set “first” to next first node.
      • Set next node to “rev_list”.
      • Set the “rev_list” to reference “ref”.
    • Make the list “rev_list” as the new list.

For filename: “LinkedList1Demo.java”:

  • Import required package.
  • Define “LinkedList1Demo” class.
    • Create an instance for “LinkedList1” class, text area, command text field, and result text field.
    • Define constructor for “LinkedList1Demo()” class.
      • Create instance.
      • Create a panel for “Command Result” text field.
      • Create a label for command result.
      • Place the text area in center of the frame.
      • Create a panel and label for “Command” text field.
      • Set up the frame.
    • Define private class “commandTextListener” that reply to the user entered command.
      • Define “actionPerformed” class.
        • Read the command from the command text field.
        • Create an object for “Scanner” class.
        • Read command from user.
        • Check the user entered command using “switch” statement.
          • If the user entered command is “sort”, then,
            • Sort the linked list “new_list” by calling the method “sort()”.
            • Displays lists in alphabetical order.
            • Assign a status message for the “sort” command was finished.
            • Displays status message to “Command Result” text field.
          • If the user entered command is “reverse”, then
            • Reverse the linked list “new_list” by calling the method “reverse()”.
            • Prints the reversed list.
            • Assign a status message for the “reverse” command was finished.
            • Displays status message to “Command Result” text field.
          • If the user entered command is “add”, then
            • Check condition. If it is true, then
              • Read index and string element from user.
              • Add that index and its element to linked list “new_list” by calling the method “add”.
            • Otherwise,
              • Read string element from user.
              • Add string element to the list “new_list” by calling the method “add”.
            • Display the added string elements.
          • If the user entered command is “remove”, then
            • Check condition. If it is true, then
              • Read an index from user.
              • Remove an element at given index.
              • Display the removed element at given index to “Command Result” text field.
            • Otherwise,
              • Read an element from the list.
              • Obtain the Boolean result.
              • Convert the Boolean into string.
              • Display the Boolean result to “Command Result” text field.
            • Displays string elements in the list
          • If the user entered command is “isEmpty”, then
            • Check the given list is empty or not by calling the method “isEmpty” and store the result in “result_text3”.
            • Display the result to “Command Result” text field.
          • If the user entered command is “size”, then
            • Compute the size of list “new_list” by calling the method “size()” and store the result in “result_text4”.
            • Display the result to “Command Result” text field.
    • Define main function.
      • The instance of “LinkedList1Demo” class displays its window.

Blurred answer
Students have asked these similar questions
What is the biggest advantage of linked list over array? Group of answer choices Unlike array, linked list can dynamically grow and shrink With linked list, it is faster to access a specific element than with array Linked list is easier to implement than array Unlike array, linked list can only hold a fixed number of elements
In python. Write a LinkedList class that has recursive implementations of the add and remove methods. It should also have recursive implementations of the contains, insert, and reverse methods. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds). It should have a recursive method named to_plain_list that takes no parameters (unless they have default arguments) and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list. The head data member of the LinkedList class must be private and have a get method defined (named get_head). It should return the first Node in the list (not the value inside it). As in the iterative LinkedList in the exploration, the data members of the Node class don't have to be private. The reason for that is because Node is a trivial class…
Implements clone which duplicates a list. Pay attention, because if there are sublists, they must be duplicated as well. Understand the implementation of the following function, which recursively displays the ids of each node in a list  Develop your solution as follows: First copy the nodes of the current list (self) Create a new list with the copied nodes Loop through the nodes of the new list checking the value field If this field is also a list (use isinstance as in the show_ids function) then it calls clone on that list and substitutes the value. Complete the code: def L4(*args,**kwargs):         class L4_class(L):          def clone(self):              def clone_node(node):                 return <... YOUR CODE HERE ...>              r = <... YOUR CODE HERE...>             return r                     return L4_class(*args,**kwargs)
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education