Java方法实现中的链表

Java方法实现中的链表,java,linked-list,Java,Linked List,现在我正在准备编码面试,我有一个关于Java中链表的问题。你能告诉我一些可靠的来源,我可以从中学习和实践基本的链表方法。我喜欢这个:www.cs.cmu.edu/~adamchik/15-121/touchts/Linked%20Lists/code/LinkedList.java,但我对一些方法实现感到困惑。例如,方法E get(int pos)返回的不是节点,而是位置pos处节点中包含的数据E。而在这里,方法节点(int index)返回该位置的节点(而不是其中包含的数据)。我应该遵循哪个实

现在我正在准备编码面试,我有一个关于Java中链表的问题。你能告诉我一些可靠的来源,我可以从中学习和实践基本的链表方法。我喜欢这个:www.cs.cmu.edu/~adamchik/15-121/touchts/Linked%20Lists/code/LinkedList.java,但我对一些方法实现感到困惑。例如,方法E get(int pos)返回的不是节点,而是位置pos处节点中包含的数据E。而在这里,方法节点(int index)返回该位置的节点(而不是其中包含的数据)。我应该遵循哪个实现

数据结构是一门非常概念化和基于上下文的学科。每个数据结构的各种实现都基于数据结构的要求和范围。
人们甚至可以说,
Collection-API
LinkedList
实现存在缺陷,因为如果多个线程同时工作,那么它就不能正常工作。然后需要从
Collections
类中创建一个
synchronizedList
,或者至少需要使用一个能够与多个线程协同工作的实现

遵循最起码的可行惯例,因为面试官不会只问你一个
LinkedList
实现。面试官想知道的是你的概念和编码技能是否达到了一定的标准

想想你能用
链表做什么。为此,您必须考虑您实际考虑的是什么类型的
LinkedList
,因为有许多不同类型的
LinkedList
,如
SingleLinkedList
DoublyLinkedList
SkipList
,等等

考虑到
单个链接列表
,您的
链接列表
实现应至少具有以下方法:
add()
remove()
contains()
clear()
size()

以下是我对
SingleLinkedList
的实现:

import java.util.Iterator;
import java.util.StringJoiner;

public class LinkedList<T> implements Iterable<T>
{
  private Node head;
  private Node tail;
  private int size;

  private class Node
  {
    private T value;
    private Node next;

    public Node(T value)
    {
      this.value = value;
    }
  }

  public void add(T value)
  {
    Node node = new Node(value);

    if (head == null)
    {
      head = node;
    }
    else
    {
      tail.next = node;
    }

    tail = node;
    ++size;
  }

  public boolean remove(T value)
  {
    Node previous = null;
    Node current = head;

    while (head != null)
    {
      if (current.value.equals(value))
      {
        if (previous != null)
        {
          previous.next = current.next;

          if (current.next == null)
          {
            tail = previous;
          }
        }
        else
        {
          head = current.next;

          if (head == null)
          {
            tail = null;
          }
        }

        --size;
        return true;
      }

      previous = current;
      current = current.next;
    }

    return false;
  }

  public boolean contains(T value)
  {
    Node current = head;

    while (current != null)
    {
      if (current.value.equals(value))
      {
        return true;
      }

      current = current.next;
    }

    return false;
  }

  public void clear()
  {
    head = null;
    tail = null;
    size = 0;
  }

  public int size()
  {
    return size;
  }

  @Override
  public Iterator<T> iterator()
  {
    return new Iterator<T>()
    {
      private Node current = head;

      @Override
      public boolean hasNext()
      {
        return current != null;
      }

      @Override
      public T next()
      {
        Node next = current;
        current = current.next;
        return next.value;
      }
    };
  }

  @Override
  public String toString()
  {
    StringJoiner joiner = new StringJoiner(", ");

    for (T value : this)
    {
      joiner.add(value.toString());
    }

    return joiner.toString();
  }
}
import java.util.Iterator;
导入java.util.StringJoiner;
公共类LinkedList实现了Iterable
{
专用节点头;
私有节点尾部;
私有整数大小;
私有类节点
{
私人T值;
私有节点下一步;
公共节点(T值)
{
这个值=值;
}
}
公共无效添加(T值)
{
节点=新节点(值);
if(head==null)
{
头部=节点;
}
其他的
{
tail.next=节点;
}
尾=节点;
++大小;
}
公共布尔删除(T值)
{
Node-previous=null;
节点电流=头;
while(head!=null)
{
如果(当前值等于(值))
{
如果(上一个!=null)
{
previous.next=current.next;
if(current.next==null)
{
尾=前一个;
}
}
其他的
{
头=当前。下一个;
if(head==null)
{
tail=null;
}
}
--大小;
返回true;
}
先前=当前;
当前=当前。下一步;
}
返回false;
}
公共布尔包含(T值)
{
节点电流=头;
while(当前!=null)
{
如果(当前值等于(值))
{
返回true;
}
当前=当前。下一步;
}
返回false;
}
公共空间清除()
{
head=null;
tail=null;
尺寸=0;
}
公共整数大小()
{
返回大小;
}
@凌驾
公共迭代器迭代器()
{
返回新的迭代器()
{
私有节点电流=头;
@凌驾
公共布尔hasNext()
{
返回电流!=null;
}
@凌驾
公共交通工具
{
节点下一个=当前;
当前=当前。下一步;
返回next.value;
}
};
}
@凌驾
公共字符串toString()
{
细木工=新细木工(“,”);
for(T值:此)
{
add(value.toString());
}
返回joiner.toString();
}
}
如您所见,我的实现可能不同于您在其他地方找到的实现。只要数据结构的概念没有发生根本性的改变,只要它能够正确地使用其定义的接口,微小的差异就不成问题


对于数据结构之类的学科,您必须自己思考,并根据您的需求,使用或实现适合您需要的数据结构。就访谈而言,实现一个最低可行的数据结构是您需要展示的全部内容,也是您所需要的全部内容。只要确保这种最小可行的数据结构在其上下文中没有问题。

一个是Java的LinkedList,另一个是有人为了学术目的而实现的。我理解其中的区别,但我在面试时应该遵循哪种惯例?面试官期望哪种实现?