mirror of
https://github.com/Rushilwiz/APCS.git
synced 2025-04-05 13:00:20 -04:00
120 lines
2.9 KiB
Java
120 lines
2.9 KiB
Java
// Name: B6-24
|
|
// Date: 11/22/19
|
|
|
|
// implements some of the List and LinkedList interfaces:
|
|
// size(), add(i, o), remove(i); addFirst(o), addLast(o);
|
|
// This class also overrides toString().
|
|
// the list is zero-indexed.
|
|
// Uses DLNode.
|
|
|
|
public class DLL //DoubleLinkedList
|
|
{
|
|
private int size;
|
|
private DLNode head = new DLNode(); //dummy node--very useful--simplifies the code
|
|
|
|
public int size()
|
|
{
|
|
return size;
|
|
}
|
|
|
|
/* appends obj to end of list; increases size;
|
|
@return true */
|
|
public boolean add(Object obj)
|
|
{
|
|
addLast(obj);
|
|
return true;
|
|
}
|
|
|
|
/* inserts obj at position index. increments size. */
|
|
public void add(int index, Object obj) throws IndexOutOfBoundsException //this the way the real LinkedList is coded
|
|
{
|
|
if( index > size || index < 0 )
|
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
|
|
/* enter your code below */
|
|
if (size == 0)
|
|
addFirst(obj);
|
|
else if (index == size)
|
|
|
|
|
|
|
|
}
|
|
|
|
/* return obj at position index. */
|
|
public Object get(int index)
|
|
{
|
|
if(index >= size || index < 0)
|
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
|
|
/* enter your code below */
|
|
|
|
}
|
|
|
|
/* replaces obj at position index.
|
|
returns the obj that was replaced*/
|
|
public Object set(int index, Object obj)
|
|
{
|
|
if(index >= size || index < 0)
|
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
|
|
/* enter your code below */
|
|
|
|
}
|
|
|
|
/* removes the node from position index (zero-indexed). decrements size.
|
|
@return the object of the node that was removed. */
|
|
public Object remove(int index)
|
|
{
|
|
if(index >= size || index < 0)
|
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
|
|
/* enter your code below */
|
|
|
|
|
|
}
|
|
|
|
/* inserts obj at front of list, increases size */
|
|
public void addFirst(Object obj)
|
|
{
|
|
DLNode p = new DLNode
|
|
if (size == 0)
|
|
|
|
}
|
|
|
|
/* appends obj to end of list, increases size */
|
|
public void addLast(Object obj)
|
|
{
|
|
|
|
}
|
|
|
|
/* returns the first element in this list */
|
|
public Object getFirst()
|
|
{
|
|
|
|
}
|
|
|
|
/* returns the last element in this list */
|
|
public Object getLast()
|
|
{
|
|
|
|
}
|
|
|
|
/* returns and removes the first element in this list, or
|
|
returns null if the list is empty */
|
|
public Object removeFirst()
|
|
{
|
|
|
|
}
|
|
|
|
/* returns and removes the last element in this list, or
|
|
returns null if the list is empty */
|
|
public Object removeLast()
|
|
{
|
|
|
|
}
|
|
|
|
/* returns a String with the values in the list in a
|
|
friendly format, for example [Apple, Banana, Cucumber]
|
|
The values are enclosed in [], separated by one comma and one space.
|
|
*/
|
|
public String toString()
|
|
{
|
|
|
|
}
|
|
} |