简体   繁体   English

Java中的数据结构通过操作删除节点后的所有节点

[英]Data structure in Java with operation remove all nodes after a node

I'm looking for a (predefined)data struture in Java, which will remove all the elements after a node. 我正在寻找Java中的(预定义)数据结构,该结构将删除节点后的所有元素。 A sample representaion given below. 下面给出了一个示例表示。

eg: 例如:

Before removal 移除前

head
 ┕>1 -> 2 -> 3 -> 4 -> 5 -> 6 ->7

After removal 去除后

removeAllFrom(5) removeAllFrom(5)

head
┕>1 -> 2 -> 3 -> 4

I checked a lot of java DS, but didn't find a perfect one in java. 我检查了很多Java DS,但没有在Java中找到理想的DS。

(1 . Preferred a data structure which is in java.util. (1。首选java.util中的数据结构。

2 . 2。 Insertion at head and iteration are the other operations which i'm using) 在头插入和迭代是我正在使用的其他操作)

Thanks for the help :) 谢谢您的帮助 :)


Edit - 1 编辑-1

After finding the element specified for removal(in the sample its 5), we just need to remove the links between the next node. 找到指定要删除的元素(在示例中为5)之后,我们只需要删除下一个节点之间的链接即可。

I checked the implementation of the answers given, but in both the cases it is removing each node separately. 我检查了给出答案的实现,但是在两种情况下,它都是分别删除每个节点。 Just curious to know any other way to do it. 只是想知道其他任何方法。 :) :)

public void clear() {
    removeRange(0, size());
}

protected void removeRange(int fromIndex, int toIndex) {
    ListIterator<E> it = listIterator(fromIndex);
    for (int i=0, n=toIndex-fromIndex; i<n; i++) {
        it.next();
        it.remove();
    }
}

Well, java.util.LinkedList implements the List interface, which has a subList() method. 好吧, java.util.LinkedList实现了List接口,该接口具有subList()方法。 Using that method, you can obtain a sub-list of the tail of the original list, and by clearing it, truncate the original List: 使用该方法,可以获得原始列表尾部的子列表,然后通过清除子列表来截断原始列表:

list.subList(firstIndexToRemove,list.size()).clear();

From the Javadoc: 从Javadoc:

List java.util.List.subList(int fromIndex, int toIndex) 列出java.util.List.subList(int fromIndex,int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. 返回此列表中指定的fromIndex(包括)和toIndex(不包括)之间的视图。 (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. (如果fromIndex和toIndex相等,则返回列表为空。)此列表支持返回的列表,因此返回列表中的非结构性更改会反映在此列表中,反之亦然。 The returned list supports all of the optional list operations supported by this list. 返回的列表支持此列表支持的所有可选列表操作。

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). 此方法消除了对显式范围操作(数组通常存在的那种范围)的需要。 Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. 通过传递subList视图而不是整个列表,可以将期望列表的任何操作用作范围操作。 For example, the following idiom removes a range of elements from a list: 例如,以下成语从列表中删除了一系列元素:

list.subList(from, to).clear(); list.subList(from,to).clear();

This requires that you know the index of the node from which you want to remove all the elements. 这要求您知道要从中删除所有元素的节点的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM