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 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.
- If the link value at new node is not null, then set the value to new node.
- 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”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
You are required to complete the LinkedList class. This class is used as a linked list that has many methods to perform operations on the linked list. To create a linked list, create an object of this class and use the addFirst or addLast or add(must be completed) to add nodes to this linked list. Your job is to complete the empty methods. For every method you have to complete, you are provided with a header, Do not modify those headers(method name, return type or parameters). You have to complete the body of the method.
package chapter02;
public class LinkedList
{
protected LLNode list;
public LinkedList()
{
list = null;
}
public void addFirst(T info)
{
LLNode node = new LLNode(info);
node.setLink(list);
list = node;
}
public void addLast(T info)
{
LLNode curr = list;
LLNode newNode = new LLNode(info);
if(curr == null)
{
list = newNode;
}
else
{
while(curr.getLink() !=…
A 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…
Please answer the question in the screenshot. The language used is Java.
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
- Write a program that implement a FIFO queue integer numbers using a double linked list as its underlying physical data structure. Use LFIFOQuequeClass as its name and define a testing class to test it.. (Note, do not include methods in the double linkedlist that will not be used by your program)arrow_forwardplease follow instructions correctly. You are required to complete the LinkedList class. This class is used as a linked list that has many methods to perform operations on the linked list. To create a linked list, create an object of this class and use the addFirst or addLast or add(must be completed) to add nodes to this linked list. Your job is to complete the empty methods. For every method you have to complete, you are provided with a header, Do not modify those headers(method name, return type or parameters). You have to complete the body of the method. package chapter02; public class LinkedList { protected LLNode list; public LinkedList() { list = null; } public void addFirst(T info) { LLNode node = new LLNode(info); node.setLink(list); list = node; } public void addLast(T info) { LLNode curr = list; LLNode newNode = new LLNode(info); if(curr == null) { list = newNode; } else…arrow_forward4 O T O I D06 It is time for you to demonstrate your skills in a project of your own choice. You must DESIGN, ANALYSE AND CODE any method for the GENERIC MyLinkedList class that will manipulate the linked list. You can decide yourself what it should be following the specification below: 1. Purpose: The method must make logical sense - it should be of some purpose to somebody. You should describe in the text who will use the method for which purpose. 2. Clearly explain the problem. Then clearly explain how your method will solve it. 3. Test program: Test the method using a wranned class like Integer l Filters Add a caption. > Status (Contacts)arrow_forward
- 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.arrow_forwardProvide me complete and correct solution thanks 3arrow_forwardProgramming Exercise 8 asks you to redefine the class to implement the nodes of a linked list so that the instance variables are private. Therefore, the class linkedListType and its derived classes unorderedLinkedList and orderedLinkedList can no longer directly access the instance variables of the class nodeType. Rewrite the definitions of these classes so that these classes use the member functions of the class nodeType to access the info and link fields of a node. Also write programs to test various operations of the classes unorderedLinkedList and orderedLinkedList. template <class Type>class nodeType{public:const nodeType<Type>& operator=(const nodeType<Type>&);//Overload the assignment operator.void setInfo(const Type& elem);//Function to set the info of the node.//Postcondition: info = elem;Type getInfo() const;//Function to return the info of the node.//Postcondition: The value of info is returned.void setLink(nodeType<Type>…arrow_forward
- Import the ArrayList and List classes from the java.util package to create a list of phone numbers and also import the HashSet and Set classes from the java.util package to create a set of unique prefixes. Create a class called PhoneNumberPrefix with a main method that will contain the code to find the unique prefixes. Create a List called phoneNumbers and use the add method to add several phone numbers to the list. List<String> phoneNumbers = new ArrayList<>(); phoneNumbers.add("555-555-1234"); phoneNumbers.add("555-555-2345"); phoneNumbers.add("555-555-3456"); phoneNumbers.add("444-444-1234"); phoneNumbers.add("333-333-1234"); Create a Set called prefixes and use a for-each loop to iterate over the phoneNumbers list. For each phone number, we use the substring method to extract the first 7 characters, which represent the prefix, and add it to the prefixes set using the add method. Finally, use the println method to print the prefixes set, which will contain all of…arrow_forwardHelp so onnumarrow_forwardThe play method in the Player class of the craps game plays an entire game without interaction with the user. Revise the Player class so that its user can make individual rolls of the dice and view the results after each roll. The Player class no longer accumulates a list of rolls, but saves the string representation of each roll after it is made. Add new methods rollDice, getNumberOfRolls, isWinner, and isLoser to the Player class. The last three methods allow the user to obtain the number of rolls and to determine whether there is a winner or a loser. The last two methods are associated with new Boolean instance variables (winner and loser respectively). Two other instance variables track the number of rolls and the string representation of the most recent roll (rollsCount and roll). Another instance variable (atStartup) tracks whether or not the first roll has occurred. At instantiation, the roll, rollsCount, atStartup, winner, and loser variables are set to their appropriate…arrow_forward
- You are required to complete the LinkedList class. This class is used as a linked list that has many methods to perform operations on the linked list. To create a linked list, create an object of this class and use the addFirst or addLast or add (must be completed) to add nodes to this linked list(I created one linked list in TestLinkedList). I’ve already created the class for you and have some completed method in there, your job is to complete the empty methods.For every method you have to complete, you are provided with a header, Do not modify those headers( method name, return type or parameters). You have to complete the body of the method. public void add(T afterThis, T info) The parameter info will be the information the new node should contain, and the new node will be added after the node that contains the parameter afterThis as an information public void removeFirst() Removes the first node in the linked list. Hint: be aware of empty lists. public void removeLast() Removes…arrow_forwardYou are required to complete the LinkedList class. This class is used as a linked list that has many methods to perform operations on the linked list. To create a linked list, create an object of this class and use the addFirst or addLast or add(must be completed) to add nodes to this linked list(I created one linked list in TestLinkedList). I've already created the class for you and have some completed method in there, your job is to complete the empty methods. For every method you have to complete, you are provided with a header, Do not modify those headers(method name, return type or parameters). You have to complete the body of the method. public void add(T after This, T info) The parameter info will be the information the new node should contain, and the new node will be added after the node that contains the parameter after This as an information. public void removeFirst() Removes the first node in the linked list. Hint: be aware of empty lists. public void removeLast() Removes…arrow_forward5 and 6arrow_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