Java个人链接列表

时间:2019-03-15 07:01:48

标签: java list linked-list

因此,我在不使用内置列表的情况下创建链接列表。我有很多方法需要完成工作。我正在竭尽全力从LL中删除元素。我真的不需要(尽管不介意)一种功能全面的方法,我只需要知道我的步骤将会是什么。这是我的代码:

public class MoveToFrontList {

    private StringCountElement head; // the head reference
    private StringCountElement tail; // the tail reference
    private int size; // the size of the list (number of valid items)

    /**
     * _Part 1: Implement this constructor._
     * 
     * Creates a new, initially empty MoveToFontList. This list should be a
     * linked data structure.
     */
    public MoveToFrontList() {
        head = new StringCountElement();        //allocating memory for head
        tail = head;            //replicating head onto tail
        //head.next = tail;     //making pointer of next from head to tail
        //tail.prev = head;     //making pointer of prev from tail to head
    }

    /**
     * This method increments the count associated with the specified string
     * key. If no corresponding key currently exists in the list, a new list
     * element is created for that key with the count of 1. When this method
     * returns, the key will have rank 0 (i.e., the list element associated with
     * the key will be at the front of the list)
     * 
     * @param key
     *            the string whose count should be incremented
     * @return the new count associated with the key
     */
    public int incrementCount(String key) {
        StringCountElement s = find(key);
        if (s != null) {
            // found the key, splice it out and increment the count
            spliceOut(s);
            s.count++;
        } else {
            // need to create a new element
            s = new StringCountElement();
            s.key = key;
            s.count = 1;
        }
        // move it to the front
        spliceIn(s, 0);
        return s.count;
    }

    /**
     * 
     * @return the number of items in the list
     */
    public int size() {
        return size;
    }

    /**
     * _Part 2: Implement this method._
     * 
     * Find the list element associated with the specified string. That is, find
     * the StringCountElement that a key equal to the one specified
     * 
     * @param key
     *            the key to look for
     * @return a StringCountElement in the list with the specified key or null
     *         if no such element exists.
     */
    public StringCountElement find(String key) {
        StringCountElement temp = head;     //creating a temp object to evaluate

        for(int i = 0; i <= size(); i++){
            if(temp.key == null){
                return null;
            }
            if(temp.key.equals(key)){
                return temp;
            }
            if(temp.next != null) {
                temp = temp.next;
            }
        }

        return null;            //returning null since no object with the right key was found
    }

    /**
     * _Part 3: Implement this method._
     * 
     * Compute the rank of the specified key. Rank is similar to position, so
     * the first element in the list will have rank 0, the second element will
     * have rank 1 and so on. However, an item that does not exist in the list
     * also has a well defined rank, which is equal to the size of the list. So,
     * the rank of any item in an empty list is 0.
     * 
     * @param key
     *            the key to look for
     * @return the rank of that item in the rank 0...size() inclusive.
     */
    public int rank(String key) {
        int rank = 0;
        StringCountElement temp = head;

        do{
            if(temp.key == null){
                return size();
            }
            if(temp.key.equals(key)) {
                return rank;
            }
            if(temp.next != null) {
                temp = temp.next;
            }
            rank++;
        } while(temp.next != null);
        return size();
    }

    /**
     * _Part 4: Implement this method._
     * 
     * Splice an element into the list at a position such that it will obtain
     * the desired rank. The element should either be new, or have been spliced
     * out of the list prior to being spliced in. That is, it should be the case
     * that: s.next == null && s.prev == null
     * 
     * @param s
     *            the element to be spliced in to the list
     * @param desiredRank
     *            the desired rank of the element
     */
    public void spliceIn(StringCountElement s, int desiredRank) {
        StringCountElement temp = head;

        for(int i=0; i < desiredRank; i++){            //reaching desired rank location
            temp = temp.next;
        }

        if(desiredRank == 0){
            head = s;
            size++;
            return;
        }
        //temp will be the spot that s will take over
        s.next = temp;      // pointing element after s to temp
        s.prev = temp.prev; // pointing previous element before s to be previous element before temp
        temp.prev.next = s; // pointing element before temp to s element
        temp.prev = s;      // pointing previous element before temp to be s
        size++;


        return;
    }

    /**
     * _Part 5: Implement this method._
     * 
     * Splice an element out of the list. When the element is spliced out, its
     * next and prev references should be set to null so that it can safely be
     * splicedIn later. Splicing an element out of the list should simply remove
     * that element while maintaining the integrity of the list.
     * 
     * @param s
     *            the element to be spliced out of the list
     */
    public void spliceOut(StringCountElement s) {

        return;
    }

}

因此,我需要有关第5部分的帮助。到目前为止,我需要了解LL中第一个和最后一个元素的测试用例。另外,我认为此分配要求使用头和尾,而不是使用空引用。

我也知道splicein方法还需要最后一个元素的测试用例。除此之外,是否有关于简化和/或清理代码的建议?任何帮助表示赞赏!谢谢!

编辑:这是元素包含的内容:

/**
 * A Container class for a doubly linked data structure that
 * stores String keys and associated integer counts.
 */
public class StringCountElement {
    public StringCountElement next;
    public StringCountElement prev;
    public String key;
    public int count;

}

2 个答案:

答案 0 :(得分:1)

所需步骤如下。

  • 找到需要拼接的元素,假设它是 current
  • 将上一个和下一个元素临时存储。
  • previous.next设置为next
  • next.previous设置为previous current.previous = current.next = null

答案 1 :(得分:0)

因此,我对代码进行了一些大修。如果你们想查看更新的代码,请告诉我。无论如何,这是我更新的第5部分的spliceOut方法:

public void spliceOut(StringCountElement s) {
        //checking if only one element in list.
        //if so, then head and tail are the references to that element
        if(size == 1){
            head = null;
            tail = null;
        }
        //if s.prev is null, then that means s is at the beginning of list. so head == s
        if(s.prev == null){
            head = s.next;          //making head reference equal the next element in the list
            head.prev = null;       //since head is equal to element two, erasing head.prev
                                    // pointer will not show first element in the list
        }
        //if s.next is null, then s is the last element in list. So tail == s
        else if(s.next == null){
            tail = s.prev;          //making tail reference equal to element before tail
            tail.next = null;       //making original last tail element null'd out of list
        }
        //s is not at the beginning or end of list
        else{
            s.prev.next = s.next;   //making prev element before s point to element after s
            s.next.prev = s.prev;   //making next element after s point to element before s
        }

        //making pointers of s not point to any other elements
        s.prev = null;
        s.next = null;
        size--;     //since an element was taken out of the list, size of list is reduced by one

        return;
    }

我很困惑,当列表中只有一个元素时该怎么办。如果列表中只有一个元素,头和尾引用是否都指向该元素?因此,如果我想从列表中删除该元素,是否不应该将head和tails设置为null?