0
그래서 저는 Java에서 링크드리스트를 처음 접했습니다. 연결된 목록에 항목을 추가하는 방법을 완료해야합니다. 아무도 내가 if 문을 완료하는 데 도움이 될 수 있도록 메서드가 올바르게 작동합니까? 도와 주셔서 감사합니다.이 방법을 사용하여 Java의 연결된 목록에 값을 추가하는 방법은 무엇입니까?
void insert(int item)
{
//find location to insert
Node current = head;
Node prev = null;
boolean located = false;
while(!located && current != null)//correct location found
{
//I need to add a condition to the if statement below. What is the condition
if(item
current = current.link;//assign the next node from head to current node
}
//create new node and
//refer to the correct location in list
Node newNode = new Node(item, current);
//set link to refer to new node
if (current == head)
{
head = newNode;//new node to head
}
else
{
//place new node after previous LINK reference not the node itself
prev.link = newNode;
}
}
나는 "당신"의 도움을 의미했습니다. – Jacob
그런 다음 편집 할 수 있습니다. –