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)
- 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
- Implement 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. Node* GetNode(int index) const;A private function which is only accessible to the class methods. . For example, index 0 corresponds to the head and index length-1 corresponds to end node of the linked list. The function returns NULL if the index is out of bound.Implement the following public methods:2. LinkedList();Constructor that sets head to NULL and length equal to zero.3. bool InsertAt(int data, int index);Insert a new node at the index. Return true if successful, otherwise,return false. The new node should be at the position “index” in the linked list after inserting it. You might have to use GetNode private function. these 3 partsarrow_forwardIN JAVA: Use a doubly-linked circular node class to implement the list. Refer to the illustration below.arrow_forwardYou 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_forward
- Assume 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_forwardIn python. Write a LinkedList class that has recursive implementations of the add and remove methods. It should also have recursive implementations of the contains, insert, and reverse methods. The reverse method should not change the data value each node holds - it must rearrange the order of the nodes in the linked list (by changing the next value each node holds). It should have a recursive method named to_plain_list that takes no parameters (unless they have default arguments) and returns a regular Python list that has the same values (from the data attribute of the Node objects), in the same order, as the current state of the linked list. The head data member of the LinkedList class must be private and have a get method defined (named get_head). It should return the first Node in the list (not the value inside it). As in the iterative LinkedList in the exploration, the data members of the Node class don't have to be private. The reason for that is because Node is a trivial class…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
- 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_forwardhead 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_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