Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在固定时间内将节点插入链表?_Java_Linked List - Fatal编程技术网

Java 在固定时间内将节点插入链表?

Java 在固定时间内将节点插入链表?,java,linked-list,Java,Linked List,我正在做一个作业,它告诉我假设我有一个带有头和尾节点的单链表。它希望我在位置p之前插入一个项目y。有人能看一下我的代码,告诉我我是否在正确的轨道上吗?如果没有,你能给我一些建议吗(没有双关语) 我认为我可能是错的,因为我根本没有使用header和tail节点,即使在问题的描述中特别提到了它们。我正在考虑编写一个while循环来遍历列表,直到它找到p并以这种方式解决问题,但这不是常数时间,不是吗?如果你被算法卡住了,就把它写下来: // First we have a pointer to a n

我正在做一个作业,它告诉我假设我有一个带有头和尾节点的单链表。它希望我在位置p之前插入一个项目y。有人能看一下我的代码,告诉我我是否在正确的轨道上吗?如果没有,你能给我一些建议吗(没有双关语)


我认为我可能是错的,因为我根本没有使用header和tail节点,即使在问题的描述中特别提到了它们。我正在考虑编写一个while循环来遍历列表,直到它找到p并以这种方式解决问题,但这不是常数时间,不是吗?

如果你被算法卡住了,就把它写下来:

// First we have a pointer to a node containing element (elm) 
// with possible a next element.
// Graphically drawn as:
// p -> [elm] -> ???

tmp = new Node();
// A new node is created. Variable tmp points to the new node which 
// currently has no value.
// p   -> [elm] -> ???
// tmp -> [?]

tmp.element = p.element;

// The new node now has the same element as the original.
// p   -> [elm] -> ???
// tmp -> [elm]

tmp.next = p.next;

// The new node now has the same next node as the original.
// p   -> [elm] -> ???
// tmp -> [elm] -> ???

p.element = y;

// The original node now contains the element y.
// p   -> [y] -> ???
// tmp -> [elm] -> ???

p.next = tmp;

// The new node is now the next node from the following.
// p   -> [y] -> [elm] -> ???
// tmp -> [elm] -> ???
你有必要的效果,但它可以更有效,我打赌你现在可以找到你自己

更清楚的是,可以这样写:

tmp = new Node();
tmp.element = y;
tmp.next = p;
p = tmp;
如果p是不可变的,这当然不起作用。但是,如果p==NULL,您的算法将失败


但我想说的是,如果你对算法有问题,就把效果写出来。特别是对于树和链表,你需要确保所有指针都指向正确的方向,否则你会弄得一团糟。

如果你被算法卡住了,就把它写下来:

// First we have a pointer to a node containing element (elm) 
// with possible a next element.
// Graphically drawn as:
// p -> [elm] -> ???

tmp = new Node();
// A new node is created. Variable tmp points to the new node which 
// currently has no value.
// p   -> [elm] -> ???
// tmp -> [?]

tmp.element = p.element;

// The new node now has the same element as the original.
// p   -> [elm] -> ???
// tmp -> [elm]

tmp.next = p.next;

// The new node now has the same next node as the original.
// p   -> [elm] -> ???
// tmp -> [elm] -> ???

p.element = y;

// The original node now contains the element y.
// p   -> [y] -> ???
// tmp -> [elm] -> ???

p.next = tmp;

// The new node is now the next node from the following.
// p   -> [y] -> [elm] -> ???
// tmp -> [elm] -> ???
你有必要的效果,但它可以更有效,我打赌你现在可以找到你自己

更清楚的是,可以这样写:

tmp = new Node();
tmp.element = y;
tmp.next = p;
p = tmp;
如果p是不可变的,这当然不起作用。但是,如果p==NULL,您的算法将失败


但我想说的是,如果你对算法有问题,就把效果写出来。特别是对于树和链表,您需要确保所有指针都指向正确的方向,否则会造成混乱。

