Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
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.
- If the queue is empty, then
-
- If “front” is not equal to “null”, then
- 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.
-
- Finally returns the resultant element.
- If the queue is not empty, then get the element at front node by calling “getQueueElement()” method and eliminate the front node.
- 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()”.
- Define main function.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Tour.java
Create a Tour data type that represents the sequence of points visited in a TSP tour. Represent the
tour as a circular linked list of nodes, one for each point in the tour. Each Node contains two
references: one to the associated Point and the other to the next Node in the tour. Each
constructor must take constant time. All instance methods must take time linear (or better) in the
number of points currently in the tour.
To represent a node, within Tour.java, define a nested class Node:
private class Node {
private Point p;
private Node next;
}
Your Tour data type must implement the following API. You must not add public methods to the
API; however, you may add private instance variables or methods (which are only accessible in the
class in which they are declared).
public class Tour
// Creates an empty tour.
public Tour()
// Creates the 4-point tour a→b→c→d→a (for debugging).
public Tour(Point a, Point b, Point c, Point d)
// Returns the number of points in this tour.
public…
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…
In c++ , write a program to create a structure of a node, create a class Linked List. Implement all operations of a linked list as member function of this class.
• create_node(int);
• insert_begin();
• insert_pos();
• insert_last();
• delete_pos();
• sort();
• search();
• update();
• reverse();
• display();
( Drop coding in words with screenshot of output as well )
Chapter 12 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Ch. 12.1 - Suppose aList is an object of the class...Ch. 12.1 - Prob. 2STQCh. 12.1 - Prob. 3STQCh. 12.1 - Prob. 4STQCh. 12.1 - Can you use the method add to insert an element at...Ch. 12.1 - Prob. 6STQCh. 12.1 - Prob. 7STQCh. 12.1 - If you create a list using the statement...Ch. 12.1 - Prob. 9STQCh. 12.1 - Prob. 11STQ
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
Similar questions
- Create a node class named LinkedNodes that uses up to 4 dynamic pointers to connect it to the other 8 nodes in the structure (a total of 9). The data in each node will be a unique integer number from 1 through 9. Include in your class definition these functions at a minimum: getVal(), setVal(), getNext(), setNext(), getPrev(), setPrev(), constructor(s) and destructor. Write a simple program to load these nodes into a linked structure. Do not use an array, the pointers will do that for you. You should be able to reach any node from any other node in the structure by traversing a maximum of 2 nodes from the current one. The output from your program should report the traversals between all nodes in the structure, starting with the node whose value is one. (See the example below) Node 1 2 3 4 5 6 7 8 9 -> Traverses to Node 1 Node 2 1->2 2->6->1 3->6->1 3->7->2 4->6->1 4->7->2 4->8->3 5->6->1 5->7->2 5->8->3 6->1->2 6->1->3 7->1->3 8->3 6->1 7->1 8->1 9->1 Node 3 1->3 2->3 7->2 8->2 9->2…arrow_forwardJava Given main() in the Inventory class, define an insertAtFront() method in the InventoryNode class that inserts items at the front of a linked list (after the dummy head node).arrow_forwardJava programarrow_forward
- Write java code for a member function insertSorted(int d) for a linked list. The function traverses the list until it finds the correct location, then it inserts a node in that location. You may use the function given the class.arrow_forwardGiven the following definition for a LinkedList: // LinkedList.h class LinkedList { public: LinkedList(); // TODO: Implement me void printEveryOther() const; private: struct Node { int data; Node* next; }; Node * head; }; // LinkedList.cpp #include "LinkedList.h" LinkedList::LinkedList() { head = nullptr; } Implement the function printEveryOther, which prints every other data value (i.e. those at the odd indices assuming 0-based indexing).arrow_forwardA double-ended queue or deque is a generalization of a stack and a queue that supports adding and removing items from either the front or the back of the data structure. This assignment has two parts: Part-1 Create a doubly linked list based DeQueDLL class that implements the DequeInterface. The class skeleton and interface are provided to you. Implement a String toString () method that creates and returns a string that correctly represents the current deque. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each queued element already provides its own reasonable toString method. Part-2 Create an application program that gives a user the following three options to choose from – insert, delete, and quit. If the user selects ‘insert’, the program should accept the integer input from the user and insert it into the deque in a sorted manner. If the user selects ‘delete’, the program should…arrow_forward
- Part 3: Building a Point of Sales (POS) linked list data structure. In a POS system, a transaction is based on items purchased by the customer. The following is an example of a customer transaction receipt, where the prices shown in the receipt are GST inclusive. Write a linked list classes (one class for Node and another class for List), which store the items in the transaction. Test the classes by printing the items in the linked list and show the total price of the transaction. The following listing is the sample output for your reference: ============================== BC Items Price ============================== 10 Pagoda Gnut 110g 3.49 11 Hup Seng Cream Cracker 4.19 12 Yit Poh 2n1 Kopi-o 7.28 13 Zoelife SN & Seed 5.24 14 Gatsby S/FO Wet&Hard 16.99 15 GB W/G U/Hold 150g 6.49 ============================== Total (GST Incl.) 43.68…arrow_forwardThe default constructor of the class queue that based on array set the front and rear to Select one: a. Front =0 and rear = maxsize b. Front = 0 and rear = maxsize-1 c. Front =0 and rear =0 d. Front=reararrow_forwardA readinglist is a doubly linked list in which each element of the list is a book. So, you must make sure that Books are linked with the previous prev and next element. A readinglist is unsorted by default or sorted (according to title) in different context. Please pay attention to the task description below. Refer to the relevance classes for more detail information. Implement the add_book_sorted method of the ReadingList class. Assume the readinglist is sorted by title, the add_book_sorted method takes an argument new_book (a book object), it adds the new_book to the readinglist such that the readinglist remain sorted by title. For example, if the readinglist contain the following 3 books: Title: Artificial Intelligence Applications Author: Cassie Ng Published Year: 2000 Title: Python 3 Author: Jack Chan Published Year: 2016 Title: Zoo Author: Cassie Chun Published Year: 2000 If we add another book (titled "Chinese History"; author "Qin Yuan"; and published year 1989) to the…arrow_forward
- C++ programming design a class to implement a sorted circular linked list. The usual operations on a circular list are:Initialize the list (to an empty state). Determine if the list is empty. Destroy the list. Print the list. Find the length of the list. Search the list for a given item. Insert an item in the list. Delete an item from the list. Copy the list. Write the definitions of the class circularLinkedList and its member functions (You may assume that the elements of the circular linked list are in ascending order). Also, write a program to test the operations of your circularLinkedList class. NOTE: The nodes for your list can be defined in either a struct or class. Each node shall store int values.arrow_forwardYou need to implement a class named "Queue" that simulates a basic queue data structure. The class should have the following methods: Enqueue(int value) - adds a new element to the end of the queue Dequeue() - removes the element from the front of the queue and returns it Peek() - returns the element from the front of the queue without removing it Count() - returns the number of elements in the queue The class should also have a property named "IsEmpty" that returns a boolean indicating whether the queue is empty or not. Constraints: The queue should be implemented using an array and the array should automatically resize when needed. All methods and properties should have a time complexity of O(1) You can write the program in C# and use the test cases to check if your implementation is correct. An example of how the class should be used: Queue myQueue = new Queue(); myQueue.Enqueue(1); myQueue.Enqueue(2); myQueue.Enqueue(3); Console.WriteLine(myQueue.Peek()); // 1…arrow_forwardAssignment 05 PART 1 Queues - Note Part 1 is a separate deliverable: Write a class with a main method that uses a priority queue to store a list of prioritized chores. This is all the information that is provided. The rest is up to you. You may decide how to store your chores and how to store your priorities. YOU MUST WRITE your own Queue class – do not use the Java Library, Queue Class.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning