Java 正在尝试将链接列表转换为循环链接列表
我在这项任务上工作了很长时间。我最终让测试人员打印列表中的内容和方法,但现在我需要将列表中的元素连接成一个圆圈,并在删除或添加元素时保持这种方式。我的书没有涉及任何关于循环链表的内容,我尝试将我在这里看到的一些示例的概念应用于循环链表,但没有成功 我将非常感激任何帮助 以下是我所拥有的:Java 正在尝试将链接列表转换为循环链接列表,java,data-structures,linked-list,Java,Data Structures,Linked List,我在这项任务上工作了很长时间。我最终让测试人员打印列表中的内容和方法,但现在我需要将列表中的元素连接成一个圆圈,并在删除或添加元素时保持这种方式。我的书没有涉及任何关于循环链表的内容,我尝试将我在这里看到的一些示例的概念应用于循环链表,但没有成功 我将非常感激任何帮助 以下是我所拥有的: import java.util.NoSuchElementException; /** A circular linked list. */ public class LinkedList {
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;
}
}
}
下面的代码将帮助您创建循环链表
class CircularLinkedList
{
class Node
{
int data;
Node next, prev;
public Node(int data)
{
this.data = data;
}
}
private Node head;
private int count;
public Node getHead()
{
return head;
}
public int getCount()
{
return count;
}
public boolean isEmpty()
{
return head==null;
}
// Add new node after the "head"
public void add(int data)
{
Node n = new Node(data);
if(isEmpty())
{
head = n;
n.next = head;
n.prev = head;
}
else
{
n.next = head.next;
n.prev = head;
head.next = n;
n.next.prev = n;
}
count++;
}
// Remove the node pointed by "head"
public void remove()
{
if(isEmpty())
return;
if(count==1)
head = null;
else
{
Node tmp = head;
tmp.prev.next = tmp.next;
tmp.next.prev = tmp.prev;
head = head.next;
tmp.next = tmp.prev = null;
}
count--;
}
public void print()
{
Node tmp = head.next;
while(tmp!=head)
{
System.out.println(tmp.data+" ");
tmp = tmp.next;
}
}
}
上面的代码将让您了解如何创建循环链表,如何添加、打印和删除节点。注意:因为这是您的任务,所以我没有向您提供确切的代码。您可以使用它,并以您需要的方式理解和调整它。通过这个,您将对循环链表有很好的了解
class CircularLinkedList
{
class Node
{
int data;
Node next, prev;
public Node(int data)
{
this.data = data;
}
}
private Node head;
private int count;
public Node getHead()
{
return head;
}
public int getCount()
{
return count;
}
public boolean isEmpty()
{
return head==null;
}
// Add new node after the "head"
public void add(int data)
{
Node n = new Node(data);
if(isEmpty())
{
head = n;
n.next = head;
n.prev = head;
}
else
{
n.next = head.next;
n.prev = head;
head.next = n;
n.next.prev = n;
}
count++;
}
// Remove the node pointed by "head"
public void remove()
{
if(isEmpty())
return;
if(count==1)
head = null;
else
{
Node tmp = head;
tmp.prev.next = tmp.next;
tmp.next.prev = tmp.prev;
head = head.next;
tmp.next = tmp.prev = null;
}
count--;
}
public void print()
{
Node tmp = head.next;
while(tmp!=head)
{
System.out.println(tmp.data+" ");
tmp = tmp.next;
}
}
}
如果你有任何问题。问我们。所以欢迎你:)根据你的类名:“LinkedListIterator”。您正在尝试使用迭代器还是循环(双)链表?它是一个内部类迭代器
root.next=//some_node
和root.prev=tail代码>和tail.next=root
和某个节点。next=tail代码>。我想那应该行。谢谢你的帮助!我想我不应该在这项作业中使用计数器。我应该从add方法调用迭代器内部类中的方法来正确放置新节点吗?我认为add方法应该是在列表末尾添加节点,并连接到列表中的第一项。@grisson:您能告诉我代码在指定行是如何中断的吗?与if
部分一样,我正在检查head
是否已初始化。如果仅为是,则此语句n.next=head.next
为executed@asifsid88:sry我忘记了当时我是如何测试它的,但我想我的意思是n.next=head.next导致问题。如果您在测试类中添加一些int元素[5,2,7],并使用head.next.data打印该元素,您将看到顺序为[5,7,2],正确的顺序应该是5->2->7->5->2。既然它是循环链表,当您在打印中使用while循环时,它不应该是一个无限循环吗?我同意您关于元素插入顺序的查询。为此,我们可以稍微调整代码。我们不需要在头之后附加新节点(在上面的代码中使用next),而可以在头之前附加新节点(即,我们可以使用prev)。通过此调整,[5,7,2,3]的插入顺序将为5->7->2->3。至于印刷部分。是的,它可以是一个无限循环,但它没有意义,因为我们无法知道内容。所以当第二次遇到元素时(跟踪头部),我们终止循环。我希望我能回答你的问题?如果有任何问题,请告诉我