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 20 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
- Make a doubly linked list and apply all the insertion, deletion and search cases. The node willhave an int variable in the data part. Your LinkedList will have a head and a tail pointer.Your LinkedList class must have the following functions: 1) insert a node1. void insertNodeAtBeginning(int data);2. void insertNodeInMiddle(int key, int data); //will search for keyand insert node after the node where a node’s data==key3. void insertNodeAtEnd(int data);2) delete a node1. bool deleteFirstNode(); //will delete the first node of the LL2. bool deleteNode(int key); //search for the node where its data==keyand delete that particular node3. bool deleteLastNode(); //will delete the last node of the LL3) Search a node1. Node* searchNodeRef(int key); //will search for the key in the datapart of the node2. bool searchNode(int key); The program must be completely generic, especially for deleting/inserting the middle nodes. Implement all the functions from the ABOVE STATEMENTS and make a login and…arrow_forward1. According to the following LinkedList, write pseudo code for the question belowHow do you insert a node with the data, "Brandon" between the node of "Chu" and the node of "Bethany"? 2.Assume that there is a linked list that connected several names.How do you set up a "while" loop to print the whole list? 3.What is the advantage of using a LinkedList over an ArrayList?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_forward
- 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…arrow_forwardAssume that the linked list class is ready and called (MyLinkedList) that contains nodes with int data, and next reference. Assume also that MyLinkedList contains only the following methods: addFirst(), addLast(), removeFirst(), removeLast(). Given that numbers are represented in linked lists (i.e. the number 567 will be represented in a linked list of three nodes; the first node contains 5, the second node contains 6 and the last node contains 7. Write a java method that receives the heads of two linked lists representing two numbers and finds the product (result of multiplication) list and returns its head. Note: You are free to use only one additional stack in your solution.arrow_forwardCreate an implementation of a doubly linked DoubleOrderedList class. You will need to create a DoubleNode class, a DoubleList class, and a DoubleIterator classarrow_forward
- head 7. Answer the questions considering the figure above. The list that you see above is a doubly DNode{ public T value, public DNode next, public DNode prev ..} When this list was created, they forgot to link the prev field of the nodes. Write a method existing list. Your method will be in the DoublyLinkedList class, so it does not need to tak since you will have an access to head.arrow_forwardpublic LLNode secondHalf(LLNode head) { }arrow_forwardI need the answer as soon as possiblearrow_forward
- Chu Bethany Daryl next pext next bead The above is a LinkedList. 3. Why do you not move the head node-reference and set up the p node-reference to iterate the moving from one node to the next node while you are inserting, removing nodes or printing a list? 4. What is the advantage of using a LinkedList over an ArrayList? Nextarrow_forwardo create the linkage for the nodes of our linkedlist. The class includes several methods for adding nodes to the list, removingnodes from the list, traversing the list, and finding a node in the list. We alsoneed a constructor method that instantiates a list. The only data member inthe class is the header node.use c#arrow_forwardGiven 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 public ItemNode(String itemInit, ItemNode nextLoc) {…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