Explanation of Solution
Method definition for “reverse()”:
The method definition for “reverse()” is given below:
/* Method definition for "reverse()" */
public void reverse()
{
//Create a list "rev_list" and set it to "null"
Node rev_list = null;
/* This loop will perform up to the first node equals to "null" */
while (first != null)
{
/* Move a node to rev_list from what leftovers of the original list */
/* Set a reference "ref" to first node */
Node ref = first;
/* Set "first" to next first node */
first = first.next;
/* Set next node to "rev_list" */
ref.next = rev_list;
/* Set the "rev_list" to reference "ref" */
rev_list = ref;
}
/* Make the list "rev_list" as the new list */
first = rev_list;
}
Explanation:
From the above method definition,
- Create a list “rev_list” and then set it to “null”.
- Performs “while” loop. This loop will perform up to the first node becomes “null”.
- Set a reference “ref” to first node.
- Set “first” to next value of “first”.
- Assign next node to “rev_list”.
- Assign the “rev_list” to reference “ref”.
- Finally make the list “rev_list” as new list.
Complete code:
The complete executable code for “reverse()” method is given below:
//Define "LinkedList1" class
class LinkedList1
{
/* The code for this part is same as the textbook of "LinkedList1" class */
/* Method definition for "reverse()"*/
public void reverse()
{
//Create a list "rev_list" and set it to "null"
Node rev_list = null;
/* This loop will perform up to the first node equals to "null" */
while (first != null)
{
/* Move a node to rev_list from what remains of the original list */
/* Set a reference "ref" to first node */
Node ref = first;
/* Set "first" to next first node */
first = first...
Want to see the full answer?
Check out a sample textbook solutionChapter 19 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Write a method max() that takes a reference to the first node in a linked list asargument and returns the value of the maximum key in the list. Assume that all keys arepositive integers, and return 0 if the list is emptyarrow_forwardIn java, how do I implement the following methods into a linkedlist class. 1) removeLast() – remove the last number of the list 2) removeLeast()- remove least number in the list 3) removeMax()- remove largest number in the list 4) removeFirst() – remove the first number of the list 5) addNum()- add another number to the listarrow_forwardWrite a method find() that takes a linked list and a string key as arguments and returns true if some node in the list has key as its item field, false otherwise.arrow_forward
- There are an undetermined number of nodes in the fastest linkedlist.Is there one that stands out as being the longest?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_forwardTrying 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…arrow_forward
- Implement program to adds methods to search a linked list for a data item with aspecified key value, and to remove an item with a specified key value. These, along with insertion at the start of the list, are the same operations carried out by the LinkList Workshop applet. Write The complete linkList2.cpp program.arrow_forwardApply the methods in the ListIterator interface to write a Java program in NetBeans that creates a LinkedList of four elements of type string, namely: Java, C#, PHP and Python.The program should then print out the elements initially in the original order, and then afterwards in reverse order using the ListIterator methods.arrow_forwardThe strongest linkedlist has undetermined nodes.Is one particularly long?arrow_forward
- Implement a class “LinkedList” which has two private data members head: A pointer to the Node class length: length of the linked listImplement the following private method: 1. bool InsertHead(int data); // Use InsertAt FunctionInsert at the start of the linked list. Return true2. bool InsertEnd(int data); // Use InsertAt FunctionInsert at the end of the linked list. Return true3. bool RemoveAt(int index);Remove and delete the node at position “index”. Return true if successful, otherwise, false. this in c++arrow_forwardYou might have heard about the "Guess the Word Game", so in this game the user has to guess the letter(s) of the word one by one. There are n chances given to guess its letters where n is the number of letters in that word. You have to implement given scenario with the help of “Doubly Linked List”. Create two linkedlist, first linkedlist will be containing of complete word e.g. ELEPHANT and second linkedlist will be containing of word but having some blank letters e.g. _LEP_AN_. You have to ask user for input for every letter and insert into second linkedlist as follows: Step 1: Letter 1: Insert into second linkedlist on first place and compare 1st value of first and second linkedlist if same then go to step 2 else go to step 1 Step 2: Letter 2: Insert into second linkedlist on fifth place and compare 5th value of first and second linkedlist if same then go to step 3 else go to step 2 Step 3: Letter 3: Insert into second linkedlist on last place and compare last value of…arrow_forwardImplement an instance method, called getFirstHalf, that belongs to yourThingLinkedBag. The method returns as output a linked list that includes allvalues in the first half of the bag's linked list. For example, for the following bag3-->5-->8-->9. the method returns 3-->5.If the bag includes an odd number of nodes, then the middle element belongs tothe first half and is included in the output.Make sure to include the methodheader. The bag should remain unchanged (i.e., do not remove the returnedvalues from the bag).arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education