gpt4 book ai didi

java - 没有导入的链接列表

转载 作者:行者123 更新时间:2023-12-02 00:43:13 25 4
gpt4 key购买 nike

我正在尝试创建一个链接列表,无论是双链接列表还是单链接列表,而不使用任何导入,它是类项目的一部分。但我不明白的是,我想如何在不使用 java.util 的情况下实际创建项目并将其添加到列表中。我有

public boolean insertItem( ItemType newItem)
{
if( p==0 || m==MAX_LENGTH)
{
head.elem = elem;
head.next = tail;
tail = head;
return false;
}
else
{
tail.next = new ListNode(); //adds new node to the end of the list
tail = tail.next;//set the tail pointer to that node
tail.elem = newItem; //set elem to be stored to the end node
m++;
return true;
}
}

所以我希望得到一些关于为什么这不起作用的意见。以及我如何实际创建此列表,我一直在使用数组 Object list[] = new list[MAX_LENGTH] 但我不确定这是否是正确的方法。

最佳答案

我会从以下内容开始:

class LinkedList
{
static class Node
{
ItemType item;
Node next;
Node prev; // if making a doubly linked list
}

private Node head;
private Node tail; // if making a doubly linked list

public boolean insertItem(ItemType item)
{
// make a new Node
// check for the head to be null...
// add the node to the end of the list, or make the head/tail point at it.
}
}

关于java - 没有导入的链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5724267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com