Create an addAll method into the LList class given below. the method should add an aray of items to the end of the list.  method header: public void addAll(T[] items)

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

Create an addAll method into the LList class given below. the method should add an aray of items to the end of the list. 

method header:

public void addAll(T[] items)

public class LList<T extends Comparable<? super T>> implements ListInterface<T> {
private Node firstNode;
private int number0fEntries;
public LList( {
initializeDataFields();
}
public void add(T newEntry) {
Node newNode
new Node(newEntry);
if (isEmpty()) {
firstNode
newNode;
} else {
Node lastNode
getNodeAt(number0fEntries);
%3D
lastNode.setNextNode(newNode); // equivalent to lastNode.next = newNode;
}
number0fEntries++;
}
public void add(int givenPosition, T newEntry) {
if ((givenPosition
1) && (givenPosition
number0fEntries
1)) {
Node newNode = new Node(newEntry);
if (givenPosition
newNode.setNextNode(firstNode);
1) { // Case 1: adding first in the chain; also works for an empty list
firstNode
newNode;
} else { // Case 2: givenPosition > 1 (and list is not empty)
Node nodeBefore = getNodeAt(givenPosition
Node nodeAfter = nodeBefore.next;
newNode.setNextNode(nodeAfter);
nodeBefore.setNextNode(newNode);
}
number0fEntries++;
} else {
throw new IndexOut0fBoundsException("Illegal position given to add operation.");
}
1);
public
remove(int givenPosition) {
T result
null;
if (validPosition(givenPosition)) {
// Assertion: !isEmpty()
if (givenPosition
result = firstNode.data;
firstNode =
} else { // Case 2: Not first entry
Node nodeBefore = getNodeAt(givenPosition
Node nodeToRemove = nodeBefore.next;
result = nodeToRemove.data;
1) { // Case 1: Remove first entry
firstNode.next;
1);
Node nodeAfter
nodeToRemove.next;
nodeBefore.setNextNode(nodeAfter); // Remove entry
}
number0fEntries--;
return result;
} else {
throw new Index0ut0fBoundsException("Illegal position given to remove operation.");
}
}
public T replace(int givenPosition, T newEntry) {
if (validPosition(givenPosition)) {
// Assertion: !isEmpty()
getNodeAt(givenPosition);
desiredNode.data;
Node desiredNode
T originalEntry
desiredNode.data
newEntry;
Transcribed Image Text:public class LList<T extends Comparable<? super T>> implements ListInterface<T> { private Node firstNode; private int number0fEntries; public LList( { initializeDataFields(); } public void add(T newEntry) { Node newNode new Node(newEntry); if (isEmpty()) { firstNode newNode; } else { Node lastNode getNodeAt(number0fEntries); %3D lastNode.setNextNode(newNode); // equivalent to lastNode.next = newNode; } number0fEntries++; } public void add(int givenPosition, T newEntry) { if ((givenPosition 1) && (givenPosition number0fEntries 1)) { Node newNode = new Node(newEntry); if (givenPosition newNode.setNextNode(firstNode); 1) { // Case 1: adding first in the chain; also works for an empty list firstNode newNode; } else { // Case 2: givenPosition > 1 (and list is not empty) Node nodeBefore = getNodeAt(givenPosition Node nodeAfter = nodeBefore.next; newNode.setNextNode(nodeAfter); nodeBefore.setNextNode(newNode); } number0fEntries++; } else { throw new IndexOut0fBoundsException("Illegal position given to add operation."); } 1); public remove(int givenPosition) { T result null; if (validPosition(givenPosition)) { // Assertion: !isEmpty() if (givenPosition result = firstNode.data; firstNode = } else { // Case 2: Not first entry Node nodeBefore = getNodeAt(givenPosition Node nodeToRemove = nodeBefore.next; result = nodeToRemove.data; 1) { // Case 1: Remove first entry firstNode.next; 1); Node nodeAfter nodeToRemove.next; nodeBefore.setNextNode(nodeAfter); // Remove entry } number0fEntries--; return result; } else { throw new Index0ut0fBoundsException("Illegal position given to remove operation."); } } public T replace(int givenPosition, T newEntry) { if (validPosition(givenPosition)) { // Assertion: !isEmpty() getNodeAt(givenPosition); desiredNode.data; Node desiredNode T originalEntry desiredNode.data newEntry;
// Assertion: !isEmpty()
Node desiredNode
T originalEntry
desiredNode.data
getNodeAt (givenPosition);
desiredNode.data;
newEntry;
return originalEntry;
} else {
throw new IndexOut0fBoundsException("Illegal position given to replace operation.");
}
public T getEntry(int givenPosition) {
if (validPosition(givenPosition)) {
// Assertion: !isEmpty(O
return getNodeAt(givenPosition).data;
} else {
throw new Index0ut0fBoundsException("Illegal position given to getEntry operation.");
}
}
public boolean contains(T anEntry) {
boolean found = false;
Node currentNode
firstNode;
while (!found && (currentNode != nul1)) {
if (anEntry.equals(currentNode.data)) {
true;
found
} else {
currentNode = currentNode.next;
}
}
return found;
}
public void clear() {
initializeDataFields();
}
public int getLength() {
return number0fEntries;
}
public boolean isEmpty() {
boolean result;
if (number0fEntries
// Assertion: firstNode == null
result = true;
} else {
// Assertion: firstNode != null
result = false;
}
return result;
0) { // Or getLength()
}
public T[] toArray() {
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] result = (T[]) new Comparable[number0fEntries]; // changed from Object[]
int index = 0;
Node currentNode =
while ((index
result[index]
currentNode = currentNode.next;
index++;
}
firstNode;
number0fEntries) && (currentNode != null)) {
currentNode.data;
return result;
}
Transcribed Image Text:// Assertion: !isEmpty() Node desiredNode T originalEntry desiredNode.data getNodeAt (givenPosition); desiredNode.data; newEntry; return originalEntry; } else { throw new IndexOut0fBoundsException("Illegal position given to replace operation."); } public T getEntry(int givenPosition) { if (validPosition(givenPosition)) { // Assertion: !isEmpty(O return getNodeAt(givenPosition).data; } else { throw new Index0ut0fBoundsException("Illegal position given to getEntry operation."); } } public boolean contains(T anEntry) { boolean found = false; Node currentNode firstNode; while (!found && (currentNode != nul1)) { if (anEntry.equals(currentNode.data)) { true; found } else { currentNode = currentNode.next; } } return found; } public void clear() { initializeDataFields(); } public int getLength() { return number0fEntries; } public boolean isEmpty() { boolean result; if (number0fEntries // Assertion: firstNode == null result = true; } else { // Assertion: firstNode != null result = false; } return result; 0) { // Or getLength() } public T[] toArray() { // The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] result = (T[]) new Comparable[number0fEntries]; // changed from Object[] int index = 0; Node currentNode = while ((index result[index] currentNode = currentNode.next; index++; } firstNode; number0fEntries) && (currentNode != null)) { currentNode.data; return result; }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
List
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
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