用Java实现链表
我试图用Java实现一个链表。 我使用的代码如下用Java实现链表,java,algorithm,Java,Algorithm,我试图用Java实现一个链表。 我使用的代码如下 public class LinkNode { private int data; public LinkNode next; public LinkNode (int data) { this.data = data; } public void setData(int data) { this.data = data; } public int getData() { return this.data; } public v
public class LinkNode
{
private int data;
public LinkNode next;
public LinkNode (int data)
{
this.data = data;
}
public void setData(int data)
{
this.data = data;
}
public int getData()
{
return this.data;
}
public void setNext(LinkNode next)
{
this.next = next;
}
public LinkNode getNext()
{
return this.next;
}
public static void main (String [] args)
{
LinkNode Node1 = new LinkNode(3);
LinkNode Head = Node1;
LinkNode Node2 = new LinkNode(4);
LinkNode Node3 = new LinkNode(5);
LinkNode Node4 = new LinkNode(6);
Head.setNext(Node1);
Node1.setNext(Node2);
Node2.setNext(Node3);
Node3.setNext(Node4);
int iCounter =0;
LinkNode currentNode= Head;
while (currentNode.getNext()!=null)
{
int data = currentNode.getData();
System.out.println(data);
currentNode = currentNode.getNext();
iCounter=iCounter+1;
}
System.out.println("No Of Nodes are"+iCounter);
}
}
这里的问题是没有节点3
代码不计算最后一个节点Node4的数量
结果如下
3
4
5
No Of Nodes are3
请让我知道代码中有什么问题。要使头部指向节点1,请写入
Head = Node1;
如果您写入Head=null,则意味着Head不指向任何节点,并且您将获得一个null指针异常,因为您随后尝试从不存在的节点获取下一个节点
第二个问题是当currentNode.getNext()
返回null
时退出循环。当您到达列表的最后一个节点时,getNext()
方法返回null;如果退出循环,则不会计算最后一个节点。将循环条件更改为:
while (currentNode != null)
请不要编辑问题来询问后续问题。编辑问题时不会通知任何人,因此您不会得到新答案。这也降低了网站对未来访问者的实用性。为您的每个问题发布一个新的“问题”。标题不应为空。相反,head中的数据应该为空,否则您无法找到下一个。以下是我多年前开发的单链表的实现:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author sergiizagriichuk
*/
public class Node<T> {
private T value;
private Node<T> next;
public Node(T value) {
this.value = value;
}
public static <T> Node<T> createLinkedListFromArray(T... array) {
if (checkIfArrayIsNullOrEmpty(array)) return new Node<T>(null);
Node<T> head = new Node<T>(array[0]);
createLinkedList(array, head);
return head;
}
private static <T> boolean checkIfArrayIsNullOrEmpty(T[] array) {
return array == null || array.length == 0;
}
private static <T> void createLinkedList(T[] array, Node<T> head) {
Node<T> node = head;
for (int index = 1; index < array.length; index++) {
T t = array[index];
node.setNext(new Node<T>(t));
node = node.getNext();
}
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return value != null && value.equals(node.value);
}
@Override
public int hashCode() {
return value.hashCode();
}
@Override
public String toString() {
List ret = createList();
return Arrays.toString(ret.toArray());
}
private List createList() {
Node root = this;
List ret = new ArrayList();
while (root != null) {
ret.add(root.getValue());
root = root.getNext();
}
return ret;
}
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.List;
/**
*@作者sergiizagriichuk
*/
公共类节点{
私人T值;
私有节点下一步;
公共节点(T值){
这个值=值;
}
公共静态节点createLinkedListFromArray(T…数组){
if(checkIfArrayIsNullOrEmpty(数组))返回新节点(null);
节点头=新节点(数组[0]);
createLinkedList(数组、头);
回流头;
}
私有静态布尔校验ArrayNullOrEmpty(T[]数组){
返回数组==null | | array.length==0;
}
私有静态void createLinkedList(T[]数组,节点头){
节点=头部;
for(int index=1;index
以及一些测试:
/**
* @author sergiizagriichuk
*/
public class NodeTest {
@Test
public void testCreateList() throws Exception {
Node<Integer> node = Node.createLinkedListFromArray(1, 2, 3, 4, 5);
Assert.assertEquals(Integer.valueOf(1), node.getValue());
Assert.assertEquals(Integer.valueOf(2), node.getNext().getValue());
}
@Test
public void testCreateListSize() throws Exception {
Integer[] values = new Integer[]{1, 2, 3, 4, 5};
int size = values.length - 1;
Node<Integer> node = Node.createLinkedListFromArray(values);
int count = 0;
while (node.getNext() != null) {
count++;
node = node.getNext();
}
Assert.assertEquals(size, count);
}
@Test
public void testNullNode() throws Exception {
Node<Integer> nullNode = new Node<Integer>(null);
assertNullNode(nullNode);
}
@Test
public void testNullArray() throws Exception {
Node<Integer> nullArrayNode = Node.createLinkedListFromArray();
assertNullNode(nullArrayNode);
}
@Test
public void testSetValue() throws Exception {
Node<Integer> node = new Node<Integer>(null);
assertNullNode(node);
node.setValue(1);
Assert.assertEquals(Integer.valueOf(1), node.getValue());
}
private void assertNullNode(Node<Integer> nullNode) {
Assert.assertNotNull(nullNode);
Assert.assertNull(nullNode.getValue());
}
}
/**
*@作者sergiizagriichuk
*/
公共类节点测试{
@试验
public void testCreateList()引发异常{
Node Node=Node.createLinkedListFromArray(1,2,3,4,5);
Assert.assertEquals(Integer.valueOf(1),node.getValue());
Assert.assertEquals(Integer.valueOf(2),node.getNext().getValue());
}
@试验
public void testCreateListSize()引发异常{
整数[]值=新整数[]{1,2,3,4,5};
int size=values.length-1;
Node Node=Node.createLinkedListFromArray(值);
整数计数=0;
while(node.getNext()!=null){
计数++;
node=node.getNext();
}
Assert.assertEquals(大小、计数);
}
@试验
public void testNullNode()引发异常{
Node nullNode=新节点(null);
assertNullNode(nullNode);
}
@试验
public void testNullArray()引发异常{
节点nullArrayNode=Node.createLinkedListFromArray();
assertNullNode(nullArrayNode);
}
@试验
public void testSetValue()引发异常{
节点=新节点(空);
assertNullNode(节点);
节点设置值(1);
Assert.assertEquals(Integer.valueOf(1),node.getValue());
}
私有void资产nullNode(节点nullNode){
Assert.assertNotNull(nullNode);
Assert.assertNull(nullNode.getValue());
}
}
尝试根据您的情况使用或重新开发检查此答案:[[1]:我不喜欢此问题。您不应该发布应用程序,而应该将其拆分,以将问题简化为您遇到的问题。您在用java创建LinkedList时遇到的问题,还是其他更具体的问题,而不是LinkedList特有的问题?