简体   繁体   English

Java 个人链表

[英]Java Personal Linked List

So I am creating a linked list without using the built in one.所以我正在创建一个链表而不使用内置的链表。 I have a bunch of methods for my assignment that I need to make work.我的作业有很多方法需要完成。 I have everything working up to removing elements from the LL.我已经完成了从 LL 中删除元素的所有工作。 I don't really need (although wouldn't mind) a fully functioning method, I just need to know what my steps would be.我真的不需要(虽然不介意)一个功能齐全的方法,我只需要知道我的步骤是什么。 Here is my code:这是我的代码:

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;
    }

}

So I need help with Part 5. Things I know so far, I will need test cases for the first and last element in the LL.所以我需要第 5 部分的帮助。到目前为止我知道的事情,我需要 LL 中第一个和最后一个元素的测试用例。 Also I think this assignment requires that the head and tail are used, not as empty references.另外我认为这个分配要求使用头部和尾部,而不是作为空引用。

I also know that the splicein method also needs a test case for the last element.我也知道 splicein 方法也需要最后一个元素的测试用例。 Aside from that is there any recommendation on simplifying and/or cleaning up my code?除此之外,是否有任何关于简化和/或清理我的代码的建议? Any help is appreciated!任何帮助表示赞赏! Thank you!谢谢!

EDIT: This is what an element contains:编辑:这是一个元素包含的内容:

/**
 * 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;

}

The steps that are needed are as below.所需的步骤如下。

  • Find the element that you need to splice out, let's assume it is current找到需要拼接的元素,假设它是current元素
  • Store the previous and next element in temporaries.将上一个和下一个元素存储在临时文件中。
  • Set previous.next to nextprevious.next next设置为next
  • Set next.previous to previous Set current.previous = current.next = null设置next.previousprevious设置current.previous = current.next = null

So I did some heavy overhauling to my code.所以我对我的代码进行了一些大修。 If you guys want to see the updated code, let me know.如果你们想查看更新的代码,请告诉我。 Anyways, here is my updated spliceOut method, for part 5:无论如何,这是我更新的 spliceOut 方法,用于第 5 部分:

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;
    }

I am confused though, what to do when there is only one element in the list.我很困惑,当列表中只有一个元素时该怎么办。 If there is only one element in the list, isn't the head and tail reference both pointing to that element?如果列表中只有一个元素,头和尾引用不是都指向那个元素吗? So if I wanted to remove that element out of the list, shouldn't I set head and tails to null?因此,如果我想从列表中删除该元素,是否应该将 head 和 tails 设置为 null?

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

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