Hi, I am not sure what's wrong with my code. Can you please check why it is giving me an error?   In the starter file is a partial implementation of a doubly-linked list in DoublyLinkedList.java. We will write three new methods in this class to provide additional functionality. Write a method addFirst that adds a new element at the beginning of a DoublyLinkedList. Write a method addLast that adds a new element at the end of a DoublyLinkedList. Write a method removeFirst that removes and returns the first element of a DoublyLinkedList. Try to keep your implementations as simple as possible. For example, recall this definition of addFirst in the (Singly) LinkedList class: public void addFirst(E value) { head = new Node(value, head); } In the DoublyLinkedList class, you will need to keep the three instance variables head, tail, and count updated in all methods. Note that addFirst and addLast will be symmetric to each other, as will removeFirst and removeLast (provided in the starter code).

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

Hi, I am not sure what's wrong with my code. Can you please check why it is giving me an error?

 

In the starter file is a partial implementation of a doubly-linked list in DoublyLinkedList.java. We will write three new methods in this class to provide additional functionality.

  1. Write a method addFirst that adds a new element at the beginning of a DoublyLinkedList.

  2. Write a method addLast that adds a new element at the end of a DoublyLinkedList.

  3. Write a method removeFirst that removes and returns the first element of a DoublyLinkedList.

Try to keep your implementations as simple as possible. For example, recall this definition of addFirst in the (Singly) LinkedList class:

public void addFirst(E value) { head = new Node(value, head); }

In the DoublyLinkedList class, you will need to keep the three instance variables head, tail, and count updated in all methods. Note that addFirst and addLast will be symmetric to each other, as will removeFirst and removeLast (provided in the starter code).

 
And here is the rest of the given code from the file I've attached!
 
public static void main(String[] args) {
 
DoublyLinkedList<Integer> nums = newDoublyLinkedList<Integer>();
 
// Test the instance methods
// TODO: add your own tests of the instance methods
 
}
 
/**
* Node class for DoublyLinkedList
*/
protectedclassNode {
privateEdata;
privateNodenext;
privateNodeprev;
 
/**
* Constructs a Node.
*
* @param v the value at this Node
* @param next reference to the next Node
* @param prev reference to the previous Node
*/
publicNode(Ev, Nodenext, Nodeprev) {
data = v;
this.next = next;
this.prev = prev;
if (next != null) {
next.setPrev(this);
}
if (prev != null) {
prev.setNext(this);
}
}
 
/**
* Constructs a Node with null references.
*
* @param v the value to be stored in this Node
*/
publicNode(Ev) {
this(v, null, null);
}
 
/**
* Returns the Node referenced by next.
*
* @return returns reference to the next Node
*/
publicNodenext() {
returnnext;
}
 
/**
* Returns the Node referenced by prev.
*
* @return returns reference to the prev Node
*/
publicNodeprev() {
returnprev;
}
 
/**
* Sets the next field of this Node.
*
* @param next Node that will be referenced by next
*/
publicvoidsetNext(Nodenext) {
this.next = next;
}
 
/**
* Sets the prev field of this Node.
*
* @param prev Node that will be referenced by prev
*/
publicvoidsetPrev(Nodeprev) {
this.prev = prev;
}
 
/**
* Returns the value field of this Node.
*
* @return value at Node
*/
publicEvalue() {
returndata;
}
 
/**
* Sets the value field of this Node.
*
* @param value value to be stored at this Node
*/
publicvoidsetValue(Evalue) {
data = value;
}
 
/**
* Returns a String representation of this node.
*
* @return String representation of this Node, enclosed in < >
*/
publicStringtoString() {
return"<Node: "+value()+">";
}
}
}
public class DoublyLinkedList<E> {
protected Node head;
protected Node tail;
protected int count;
public DoublyLinkedList() {
head = null;
tail = null;
count = 0;
}
public void addFirst (E value) {
Node<E> newNode = new Node<E> (value);
0) {
tail = newNode;
if (count ==
} else {
head.setPrevious (newNode);
newMode.setNext (head);
head = newNode;
count++;
}
public void addLast (E value) {
Node<E> newNode = new Node<E> (value);
if (count ==
0) {
head = newNode;
} else {
tail.setNext (newNode);
newNode.setPrevious (tail);
tail = newNode;
count++;
}
public E removeFirst() {
if (count == = 0) {
return null;
}
E value head.getValue();
if (count
== 1) {
head = null;
tail = null;
} else {
head = head.getNext();
head.setPrevious (null);
Transcribed Image Text:public class DoublyLinkedList<E> { protected Node head; protected Node tail; protected int count; public DoublyLinkedList() { head = null; tail = null; count = 0; } public void addFirst (E value) { Node<E> newNode = new Node<E> (value); 0) { tail = newNode; if (count == } else { head.setPrevious (newNode); newMode.setNext (head); head = newNode; count++; } public void addLast (E value) { Node<E> newNode = new Node<E> (value); if (count == 0) { head = newNode; } else { tail.setNext (newNode); newNode.setPrevious (tail); tail = newNode; count++; } public E removeFirst() { if (count == = 0) { return null; } E value head.getValue(); if (count == 1) { head = null; tail = null; } else { head = head.getNext(); head.setPrevious (null);
}
public E removeLast() {
if (tail == null) {
return null;
count--;
return value ();
}
}
else {
E returnValue = tail.value();
tail = tail.prev();
if (tail == null) {
head = null;
}
else {
}
}
tail.setNext (next:null);
public String toString() {
Node finger = head;
String result = "[ ";
while (finger != null) {
count--;
return returnValue;
result + finger.toString() +
finger = finger.next();
}
result += "]";
return result;
/**
* Returns the number of elements in this list.
*
* @return the number of elements in this list
*/
public int size() {
return count;
Run | Debug
public static void main(String[] args) {
Transcribed Image Text:} public E removeLast() { if (tail == null) { return null; count--; return value (); } } else { E returnValue = tail.value(); tail = tail.prev(); if (tail == null) { head = null; } else { } } tail.setNext (next:null); public String toString() { Node finger = head; String result = "[ "; while (finger != null) { count--; return returnValue; result + finger.toString() + finger = finger.next(); } result += "]"; return result; /** * Returns the number of elements in this list. * * @return the number of elements in this list */ public int size() { return count; Run | Debug public static void main(String[] args) {
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 7 steps with 7 images

Blurred answer
Knowledge Booster
Hash Table
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.
Similar questions
  • SEE MORE QUESTIONS
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