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 11PP
Program Plan Intro

“LinkedQueue” class

Program Plan:

Filename: “LinkedQueue.java”

  • Define “LinkedQueue” class.
    • Declare variable for reference of first node and number of items.
    • Default constructor for “LinkedQueue” class.
      • Set front to “null” and count to “0”.
    • Define the method “addToQueue()” with argument of “item”.
      • If “front” is not equal to “null”, then
        • Create a new node using “QueueNode” class.
        • Create a reference to front.
        • Performs “while” loop. This loop will repeat to obtain last node.
          • Compute the value of next node by calling “getNextNode()” method.
        • Set the new node at last of the queue by calling “setNextNode()” method.
            • If the queue is empty, then
              • Assign the next node to “null” by using “QueueNode” class.
              • Increment the number of items.
    • Define the method “removeFromQueue()”.
      • If the queue is not empty, then get the element at front node by calling “getQueueElement()” method and eliminate the front node.
        • Finally returns the resultant element.
            • If the queue is empty, then returns null.
    • Define the method “isEmpty()”.
      • This method returns “true” if the value of “count” is equal to “0”. Otherwise, returns “false”.
    • Define the method “displayQueueValues()”.
      • Create a reference to front.
      • Performs “while” loop. This loop will repeat to display all elements in queue.
        • Display the element of the queue.
        • Go to the next node by using “getNextNode()” method.

Filename: “QueueNode.java”

  • Define “QueueNode” class.
    • Declare required variables for queue elements and next node value.
    • Create default constructor for “QueueNode” class.
      • Assign “null” values for queue elements and next node.
    • Create the parameterized constructor for “QueueNode” class.
    • Define the method “setNextNode()” which is used to set value for next node.
    • Define the method “getNextNode()” which is used to returns the next node value.
    • Define the method “setQueueElement()” which is used to assign value for queue elements.
    • Define the method “getQueueElement()” which is used to returns queue elements.

Filename: “LinkedQueueTest.java”

  • Define “LinkedQueueTest” class.
    • Define main function.
      • Create an object “nameQueue” for “LinkedQueue” class.
      • Add names to object “nameQueue” by using “addToQueue()” method.
      • Call the method “displayQueueValues()” to display the elements in queue.
      • Remove an element from the queue by calling the method “removeFromQueue()”.
      • Display the result of whether the queue is empty or not by calling the method “isEmpty()”.
      • Display the elements in queue after removing an element from queue by calling the method “displayQueueValues()”.
      • Remove the elements from the queue by calling the method “removeFromQueue()”.
      • Display the result of whether the queue is empty or not by calling the method “isEmpty()”.

Blurred answer
Students have asked these similar questions
Python For this assignment, you will write a program to allow the user to store the names and phone numbers of their contacts. Create the following classes: Contact: two attributes. One for the contact's name and the contact's phone number. Node: two attributes: _data and _next. For use in a linked list. ContactList: One attribute: _head, a reference to the head of an internal linked list of Node objects. Note: self._head is an attribute of the class, so any method which updates the internal linked list can simply update the list self._head references. There is no need to return anything like we did in methods such as add_to_end in the lecture. Note also that for the same reason, we do not have to pass a reference to the head of the list to any method, because each method should already have access to the self._head attribute. ContactList should also have the following methods: add(name, new_number). Creates a new Node with a Contact object as its _data. The new Node is then added…
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…
Programming in Java. What would the difference be in the node classes for a singly linked list, doubly linked list, and a circular linked list? I attached the node classes I have for single and double, but I feel like I do not change enough? Also, I use identical classes for singular and circular node which does not feel right. Any help would be appreciated.

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