需要帮助在Java中实现双链接列表[最终版]
大家好,谢谢大家的帮助,到目前为止,我已经基本完成了这个项目 现在我将解释我的代码和我必须实现的特殊功能。 1.我的链表必须以特定数量的元素开始,您将在dll构造函数中看到这一点 2.将新值输入到创建的元素中的方法。 3.我有一个get方法来获取某个节点上的值。如果用户调用的索引值大于列表大小,则还会创建新节点 4.我还创建了一个insert方法,将元素插入到特定位置 我的节点类如下所示(很抱歉使用小写的类名): 我的DLL类(我只知道项目的名称):需要帮助在Java中实现双链接列表[最终版],java,doubly-linked-list,Java,Doubly Linked List,大家好,谢谢大家的帮助,到目前为止,我已经基本完成了这个项目 现在我将解释我的代码和我必须实现的特殊功能。 1.我的链表必须以特定数量的元素开始,您将在dll构造函数中看到这一点 2.将新值输入到创建的元素中的方法。 3.我有一个get方法来获取某个节点上的值。如果用户调用的索引值大于列表大小,则还会创建新节点 4.我还创建了一个insert方法,将元素插入到特定位置 我的节点类如下所示(很抱歉使用小写的类名): 我的DLL类(我只知道项目的名称): 公共类Betterray{ 私人国际单位大小
公共类Betterray{
私人国际单位大小;
专用节点头;
私有节点_尾;
公共竞技场(国际北){
_head=null;
_tail=null;
_尺寸=n;
如果(_head==null){
_头=新节点(0);
_尾巴=头;
}
对于(int i=1;i
现在,我更好地理解了您想要的内容(双链表数据结构的任意索引插入函数),下面是一些代码,可能会对您有所帮助:
public class BetterArray{
public node _head = null;
public node _tail = null;
public BetterArray(){
_head = _tail = new node();
}
public node insert(int val, int index) {
if (index < 0) {
throw new IllegalArgumentException("You must provide an index which is not negative");
}
node current = _head;
for (int i = 0; i < index; i++) {
if (current.next == null) {
current.next = new node();
current.next.prev = current;
_tail = current;
}
current = current.next;
}
current.set(val);
return current;
}
}
public class node {
private int _value;
private boolean _initialized;
public node(int v){
_value = v;
_initialized = true;
}
public node(){
_initialized = false;
}
public int get(){
if (!_initialized) {
throw new IllegalArgumentException("This node has not been set with any value");
}
return _value;
}
public void set(int v){
_value = v;
_initialized = true;
}
public node next = null;
public node prev = null;
}
正如您所知,还实现了标准Java数据结构。您可能想看看。基本上,我所做的与添加节点相同,直到其大小阻止它获得IllegalArgumentException
,然后执行设置
原职:
链表和数组是两种完全不同的数据结构。与其专注于实现,不如考虑一下你想对数据结构做什么。需要随机访问数据吗?一个(双重)链表需要O(n)是时候为读取和写入找到正确的元素了;您已经在insert中实现的逻辑中看到了这一点。对于数组,随机访问是一个O(1)常量时间操作。如果您想编写一个数据结构,如列表,以进行随机访问,请尝试使用new node[n]
,并将整个数组对象私有地保存在内存中
如果更大,标准做法是创建一个两倍于所需索引大小的新数组,并将所有旧数据复制到新数组中。这是一个O(n)操作,而链表开头或结尾的插入时间是O(1)常量时间
有中间立场吗?实际上有!如果你实现一个平衡二叉树,你可以得到O(lg(n))insert和O(lg(n))访问你的节点。我建议你复习一下你的数据结构。试着用铅笔和纸把它写出来,直到你理解了结构的感觉,然后把它写进代码中。除非你对Java感到满意或者你的类需要它,否则就坚持你先学的任何语言(我之所以选择C,是因为你的写作风格和你称之为“指针”的方式)。否则,您将同时学习两件事,这通常比一次学习一件事更难。创建包含N个无值元素的链表毫无意义。我建议您不要这样做。如果必须这样做,只需调用add(null)
N次,因为您必须实现add()
无论如何。@andreas我相信这就是insert方法的目的,或者我在这里的逻辑是错误的。请注意,如果类名node
以大写字母开头,则更加清晰,因此node
。insert方法是用一个值创建一个新节点。@martijnn2008和BetterArray
是doub的糟糕名称ly链表实现。@Andreas您能就我的问题而不是我的命名约定留下评论吗?谢谢。这如何回答我需要实现双链表的问题?为什么?您的列表对象需要什么操作?我只是指出数据结构中存在差异res不能帮助回答如何实现问题中所问的函数。它是一个对象、一个数据结构,而不是一个函数。如果有的话,它是一个函数/操作的集合。您需要指定此链表数据结构需要哪些操作,否则您将无法完全实现它。它还将e了解这些操作所需的运行时间很有帮助:您可以扫描整个链表以查找内容吗?原始帖子中隐藏的问题..“我的insert方法编译得很好,但如果有人能帮我检查一下,我想确保我正确设置了所有指针”。因此,问题中尝试实现的双链表对象的insert
函数未按预期工作。无需提及数组或树……这是我的观点
public class BetterArray{
private int _size;
private node _head;
private node _tail;
public BetterArray(int n){
_head = null;
_tail = null;
_size = n;
if(_head == null){
_head = new node(0);
_tail = _head;
}
for(int i = 1; i < n; i++){
node current = _head;
for(int j = 1; j < i; j++){
current = current.next;
}
node newNode = current.next;
current.next = new node(0);
current.next.next = newNode;
current.next.prev = current;
_tail = current.next;
}
}
public BetterArray(){
}
public int get(int index){
int value = 0;
node temp = _head;
if(index < _size){
for(int loc = 0; loc < index; loc++){
temp = temp.next;
}
value = temp.get();
}
else{
for(int i = _size; i <= index; i++){
node current = temp;
for(int j = _size; j < i; j++){
current = current.next;
}
node newNode = current.next;
current.next = new node(0);
_size++;
current.next.next = newNode;
current.next.prev = current;
_tail = current.next;
}
}
return value;
}
public void put(int value, int index){
node temp = _head;
if(index < _size){
for(int loc = 0; loc < index; loc++){
temp = temp.next;
}
temp.set(value);
}
else{
for(int i = _size; i < index; i++){
node current = temp;
for(int j = _size; j < i; j++){
current = current.next;
}
node newNode = current.next;
current.next = new node(value);
_size++;
current.next.next = newNode;
current.next.prev = current;
_tail = current.next;
}
}
}
public void insert(int value,int index){
node current = _head;
for(int loc = 0; loc < index - 1; loc++){
current = current.next;
}
node temp = current.next;
current.next = new node(value);
_size++;
current.next.next = temp;
current.next.prev = current;
_tail = current.next;
}
}
public void delete(int index){
node pre = _head;
for(int loc = 0; loc < index; loc++){
pre = pre.next;
}
node current = pre.next;
pre.next = current.next;
_size--;
}
public int getSize(){
return _size;
}
public class BetterArray{
public node _head = null;
public node _tail = null;
public BetterArray(){
_head = _tail = new node();
}
public node insert(int val, int index) {
if (index < 0) {
throw new IllegalArgumentException("You must provide an index which is not negative");
}
node current = _head;
for (int i = 0; i < index; i++) {
if (current.next == null) {
current.next = new node();
current.next.prev = current;
_tail = current;
}
current = current.next;
}
current.set(val);
return current;
}
}
public class node {
private int _value;
private boolean _initialized;
public node(int v){
_value = v;
_initialized = true;
}
public node(){
_initialized = false;
}
public int get(){
if (!_initialized) {
throw new IllegalArgumentException("This node has not been set with any value");
}
return _value;
}
public void set(int v){
_value = v;
_initialized = true;
}
public node next = null;
public node prev = null;
}
BetterArray whatever = new BetterArray();
whatever.insert(5,3);
System.out.println(whatever._head.next.next.next.get());