Question: LLab 1 3 List Implementation Goal In this lab you will explore two implementations of the ADT list. The first implementation will use an array
LLab List Implementation
Goal
In this lab you will explore two implementations of the ADT list. The first implementation will use an array
and the second will use a linked structure. In both cases you will create new methods that work directly with the
implementations. You will implement a reverse method that will reverse the order of the items in the list.
You will also implement a cycle method that will move the first item in the list to the last position.
Resources
Appendix A: Creating Classes from Other Classes
Chapter : Lists
Chapter : List Implementations That Use Arrays
Chapter : A List Implementation That Links Data
In javadoc directory
ListInterface.html Interface documentation for the interface ListInterface
Java Files
AList.java
ArrayListExtensionsTest.java
LList.java
LinkedListExtensionsTest.java
ListInterface.java
Introduction
As was seen in the last lab, a list is an ordered collection of elements supporting basic operations such as add
and remove. One way to implement a list is to use an array. The other standard implementation is a linked
structure. In this lab, you will take a working implementation of each basic type and create two new methods.
If you have not done so already, take a moment to examine the code in AList.java
Consider the code that implements the add method.
public void addint newPosition, T newEntry
checkInitialization;
if newPosition && newPosition numberOfEntries
if newPosition numberOfEntries
makeRoomnewPosition;
listnewPosition newEntry;
numberOfEntries;
ensureCapacity; Ensure enough room for next add
else
throw new IndexOutOfBoundsException
"Illegal position given to add operation.";
end add
Lab List Implementation
Doubles the size of the array list if it is full.
private void ensureCapacity
int capacity list.length ;
if numberOfEntries capacity
int newCapacity capacity;
checkCapacitynewCapacity; Is capacity too big?
list Arrays.copyOflist newCapacity ;
end ensureCapacity
Makes room for a new entry at newPosition.
Precondition: newPosition numberOfEntries;
numberOfEntries is list's length before addition.
checkInitialization has been called.
private void makeRoomint newPosition
assert newPosition && newPosition numberOfEntries ;
int newIndex newPosition;
int lastIndex numberOfEntries;
Move each entry to next higher index, starting at end of
array and continuing until the entry at newIndex is moved
for int index lastIndex; index newIndex; index
listindex listindex;
end makeRoom
Let's trace the last statement in the following code fragment.
AList x new AList;
xadda;
xaddb;
xaddc;
xaddd;
xaddx;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
