Java Programming language Help please.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Java Programming language

Help please. 

 

46
47
48
49
50
51
52
public E removeFirst() {
if (isEmpty()) return null;
E answer = head.getElement();
head.getNext();
head =
53
54 }
55 }
size--;
if (size
0)
tail = null;
return answer;
==
// removes and returns the first element
// nothing to remove
// will become null if list had only one node
// special case as list is now empty
Transcribed Image Text:46 47 48 49 50 51 52 public E removeFirst() { if (isEmpty()) return null; E answer = head.getElement(); head.getNext(); head = 53 54 } 55 } size--; if (size 0) tail = null; return answer; == // removes and returns the first element // nothing to remove // will become null if list had only one node // special case as list is now empty
The removeFirst method of the SinglyLinked List class includes a special case to
reset the tail field to null when deleting the last node of a list (see lines 51 and 52
of Code Fragment 3.15). What are the consequences if we were to remove those
two lines from the code? Explain why the class would or would not work with
such a modification.
Transcribed Image Text:The removeFirst method of the SinglyLinked List class includes a special case to reset the tail field to null when deleting the last node of a list (see lines 51 and 52 of Code Fragment 3.15). What are the consequences if we were to remove those two lines from the code? Explain why the class would or would not work with such a modification.
Expert Solution
Step 1 ans

 1 public class SinglyLinked List<E> 
{
 (nested Node class goes here) 
......
14
 15 // instance variables of the SinglyLinked List private Node<E> head = null; 
private Node<E> tail = null; private int size = 0; 

16 // head node of the list (or null if empty) last node of the list (or null if empty) number of nodes in the list 
17 // constructs an initially empty list public SinglyLinkedList()
 {
 }
 // access methods public int size()
 {
 return size; 
}
 public boolean isEmpty()
 {
 return size == 0; 

public E first() 

// returns (but does not remove) the first element
 if (isEmpty()) return null; 
24 return head.getElement();
 }
 public E last() 

// returns (but does not remove) the last element
 if (isEmpty()) 
return null; 
28 return tail.getElement(); 
}
 // update methods public void addFirst(E e) 
{
 head = new Node<>(e, head); 0) 
// adds element e to the front of the list create and link a new node
 if (size == tail = head; 
// special case: new node becomes tail also size++; 
36 

37
 38 public void add Last(E e) 

// adds element e to the end of the list 

Node<E> newest = new Node<>(e, null); 
// node will eventually be the tail
 if (isEmpty()) 
39 
40 head newest; 
// special case: previously empty list
 41
 else
 42 
tail.setNext(newest); 
new node after existing tail 
43 tail newest; 
// new node becomes the tail
 44
 size++; 
45
 } 
46 public E removeFirst()
 { 
// removes and returns the first element 
// nothing to remove 
47
 if (isEmpty()) 
return null;
 E  answer = head.getElement(); 
48 head.getNext(); 
// will become null 
if list had only one node size--; == 0)
 if (size)
 tail = null;
 // special case as list is now empty
 return answer;
}
}

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Multithreading Methods
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education