提示:只有在位置n=0或列表头时,插入链表才是恒定的。否则,最坏情况复杂度为O(n)。这并不是说你不能创建一个合理有效的算法,但它至少会有线性复杂度。

提示:只有当位置n=0或列表头时,插入到链接列表中才是恒定的。否则,最坏情况复杂度为O(n)。这并不是说你不能创建一个合理有效的算法,但它总是至少具有线性复杂性。

你没有做的是将插入y之前p的元素链接到y。因此,当y插入p之前时,现在没有人指向y(至少在您显示的代码中没有)


如果您知道必须在其中插入y的元素的位置,则只能以恒定时间插入。如果必须搜索该位置,则在单个链接列表中永远不能有固定时间插入。

您没有做的是将插入y之前的p元素链接到y。因此,当y插入p之前时,现在没有人指向y(至少在您显示的代码中没有)


如果您知道必须在其中插入y的元素的位置,则只能以恒定时间插入。如果您必须搜索该位置,那么您永远无法在单个链接列表中进行固定时间插入。

如何使用已有的代码?LinkedHashMap、LinkedList、LinkedHashSet。您也可以查看代码并从中学习。

如何使用已有的代码?LinkedHashMap、LinkedList、LinkedHashSet。您还可以查看代码并从中学习。

创建节点ptr
create a node ptr
ptr->info = item //item is the element to be inserted...
ptr->next = NULL
if (start == NULL) //insertion at the end...
    start = ptr
else
    temp = ptr
    while (temp->next != NULL)
        temp = temp->next
    end while 
end if
if (start == NULL) //insertion at the beginning...
    start = ptr
else
    temp = start
    ptr->info = item
    ptr->next = start
    start = ptr
end if
temp = start //insertion at specified location...
for (i = 1; i < pos-1; i++)
    if (start == NULL)
        start = ptr
    else
        t = temp
        temp = temp->next
    end if
end for
t->next = ptr->next
t->next = ptr
ptr->info=item//item是要插入的元素。。。 ptr->next=NULL 如果(start==NULL)//在末尾插入。。。 开始=ptr 其他的 温度=ptr while(临时->下一步!=NULL) 温度=温度->下一步 结束时 如果结束 如果(start==NULL)//在开头插入。。。 开始=ptr 其他的 温度=开始 ptr->info=物料 ptr->next=开始 开始=ptr 如果结束 temp=开始//在指定位置插入。。。 对于(i=1;i下一步 如果结束 结束 t->next=ptr->next t->next=ptr
创建节点ptr
ptr->info=item//item是要插入的元素。。。
ptr->next=NULL
如果(start==NULL)//在末尾插入。。。
开始=ptr
其他的
温度=ptr
while(临时->下一步!=NULL)
温度=温度->下一步
结束时
如果结束
如果(start==NULL)//在开头插入。。。
开始=ptr
其他的
温度=开始
ptr->info=物料
ptr->next=开始
开始=ptr
如果结束
temp=开始//在指定位置插入。。。
对于(i=1;i下一步
如果结束
结束
t->next=ptr->next
t->next=ptr

问题中给出header和tail节点的原因是,如果您创建的替换节点恰好成为header或tail,则更新header和tail引用。换句话说,给定的前一个节点是头节点或尾节点。

问题中给出头节点和尾节点的原因是,如果您创建的替换节点恰好成为头节点或尾节点,则更新头节点和尾节点引用。换句话说,给定的前一个节点是头节点或尾节点。

在单链接列表中,仅将一个节点添加到列表的开头或仅使用一个节点创建列表需要O(1)。或者,正如他们提供的那样,在列表末尾插入节点也需要O(1)


每隔一次插入操作都需要O(n)。

在单链接列表中,仅在列表开头添加一个节点或创建一个只有一个节点的列表需要O(1)。或者,正如他们提供的那样,在列表末尾插入节点也需要O(1)

每隔一次插入操作都需要O(n)。

等待,所以我想我