在Java中设置链表

在Java中设置链表,java,linked-list,Java,Linked List,我正在做一些基本的链表工作,比如插入,删除,转到链表的前面或结尾,基本上我理解了所有这些东西的概念,一旦我有了链表,我想,但是我在设置链表时遇到了麻烦。我想知道你们能告诉我我的方向是否正确。(主要是设置)到目前为止,我已经做到了: public class List { private int size; private List linkedList; List head; List cur; List next; /** * Creates an empty list. * @pre

我正在做一些基本的链表工作,比如插入,删除,转到链表的前面或结尾,基本上我理解了所有这些东西的概念,一旦我有了链表,我想,但是我在设置链表时遇到了麻烦。我想知道你们能告诉我我的方向是否正确。(主要是设置)到目前为止,我已经做到了:

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

我得到了node类(我认为它可以正常工作),但是有必要拥有这个类吗?或者我可以不用它就去做(只是好奇)。 例如,对于list类中的get()方法,我不能从node类调用getItem()方法,因为它得到了一个错误,尽管我认为这是node类的全部要点

总之,我只是想确保我的列表是正确的


谢谢大家的帮助,我是新加入链表的,请容忍我

head
cur
last
应该是
节点
s,而不是
列表
s

您还应该将
节点
声明为
节点
,然后它可以包含任何类型的对象(而不仅仅是
字符
)。你必须用
T
替换所有单词
char
,因此你的类
List
也应该是
List

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

此外,您还提供了
列表
迭代器功能(使用
cur
)。。。您最好在单独的类中分离该功能(或将列表重命名为“IteratedList”)或其他内容


否则这是一个不错的开始

我认为你遗漏了一些非常重要的东西——更多的评论。特别是,我认为您应该编写一个注释块来解释列表的表示方式,包括当列表为空、包含一个元素和多个元素时的外观。用纸和铅笔完成一些场景


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

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

我认为把
私有列表链接起来是没有意义的。为了更好地理解它为什么不工作,尝试手动初始化列表,您将发现自己正在初始化一个新列表,这将导致初始化一个新列表。。。这就是为什么不管我在设计中提到的其他问题,你都会出现错误的原因之一


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

这听起来像是我10年前做的家庭作业。为什么头等都列在清单上?这是一团糟:(有那么糟吗?):(我试图理解,但从每个人说的话和你的声音来看,我似乎没有希望!嘿,谢谢你的回复!我知道delete必须删除哈哈,这不是一个蹩脚的尝试,这是我还没有做的!而且这也没关系,因为我只需要char的。这只是一个简单的列表来完成这些简单的任务。你能详细说明一下吗在CUR的情况下,因为我在这个类中需要它作为遍历的一种方式,诸如此类(比如我在那里使用它是错误的吗?)。为什么一个
列表
需要知道如何遍历自己?一个
列表
是一堆东西。这就像设计一个带笔的笔记本一样。或者,你可以分别提供一个笔记本和一支笔。Java做这件事的方式,
Java.util.List
(实际上,笔记本)有一个方法
。迭代器()
它为您提供了一个迭代器(笔),可以为您执行该任务。当然,您可能希望为您的用户提供一个“笔记本+笔”…开始有意义了,因此如果我不使用cur,那么我将如何执行诸如转到下一个元素(cur=cur.next)之类的操作或者get方法的全部要点是返回当前元素,那么没有当前元素该如何工作呢?在
列表
类中不会有
get
方法……或者可以有
getAt(int index)
。但是可以执行
Node head=myList.getHeadNode()
,然后执行
current=head;执行{//stuff}while((current=current.next)!=null);
遍历列表谢谢你的回复!就像我对另一个人说的那样,我应该在delete方法中注释,哈哈,我没有完成,我不是thaaaaat boneheaded:p那么你是怎么说“列表头,cur,next和LinkedList”的呢不必要?我之所以使用这些方法,是因为我可以说cur.next之类的东西,还是我完全错了?比如说,我使用节点类方法,比如cur.setNext(next)?这取决于你所说的“next”和“curr”是什么意思,它们的用途是什么?我没有说它们是不必要的(如果我要实现这个类,我会使用类似的东西),我说它们应该是节点,而不是列表。我想我不会用它们做任何事情,我只是想你必须声明它们是如何实现的,这样才能说出类似于public void goLast()的东西“
cur=cur.next
”我的意思是,现在我想到它,我想cur只是一个指针,用来跟踪我在列表中的位置。在开始编写代码之前,您需要首先知道您要实现的类的功能是什么。