繁体   English   中英

设置链表Java

[英]setting up linked list Java

我正在处理一些基本的链接列表内容,例如插入,删除,转到列表的开头或结尾,基本上,一旦我有了列表,我就会理解所有这些东西的概念,但是我在设置时遇到了麻烦名单。 我想知道你们能否告诉我即时消息是否朝着正确的方向发展。 (主要只是设置)这是我到目前为止所拥有的:

public class List {

private int size;
private List linkedList;
List head;
List cur;
List next;

/**
 * Creates an empty list.
 * @pre 
 * @post
 */
public List(){
    linkedList = new List();
    this.head = null;
    cur = head; 
}

/**
 * Delete the current element from this list. The element after the deleted element becomes the new current. 
 * If that's not possible, then the element before the deleted element becomes the new current. 
 * If that is also not possible, then you need to recognize what state the list is in and define current accordingly.
 * Nothing should be done if a delete is not possible.
 * @pre
 * @post
 */ 
public void delete(){

}

/**
 * Get the value of the current element. If this is not possible, throw an IllegalArgumentException.
 * @pre the list is not empty
 * @post
 * @return value of the current element.
 */
public char get(){
    return getItem(cur);
}

/**
 * Go to the last element of the list. If this is not possible, don't change the cursor.
 * @pre
 * @post
 */
public void goLast(){
    while (cur.next != null){
        cur = cur.next;
    }
}

/**
 * Advance the cursor to the next element. If this is not possible, don't change the cursor.
 * @pre
 * @post
 */
public void goNext(){
    if(cur.next != null){
        cur = cur.next;}
    //else do nothing
}

/**
 * Retreat the cursor to the previous element. If this is not possible, don't change the cursor.
 * @pre
 * @post
 */
public void goPrev(){

}

/**
 * Go to top of the list. This is the position before the first element.
 * @pre
 * @post
 */
public void goTop(){

}

/**
 * Go to first element of the list. If this is not possible, don't change the cursor.
 * @pre
 * @post
 */
public void goFirst(){

}

/**
 * Insert the given parameter after the current element. The newly inserted element becomes the current element.
 * @pre
 * @post
 * @param newVal : value to insert after the current element.
 */
public void insert(char newVal){
    cur.setItem(newVal);
    size++;
}

/**
 * Determines if this list is empty. Empty means this list has no elements.
 * @pre
 * @post
 * @return true if the list is empty.
 */
public boolean isEmpty(){
    return head == null;
}

/**
 * Determines the size of the list. The size of the list is the number of elements in the list.
 * @pre
 * @post
 * @return size which is the number of elements in the list.
 */
public int size(){
    return size;
}


public class Node {

    private char item;
    private Node next;

    public Node() {
    }

    public Node(char item) {
            this.item = item;
    }

    public Node(char item, Node next) {
        this.item = item;
        this.next = next;
    }

    public char getItem() {
        return this.item;
    }

    public void setItem(char item) {
        this.item = item;
    }

    public Node getNext() {
        return this.next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

}

我的节点类还不错(好吧,我认为它可以正常工作),但是是否有必要拥有该类呢? 或者我可以不使用它就去解决它(只是好奇)。 例如,在列表类中的方法get()上,我不能从节点类中调用该getItem()方法,因为即使我认为这是节点类的重点,它也会报错。

最重要的是,我只是想确保即时通讯设置正确的列表。

谢谢大家的帮助,我是链表的新手,请多多包涵!

headcurlast应该是Node S,未List秒。

还应该声明Node作为Node<T>那么它可以包含任何类型的对象(而不是只是一个char )。 您必须将所有单词char替换为T ,因此您的类List也应该是List<T>

delete实际需要来删除一个元素,它不是现在。

另外,您还给了List迭代器功能(带有cur )...最好将功能分开在单独的类中(或将列表重命名为“ IteratedList”)或其他。

否则一个不错的开始!

我认为您缺少一些非常重要的内容-一些其他评论。 特别是,我认为您应该编写一个注释块来解释该列表的表示方式,包括该列表在为空,带有一个元素并且包含多个元素时的外观。 使用纸和笔在几种情况下工作。

当您知道一个空列表应该是什么样子,并确保表示形式满足您的需要时,构造函数将非常容易编写。

据我所理解。 您正在创建一个链表,所以List对象应该以某种方式使用Node对象,对吗? 您希望列表由头(节点),当前(节点),下一个(也是节点)以及大小(整数)表示。

我不认为拥有private List linkedList;是没有道理的private List linkedList; 为了更好地理解为什么它不起作用,请手动尝试初始化列表,您会发现自己正在初始化一个新列表,这将导致初始化一个新列表,等等。这就是为什么您得到一个列表的原因之一。错误,无论我在设计本身中告诉过您什么其他问题。

继续努力。 您还需要增强delete的实现。 要从列表中删除节点,您不仅要减小大小,还应使其前一个节点引用其下一个节点。 类似prev.next = node.next 但是继续努力,您将在本练习中学到很多东西。

暂无
暂无

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

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