Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 3, Problem 12R
Explanation of Solution
Method rotate() in Singly linked list:
//Define the rotate() method
public void rotate()
{
//Check whether the list is empty
if(isEmpty())
//Return it
return;
//Otherwise, execute else statement
else
{
/*Set the next node of "tail" as "head" after existing tail. */
tail.setNext((Node<E>) head);
}
/*Set the "tail" as "head"*/
tail=(Node<E>)head;
//Set next node of head as "head"
head = head...
Expert Solution & Answer
Learn your wayIncludes step-by-step video
schedule03:02
Students have asked these similar questions
Write a java program to Implement the size( ) method for the DoublyLinkedList class, assuming that wedid not keep the size variable as an instance variable.
Implement a linked list of integers as aclass LinkedList. Build the following methods:✓ print that prints the content of the linked list;✓ addFirst that adds a new node to the beginning (the head) of thelinked list;✓ addLast that adds a new node to the end (the tail) of the linked list;✓ indexOf that finds a specific node by its value, and returns node’sindex (node’s position from the left in the linked list); if the valueis not present in the linked list, it returns −1;✓ deleteFirst that deletes the first node in the linked list;✓ deleteLast that deletes the last node in the linked list.Test your class creating a list in the main and1) adding one by one nodes 2, 4, 8 to the tail;2) adding nodes -2, -8 to the head;3) adding a node 9 to the tail;4) printing the list;5) printing indexOf(4);6) printing contains(9);7) deleting one by one all the nodes in the list – either from the tail orfrom the head – and printing the result after each deletion.
THEN AFTER THIS
Add a new method IN THE…
Implement a nested class DoubleNode for building doubly-linked lists, whereeach node contains a reference to the item preceding it and the item following it in thelist (null if there is no such item). Then implement static methods for the followingtasks: insert at the beginning, insert at the end, remove from the beginning, removefrom the end, insert before a given node, insert after a given node, and remove a givennode.
Chapter 3 Solutions
Data Structures and Algorithms in Java
Ch. 3 - Prob. 1RCh. 3 - Write a Java method that repeatedly selects and...Ch. 3 - Prob. 3RCh. 3 - The TicTacToe class of Code Fragments 3.9 and 3.10...Ch. 3 - Prob. 5RCh. 3 - Prob. 6RCh. 3 - Prob. 7RCh. 3 - Prob. 8RCh. 3 - Prob. 9RCh. 3 - Prob. 10R
Ch. 3 - Prob. 11RCh. 3 - Prob. 12RCh. 3 - Prob. 13RCh. 3 - Prob. 14RCh. 3 - Prob. 15RCh. 3 - Prob. 16RCh. 3 - Prob. 17CCh. 3 - Prob. 18CCh. 3 - Prob. 19CCh. 3 - Give examples of values for a and b in the...Ch. 3 - Suppose you are given an array, A, containing 100...Ch. 3 - Write a method, shuffle(A), that rearranges the...Ch. 3 - Suppose you are designing a multiplayer game that...Ch. 3 - Write a Java method that takes two...Ch. 3 - Prob. 25CCh. 3 - Prob. 26CCh. 3 - Prob. 27CCh. 3 - Prob. 28CCh. 3 - Prob. 29CCh. 3 - Prob. 30CCh. 3 - Prob. 31CCh. 3 - Prob. 32CCh. 3 - Prob. 33CCh. 3 - Prob. 34CCh. 3 - Prob. 35CCh. 3 - Write a Java program for a matrix class that can...Ch. 3 - Write a class that maintains the top ten scores...Ch. 3 - Prob. 38PCh. 3 - Write a program that can perform the Caesar cipher...Ch. 3 - Prob. 40PCh. 3 - Prob. 41PCh. 3 - Prob. 42PCh. 3 - Prob. 43P
Knowledge Booster
Similar questions
- Implement a method to remove a node from the centre of a singly linked list—that is, any node other than the first and last nodes, not necessarily in that precise order.EXAMPLElnput:the node c from the linked list a->b->c->d->e->fResult: nothing is returned, but the new linked list looks like a ->b->d->e->farrow_forwardSuppose we want to extend the PositionalList ADT with a method, indexOf(p), that returns the current index of the element stored at position p. Write this method using only other methods of the PositionalList interface (not details of our LinkedPositionalList implementation). Write the necessary code to test the method. Hint: Count the steps while traversing the list until encountering position p public interface PositionalList<E> extends Iterable<E> { int size(); boolean isEmpty(); Position<E> first(); Position<E> last(); Position<E> before(Position<E> p) throws IllegalArgumentException; Position<E> after(Position<E> p) throws IllegalArgumentException; Position<E> addFirst(E e); Position<E> addLast(E e); Position<E> addBefore(Position<E> p, E e) throws IllegalArgumentException; Position<E> addAfter(Position<E> p, E e) throws IllegalArgumentException; E set(Position<E> p, E e) throws…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
- Implement the complete code Delete that, when given an info value newElement, finds and deletes the first element with this value, if it exists. This involves also considering the four special cases listed for deleting a node from the beginning or the end of a linked list.arrow_forwardExtend the LinkedList class adding a newmethod printMiddle that prints values of the middle node(s) of a linkedlist in one pass. Assume that we do not know the size of the linked listahead of time.Note that if the list has an odd number of nodes, there will be onlyone middle node; otherwise, there will be two middle nodes.E.g., applying printMiddle to the linked list [1 → 7 → 5 → 4 → 2],we get 5; applying it to [1 → 2 → 3 → 4 → 5 → 6], we get 3 and 4.Extra1. You may get 5 extra points for each of the problems 1.a,1.b, and 2 (15 points in total)2. To this end, you have to explain3 whichof the following classes of runtime complexities better characterizes yourmethod – O(log n), O(n), O(n2), or O(2n). Simply briefly write it in thecomments after the method declaration. For example, for the look-upfunction indexOf, your explanation might look like:public int indexOf(int element) {// O(n): Since we need to iterate over// the entire array to find the number.// This number may be at the end…arrow_forwardThe strongest linkedlist is made up of an unknown number of nodes. Is there one in particular that stands out as being particularly lengthy?arrow_forward
- The strongest linkedlist is made up of an unknown number of nodes.Is there one in particular that stands out as being particularly lengthy?arrow_forwardDesign and implement a getLastHalf() method for the MyLinkedList class. The method should return the last half of the list as a new list leaving the original list intact. If the list has an uneven number of elements the extra one element must be in the list returned. Example : [1,3,5,4] return [5,4] and [1,6,9] return [6,9]. Follow the three step method for designing the method Develop the java code for the method Create a test program to test the method thoroughly using the Integer wrapper class.arrow_forwardDesign and implement a getLastHalf() method for the MyLinkedList class. The method should return the last half of the list as a new list leaving the original list intact. If the list has an uneven number of elements the extra one element must be in the list returned. Example : [1,3,5,4] return [5,4] and [1,6,9] return [6,9]. Follow the three step method for designing the method Develop the java code for the method Create a test program to test the method thoroughly using the Integer wrapper class. public class MyLinkedList<E> { private Node<E> head, tail; public MyLinkedList() { head = null; tail = null; } /** Return the head element in the list */ public E getFirst() { if (head == null) { return null; } else { return head.element; } } /** Return the last element in the list */ public E getLast() { if (head==null) { return null; } else { return tail.element; } } /** Add an element to the beginning of the list */ public void prepend(E e) { Node<E> newNode = new…arrow_forward
- Design and implement a getLastHalf() method for the MyLinkedList class. The method should return the last half of the list as a new list leaving the original list intact. If the list has an uneven number of elements the extra one element must be in the list returned. Example : [1,3,5,4] return [5,4] and [1,6,9] return [6,9]. Follow the three step method for designing the method Develop the java code for the method Create a test program to test the method thoroughly using the Integer wrapper class.arrow_forwardUse a Doubly Linked List to implement a Dequea. Define a Deque interface.b. Define a LinkedDeque class (Node class)..c. Define a Test class to test all methods first code package Deque; public class DLinkedList<E> { private Node<E> header = null; private Node<E> trailer = null; private int size = 0; public DLinkedList() { header = new Node<>(null, null,null); trailer = new Node<>(null,header,null); header.setNext(trailer); size = 0; } public int getSize() { return size; } public boolean isEmpty() { return size ==0; } public E getFirst(){ if(isEmpty()) { return null; } return header.getNext().getElement(); } public E getLast(){ if(isEmpty()) { return null; } return trailer.getPrev().getElement(); } public void addFirst(E e) { addBetween(e,header,header.getNext()); } public void addLast(E e) { addBetween(e,trailer.getPrev(), trailer); } public E removeFirst() { if (isEmpty()) { return null; } return remove(header.getNext( )); } public E…arrow_forwardImplement a class for Circular Doubly Linked List (with a dummy header node) whichstores integers in unsorted order. Your class definitions should look like as shown below:class CDLinkedList;class DNode {friend class CDLinkedList;private:int data;DNode* next;DNode* prev;};class CDLinkedList {private:DNode head; // Dummy header nodepublic:CDLinkedList(); // Default constructorbool insert (int val); // Inserts val into the linked list. Time complexity: O(1)bool removeLastValue (int v);//Note: Remove the last val from the linked list. Time complexity: O(1)void findMiddleValue(); // find middle value of the linklist and delete it.void display(); // Displays the contents of linked list on screen …};arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- 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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education