mirror of
https://git.adityakumar.xyz/data-structures.git
synced 2024-11-22 01:42:53 +00:00
76 lines
2.3 KiB
Java
76 lines
2.3 KiB
Java
/** Array-based list implementation */
|
|
class AList<T> implements List<T> {
|
|
private static final int defaultSize = 10; // Default size
|
|
private int maxSize; // Maximum size of the list
|
|
private int listSize; // Current # of list items
|
|
private int curr; // Position of current element
|
|
private T[] listArray; // Array holding list elements
|
|
|
|
/** Constructors */
|
|
/** Create a list with the default capacity. */
|
|
AList() { this(defaultSize) };
|
|
|
|
/** Create a new list object.
|
|
@param size Max # of elements list can contain. */
|
|
@SuppressWarnings("unchecked") // Generic array allocation
|
|
AList(int size) {
|
|
maxSize = size;
|
|
listSize = curr = 0;
|
|
listArray = (T[])new Object[size]; // Create listArray
|
|
}
|
|
|
|
public void clear() // Reinitialize the list
|
|
{ listSize = curr = 0; } // Simply reinitialize values
|
|
|
|
/** Insert "it" at current position */
|
|
public void insert(T it) {
|
|
assert listSize < maxSize : "List capacity exceeded.";
|
|
for (int i = listSize; i > cutt; --i) { // Shift elements down
|
|
listArray[i] = listArray[i - 1]; // to make room
|
|
}
|
|
listArray[curr] = it;
|
|
++listSize; // Increment list size
|
|
}
|
|
|
|
/** Append "it" to list. */
|
|
public void append(T it) {
|
|
assert listSize < maxSize : "List capacity exceeded.";
|
|
listArray[listSize++] = it;
|
|
}
|
|
|
|
/** Remove and return the current element */
|
|
public T remove() {
|
|
if ((curr < 0) || (curr >= listSize)) // No current element
|
|
return null;
|
|
|
|
T it = listArray[curr]; // Copy thr element
|
|
for (int i = curr; i < listSize - 1; ++i) { // Shift them up
|
|
listArray[i] = listArray[i + 1];
|
|
}
|
|
--listSize; // Decrement size
|
|
return it;
|
|
}
|
|
|
|
public void moveToStart() { curr = 0; } // Set to front
|
|
public void moveToEnd() { curr = listSize; } // Set at end
|
|
public void prev() { if (curr != 0) --curr; } // Back up
|
|
public void next() { if (curr < listSize) ++curr; }
|
|
|
|
/** @return List size */
|
|
public int length() { return listSize; }
|
|
|
|
/** @return Current position */
|
|
public int currPos() { return curr; }
|
|
|
|
/** Set current list position to "pos" */
|
|
public void moveToPos(int pos) {
|
|
assert(pos >= 0) && (pos <= listSize) : "Pos out of range";
|
|
curr = pos;
|
|
}
|
|
|
|
/** @return Current element */
|
|
public T getValue() {
|
|
assert (curr >= 0) && (curr < listSize) : "No current element";
|
|
return listArray[curr];
|
|
}
|
|
}
|