这是一个LinkedList
具有修改列表的方法。我必须做的一件事是:
将 LinkedList 类更改为与该类类似的通用集合
java.util.ArrayList
。
我不知道该怎么做?
import java.util.NoSuchElementException;
/**
A linked list is a sequence of nodes with efficient
element insertion and removal. This class
contains a subset of the methods of the standard
java.util.LinkedList class.
*/
public class LinkedList<E>
{
int currentSize =0;
private Node head;
private Node tail;
/**
Constructs an empty linked list.
*/
public LinkedList()
{
head = null;
tail = null;
}
/**
Adds an element to the front of the linked list.
@param element the element to add
*/
public void addFirst(Object element)
{
Node newNode = new Node();
newNode.data = element;
newNode.next = head;
currentSize++;
head = newNode;
tail = head;
while( tail.next != null)
{
tail = tail.next;
}
}
/**
Removes the head element in the linked list.
@return the removed element
*/
public Object removeFirst()
{
if (head == null)
throw new NoSuchElementException();
Object temp = head.data;
head = head.next;
currentSize--;
return temp;
}
/**
Returns the head element in the linked list.
@return the head element in the linked list
*/
public Object getFirst()
{
if (head == null)
throw new NoSuchElementException();
return head.data;
}
/**
Returns the element at a given position in the linked list.
@param index the element position
@return the element at the given position
*/
Object get(int index)
{
if (index < 0)
throw new NoSuchElementException();
Node current = head;
int i = 0;
while (current != null && i < index)
{
current = current.next;
i++;
}
if (current == null)
throw new NoSuchElementException();
return current.data;
}
/**
Computes the size of the linked list
@return the number of elements in the list
*/
public int size()
{
//to be completed for lab 7.1 #2
return currentSize; // rewrite this, just makes it compile
}
/**
Reverses all elements in a linked list.
*/
public void reverse()
{
//to be completed for lab 7.1 #1
if(head == null)
throw new NoSuchElementException();
tail = head;
Node current = head;
Node nextt= head.next;
Node nul= null;
while(nextt!= null)
{
current.next = nul;
nul = current;
current = nextt;
nextt = nextt.next;
}
head = current;
head.next = nul;
}
/**
Adds an element to the end of the linked list.
@param element the element to add
*/
public void add(Object element)
{
if(head==null)
addFirst(element);
else
{
Node newNode = new Node();
newNode.data = element;
newNode.next = null;
tail.next = newNode;
tail = newNode;
currentSize++;
}
//to be completed for lab 7.1 #3
}
/**
Returns an iterator for iterating through this list.
Allows the use of the iterator outside of this class.
@return an iterator for iterating through this list
*/
public ListIterator listIterator()
{
return new LinkedListIterator();
}
/**
Returns a string representation of this list in brackets
separated by commas.
@return a string of list elements.
*/
public String toString()
{
StringBuilder temp = new StringBuilder();
ListIterator it = listIterator();
while(it.hasNext()){
temp.append(it.next());
if(it.hasNext())
temp.append(", ");
}
return temp.toString();
}
private static class Node
{
private Object data;
private Node next;
}
private class LinkedListIterator implements ListIterator
{
private Node position;
private Node previous;
/**
Constructs an iterator that points to the front
of the linked list.
*/
public LinkedListIterator()
{
position = null;
previous = null;
}
/**
Moves the iterator past the next element.
@return the traversed element
*/
public Object next()
{
if (!hasNext())
throw new NoSuchElementException();
previous = position; // Remember for remove
if (position == null)
position = head;
else
position = position.next;
return position.data;
}
/**
Tests if there is an element after the iterator
position.
@return true if there is an element after the iterator
position
*/
public boolean hasNext()
{
if (position == null)
return head != null;
else
return position.next != null;
}
/**
Adds an element before the iterator position
and moves the iterator past the inserted element.
@param element the element to add
*/
public void add(Object element)
{
if (position == null)
{
addFirst(element);
position = head;
}
else
{
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
position.next = newNode;
position = newNode;
previous = position;
tail=head;
while( tail.next != null)
{
tail = tail.next;
}
currentSize++;
}
}
/**
Removes the last traversed element. This method may
only be called after a call to the next() method.
*/
public void remove()
{
if (previous == position)
throw new IllegalStateException();
if (position == head)
{
removeFirst();
}
else
{
previous.next = position.next;
tail=head;
while(tail.next != null)
{
tail = tail.next;
}
currentSize--;
}
position = previous;
}
/**
Sets the last traversed element to a different
data.
@param element the element to set
*/
public void set(Object element)
{
if (position == null)
throw new NoSuchElementException();
position.data = element;
}
}
}