2 LinkedList remove and iterator

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
2 LinkedList remove and iterator
Your class ICS211LinkedList.java must have the exact same class variables as LinkedList.java, that is, head, tail, and size. You must also keep the checkInvariants() method, and call it at the beginning and end of both of the two public
methods (remove and iterator) you must add in creating your ICS211LinkedList class.
2.1 LinkedList remove
Implement the E remove(int index) method defined by the java.util.List interface.
Removing a node from a linked list requires access to the node preceding the node to be removed. This means you must have a loop which starts at the head and follow the links until you are at the correct index.
If the index is size-1, which is the last valid index of the list, remember to update the tail reference.
If the index is 0, this is a special case that updates the head reference. If the list only has one element, the tail reference must also be updated.
Make sure your code works correctly for
• an empty list (must throw IndexOutOfBoundsException.html)
●
illegal indices (must throw IndexOutOfBoundsException.html)
• a list of size 1
removing the first value
• removing the last value
2.2 LinkedList iterator
In your ICS211LinkedList class, implement the iterator (.) method.
2.3 LinkedListIterator remove
In your LinkedListIterator private class, implement the remove (.). method.
Note that the remove method of the ICS211LinkedList class returns the value removed, and the remove method of the private LinkedListIterator class does not.
Also, to avoid having to loop in the remove method, you must add a class variable to the LinkedListIterator private class. This new class variable must keep track of the previous node. When the iterator is first created, there is no previous node,
so the constructor must initialize this variable to null. If remove is called before the first call to next, remove must throw an IllegalStateException. Every call to the next () method must update the value of this variable.
As in the linked list remove method, you must also take into consideration special cases of removing the first and the last elements of the linked list.
The implementation of this method is must not call the remove method of the ICS211LinkedList class, which takes O(n) time -- this method must work in O(1) time and is not allowed to have any loops.
Transcribed Image Text:2 LinkedList remove and iterator Your class ICS211LinkedList.java must have the exact same class variables as LinkedList.java, that is, head, tail, and size. You must also keep the checkInvariants() method, and call it at the beginning and end of both of the two public methods (remove and iterator) you must add in creating your ICS211LinkedList class. 2.1 LinkedList remove Implement the E remove(int index) method defined by the java.util.List interface. Removing a node from a linked list requires access to the node preceding the node to be removed. This means you must have a loop which starts at the head and follow the links until you are at the correct index. If the index is size-1, which is the last valid index of the list, remember to update the tail reference. If the index is 0, this is a special case that updates the head reference. If the list only has one element, the tail reference must also be updated. Make sure your code works correctly for • an empty list (must throw IndexOutOfBoundsException.html) ● illegal indices (must throw IndexOutOfBoundsException.html) • a list of size 1 removing the first value • removing the last value 2.2 LinkedList iterator In your ICS211LinkedList class, implement the iterator (.) method. 2.3 LinkedListIterator remove In your LinkedListIterator private class, implement the remove (.). method. Note that the remove method of the ICS211LinkedList class returns the value removed, and the remove method of the private LinkedListIterator class does not. Also, to avoid having to loop in the remove method, you must add a class variable to the LinkedListIterator private class. This new class variable must keep track of the previous node. When the iterator is first created, there is no previous node, so the constructor must initialize this variable to null. If remove is called before the first call to next, remove must throw an IllegalStateException. Every call to the next () method must update the value of this variable. As in the linked list remove method, you must also take into consideration special cases of removing the first and the last elements of the linked list. The implementation of this method is must not call the remove method of the ICS211LinkedList class, which takes O(n) time -- this method must work in O(1) time and is not allowed to have any loops.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Operations of Linked 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