删除链接列表中的元素

时间:2016-06-04 21:40:44

标签: java linked-list

如果我有包含一些元素的链表。例如, myLinked = {3,6 100,95,16,19,7 13,44}。我想删除8到20之间的任何元素。链表将删除7,16,19,13。有人能告诉我该怎么做。另外,我可以不使用排序这样做。

这是我的LinkedList类:

public class MyList
{
    static class Node
    {
        public Node(double item, Node next)
        {
            this.item = item;
            this.next = next;
        }

        public double item;
        public Node   next;
    }

    Node first;

    public MyList()
    {
        first = null;
    }

    public boolean isEmpty()
    {
        return first == null;
    }

    public void add (double item)
    {
        Node newfirst = new Node (item, first); 
        this.first = newfirst;
    }
}

3 个答案:

答案 0 :(得分:1)

首先,您的add方法会以相反的顺序将Nodes放入链接列表。

也就是说,如果你要对你的实现进行以下调用:

MyList list = new MyList();

list.add(1.0);
list.add(2.0);
list.add(3.0);

然后您的链接列表如下所示:

head-->3.0-->2.0-->1.0-->null

要解决此问题,您的add方法应如下所示:

public void add (double item)
{
    // create the new Node and have next point to null
    Node newNode = new Node (item, null);

    // the list was empty
    if (first == null)
    {
        this.first = newNode;
    }
    // loop until the end and attach the new Node
    else
    {
        Node next = first;
        Node prev = null;

        while (next != null)
        {
            prev = next;
            next = next.next;
        }
        prev.next = newNode;
    }
}

现在,如果运行以下代码:

MyList list = new MyList();

list.add(1.0);
list.add(2.0);
list.add(3.0);

您的链接列表如下所示:

head-->1.0-->2.0-->3.0-->null

现在我们有了一个正确的添加方法,我们可以实现删除删除边界内所有节点的方法(例如lowerupper

以下是删除实施:

public void removeBetween (double lower, double upper)
{
    Node current = first; // pointer to current Node
    Node prev = first; // pointer to previous Node
    Node removal = null; // pointer to Node we will remove

    // go through the entire linkedList
    while (current != null)
    {
        double data = current.item;

        // lower <= data <= upper
        // See note below on why use Double.compare()
        if (Double.compare(data, lower) >= 0 && Double.compare(data, upper) <= 0)
        {
            System.out.println("removing: " + data);
            removal = current;
        }

        // we found a Node to remove
        if (removal != null)
        {
            // special case it was the first
            if (removal == first)
            {
                // change first to next
                first = removal.next;
            }
            else
            {
                // move removals previous next to removal next
                prev.next = removal.next;
            }
        }

        // advance the pointers
        // only change previous if we didn't have a removal
        if (removal == null)
        {
            prev = current;
        }

        // move current along
        current = current.next;

        // detached the removal
        if (removal != null)
            removal.next = null;

        // reset the removal pointer
        removal = null;
    }
}

注意

答案 1 :(得分:0)

    int min = 8; //the min value you want to remove
    int max = 20; //the max value you want to remove

    for (int i = 0; i < myLinkedList.size(); i++) {
        int current = myLinkedList.get(i); //get the current element of your list
        if(current > min && current < max){ //check if current is between those numbers
            myLinkedList.remove(i);
            i--;
        }
    }

答案 2 :(得分:0)

试试这个:

cp page.html  newpage.html
while read var
do
    mv newpage.html newpage1.html
    sed -e "s@$var@<b>$var</b>@g" newpage1.html > newpage.html
    # optionally rm newpage1.html
done < code_list.txt