简体   繁体   English

双向链表将项目移动到结束 java

[英]Doubly Linked List moving item to end java

I found this code for adding an item to the front of the linked list, but since I have a last node, it doesn't work quite right, so I changed it a tiny bit:我找到了将项目添加到链表前面的这段代码,但是由于我有一个最后一个节点,所以它不能正常工作,所以我对其进行了一点更改:

    public void moveToFront(String node) {

    DoubleNode previous = first;
    temp = first;

    while (temp != null) {
        if (node.equals(temp.item)) {
            //Found the item
            previous.next = temp.next;
            temp.next = first;
            first = temp;

            if (last.next != null) {
                last = last.prev;
                last.prev = previous;
            }

            return;
        }
        previous = temp;
        temp = temp.next;
    }

The if (last.next != null) is asking if the original last was moved, and checking if the new last has the right links. if (last.next != null)询问原始最后一个是否已移动,并检查新最后一个是否具有正确的链接。 Now I think it works properly for my code.现在我认为它适用于我的代码。

I'd like to implement this code, but for adding an item to the end.我想实现此代码,但要在末尾添加一个项目。 However, last just isn't right now.但是,最后一次不是现在。 When calling last.prev it only gets the one item behind it, but last.prev.prev to infinity is the same item.调用last.prev时,它只获取它后面的一项,但last.prev.prev到 infinity 是同一项。

My idea was instead of working from first like in moveToFront() , I work from last, and step through each node backwards, but obviously that doesn't work when last doesn't work anymore.我的想法不是像在moveToFront()中那样从头开始工作,而是从最后开始工作,然后向后逐步遍历每个节点,但显然,当最后一个不再起作用时,这不起作用。

public void moveToEnd(String node) {

DoubleNode previous = last;
    temp = last;
    System.out.println("last = " + last.prev.item);
    System.out.println("temp = " + temp.item);

    while (!temp.item.equals(first.item)) {

        if(node.equals(temp.item)){
            System.out.println("previous.prev = " + previous.prev.item);
        }

        previous = temp;
        temp = temp.prev;

        System.out.println("temp.prev = " + temp.prev.prev.prev.prev.prev.prev.prev.prev.prev.prev.prev.item);
    }

Here's how I implement my linked list:这是我实现链接列表的方式:

public class LinkedListDeque {

public DoubleNode first = new DoubleNode(null);
public DoubleNode last = new DoubleNode(null);
public DoubleNode temp;
public int N;

LinkedListDeque() {
    first.next = last;
    last.prev = first;
}

private class DoubleNode {

    String item;
    int counter = 0;
    DoubleNode next;
    DoubleNode prev;

    DoubleNode(String i) {
        this.item = i;
    }
}

I found this example of a complete doubly linked list.我找到了一个完整的双向链表的这个例子。 It does not have an add to front method, but it is adding to the back of the linked list each time.它没有添加到前面的方法,但每次都添加到链表的后面。 Hopefully, it will help and give you a better idea of how this data structure is supposed to work and function.希望它能帮助您并让您更好地了解此数据结构应该如何工作以及 function。 I would definitely test it first as it states in the readme for this GitHub that none of the code has been tested.我肯定会首先测试它,因为它在 GitHub 的自述文件中指出,没有任何代码经过测试。 Sorce 索斯

/*******************************************************
 *  DoublyLinkedList.java
 *  Created by Stephen Hall on 9/22/17.
 *  Copyright (c) 2017 Stephen Hall. All rights reserved.
 *  A Linked List implementation in Java
 ********************************************************/
package Lists.Doubly_Linked_List;

/**
 * Doubly linked list class
 * @param <T> Generic type
 */
public class DoublyLinkedList<T extends Comparable<T>> {
    /**
     * Node class for singly linked list
     */
    public class Node{
        /**
         * private Members
         */
        private T data;
        private Node next;
        private Node previous;

        /**
         * Node Class Constructor
         * @param data Data to be held in the Node
         */
        public Node(T data){
            this.data = data;
            next = previous = null;
        }
    }

    /**
     * Private Members
     */
    private Node head;
    private Node tail;
    private int count;

    /**
     * Linked List Constructor
     */
    public DoublyLinkedList(){
        head = tail = null;
        count = 0;
    }

    /**
     * Adds a new node into the list with the given data
     * @param data Data to add into the list
     * @return Node added into the list
     */
    public Node add(T data){
        // No data to insert into list
        if (data != null) {
            Node node = new Node(data);
            // The Linked list is empty
            if (head == null) {
                head = node;
                tail = head;
                count++;
                return node;
            }
            // Add to the end of the list
            tail.next = node;
            node.previous = tail;
            tail = node;
            count++;
            return node;
        }
        return null;
    }

    /**
     * Removes the first node in the list matching the data
     * @param data Data to remove from the list
     * @return Node removed from the list
     */
    public Node remove(T data){
        // List is empty or no data to remove
        if (head == null || data == null)
            return null;

        Node tmp = head;
        // The data to remove what found in the first Node in the list
        if(equalTo(tmp.data, data)) {
            head = head.next;
            count--;
            return tmp;
        }

        // Try to find the node in the list
        while (tmp.next != null) {
            // Node was found, Remove it from the list
            if (equalTo(tmp.next.data, data)) {
                if(tmp.next == tail){
                    tail = tmp;
                    tmp = tmp.next;
                    tail.next = null;
                    count--;
                    return tmp;
                }
                else {
                    Node node = tmp.next;
                    tmp.next = tmp.next.next;
                    tmp.next.next.previous = tmp;
                    node.next = node.previous = null;
                    count--;
                    return node;
                }
            }
            tmp = tmp.next;
        }
        // The data was not found in the list
        return null;
    }

    /**
     * Gets the first node that has the given data
     * @param data Data to find in the list
     * @return Node First node with matching data or null if no node was found
     */
    public Node find(T data){
        // No list or data to find
        if (head == null || data == null)
            return null;

        Node tmp = head;
        // Try to find the data in the list
        while(tmp != null) {
            // Data was found
            if (equalTo(tmp.data, data))
                return tmp;
            tmp = tmp.next;
        }
        // Data was not found in the list
        return null;
    }

    /**
     * Gets the node at the given index
     * @param index Index of the Node to get
     * @return Node at passed in index
     */
    public Node indexAt(int index){
        //Index was negative or larger then the amount of Nodes in the list
        if (index < 0 || index > size())
            return null;

        Node tmp = head;
        // Move to index
        for (int i = 0; i < index; i++)
            tmp = tmp.next;
        // return the node at the index position
        return tmp;
    }

    /**
     * Gets the current count of the array
     * @return Number of items in the array
     */
    public int size(){
        return count;
    }

    /**
     * Determines if a is equal to b
     * @param a: generic type to test
     * @param b: generic type to test
     * @return boolean: true|false
     */
    private boolean equalTo(T a, T b) {
        return a.compareTo(b) == 0;
    }
}

There is a problem with your moveToFront() method.您的moveToFront()方法有问题。 It does not work for if node == first .它不适用于 if node == first If the node that needs to be moved is the first node, you end up setting first.next = first which is incorrect.如果需要移动的节点是第一个节点,则最终设置first.next = first这是不正确的。 The below code should work下面的代码应该可以工作

    public void moveToFront(DoubleNode node) {

    DoubleNode previous = first;
    temp = first;

    while (temp != null && node != first) {
        if (node.equals(temp.item)) {
            //Found the item
            previous.next = temp.next;
            temp.next = first;
            first = temp;

            if (last.next != null) {
                last = last.prev;
                last.prev = previous;
            }

            return;
        }
        previous = temp;
        temp = temp.next;
    }

Now coming to moveToLast() the following code should work现在来到moveToLast()以下代码应该可以工作

public void moveToLast(DoubleNode node) {
    DoubleNode temp = first;
    DoubleNode prev = new DoubleNode(null);   //dummy sentinel node

    while (temp != null && node != last) {
         if (temp == node) {
              if (temp == first) first = temp.next;

              prev.next = temp.next;
              if (temp.next != null) temp.next.prev = prev;
              last.next = temp;
              temp.prev = last;
              last = temp;
              last.next = null;
              break;
         }

         prev = temp;
         temp = temp.next;
    }
}

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

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