我为这个任务工作了很长时间。我终于让测试人员打印列表中的内容和方法工作,但现在我需要将列表中的元素连接成一个圆圈,并在删除或添加元素时保持这种方式。我的书没有涉及任何关于循环链表的内容,我试图将我在此处看到的一些示例的概念应用于循环链表,但没有任何成功。
我将不胜感激任何帮助。
这是我所拥有的:
import java.util.NoSuchElementException;
/**
A circular linked list.
*/
public class LinkedList
{
private Node last;
// Don't add other instance fields
/**
Constructs an empty linked list.
*/
public LinkedList()
{
last = null;
}
/**
Returns the first element in the linked list.
@return the first element in the linked list
*/
public Object getFirst()
{
//. . .
if (last == null)
throw new NoSuchElementException();
return last.data;
}
/**
Removes the first element in the linked list.
@return the removed element
*/
public Object removeFirst()
{
//. . .
if (last == null)
throw new NoSuchElementException();
Object element = last.data;
last = last.next;
return element;
}
/**
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 = last;
last = newNode;
}
/**
Adds an element to the end of the linked list.
@param element the element to add
*/
public void add(Object element)
{
//. . .
if (last == null)
{
addFirst(element);
//position = last;//had to comment out
}
else
{
Node newNode = new Node();
newNode.data = element;
newNode.next = last.next;
last.next = newNode;
last = newNode;
}
}
/**
Returns an iterator for iterating through this list.
@return an iterator for iterating through this list
*/
public ListIterator listIterator()
{
return new LinkedListIterator();
}
private class Node
{
public Object data;
public 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; //remeber for remove
if (position == null)
position = last;
else
position = position.next;
return position.data; //correct line
}
/**
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 last != 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 = last;
}
}
/**
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 == last)
{
removeFirst();
}
else
{
previous.next = position.next;
}
position = previous;
}
/**
Sets the last traversed element to a different
value.
@param element the element to set
*/
public void set(Object element)
{
if (position == null)
throw new NoSuchElementException();
position.data = element;
}
}
}