0

我正在研究一些基本的链接列表的东西,比如插入、删除、转到列表的前面或末尾,基本上我理解了所有这些东西的概念,一旦我有了我想的列表,但我在设置时遇到了麻烦名单。我想知道你们能告诉我我是否朝着正确的方向前进。(主要是设置)这是我到目前为止所拥有的:

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() 方法,因为即使我认为这是节点类的重点,它也会出错。

底线我只是想确保即时设置列表正确。

感谢您的帮助,我是链表的新手,所以请多多包涵!

4

3 回答 3

0

我认为你遗漏了一些非常重要的东西——更多的评论。特别是,我认为你应该写一个注释块来解释列表是如何表示的,包括它在空时应该是什么样子,有一个元素,有多个元素。用纸和铅笔完成几个场景。

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

于 2012-11-05T05:00:59.483 回答
0

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

我不相信拥有private List linkedList;. 为了更好地理解它为什么不起作用,尝试手动初始化一个列表,你会发现自己正在初始化一个新列表,这将导致初始化一个新列表,......等等。这就是你得到一个无论我在设计本身中告诉您的其他问题如何,都会出错。

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

于 2012-11-05T05:02:42.497 回答
0

headcur并且last应该是Nodes,而不是Lists。

您还应该声明NodeNode<T>,然后它可以包含任何类型的对象(而不仅仅是 a char)。您必须将所有单词替换为char,因此T您的课程List也应该是List<T>

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

此外,您已经提供了List迭代器功能(使用cur)......您最好将该功能分离到一个单独的类中(或将您的列表重命名为“IteratedList”)或其他东西。

否则一个相当不错的开始!

于 2012-11-05T04:58:46.787 回答