Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
Expert Solution & Answer
Book Icon
Chapter 3, Problem 5R

Explanation of Solution

Consequences when delete the two lines from code:

Note: Refer Code Fragment 3.15 “SinglyLinkedList class definition” in the Text book.

The SinglyLinkedList class contains number of methods such as “size()”, “first()”, “last()”, “addFirst()”, “addLast()”, “removeLast()”, and “removeFirst()”.

In removeFirst() method,

  • Remove the line number “51” and “52” in this method.
    • That is, resetting the “tail” field to “null”.

Modified:

In the above code, just remove or delete the Line “51” and “52”. Then the code is given below:

  //Method definition

public E removeFirst( )                //Line 46

{

  //Check whether the linked list is empty

  if (isEmpty( ))                //Line 47

  //Return null, there is no element to remove

  return null;

/*Get the head node from linked list and assigned it. */

  E answer = head.getElement( );    //Line 48

  //Assign the head node as next node of head

  head = head...

Blurred answer
Students have asked these similar questions
please 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…
Important: Make all your implementation in the same attached file and submit only one file in the submission link. Given the linked list data structure discussed in the lecture, implement a sub-class TSortedList that makes sure that elements are inserted and maintained in an ascending order in the list. So, given the input sequence {1,7,3,11,5}, when printing the list after inserting the last element it should print like 1, 3, 5, 7, 11. Note that with inheritance, you have to only care about the insertion situation as deletion should still be handled by the parent class. In the basic implementation, assume that you only have head which is a pointer to the head of the list. Now, if we have both head and tail pointers, what improvements can be done to speed up the insertion?
Add a new public member function to the LinkedList class named reverse() which reverses the items in the list.  You must take advantage of the doubly-linked list structure to do this efficiently as discussed in the videos/pdfs, i.e. swap each node’s prev/next pointers, and finally swap headPtr/tailPtr.  Demonstrate your function works by creating a sample list of a few entries in main(), printing out the contents of the list, reversing the list, and then printing out the contents of the list again to show that the list has been reversed.   Note: your function must actually reverse the items in the doubly-linked list, not just print them out in reverse order! Note: we won't use the copy constructor in this assignment, and as such you aren't required to update the copy constructor to work with a doubly-linked list.   This is what I have so far but its not working! template<class ItemType>void LinkedList<ItemType>::reverse(){   Node<ItemType>*curPtr,*prev,*next;…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education