简体   繁体   English

设置链表Java

[英]setting up linked list Java

I'm working on some basic linked list stuff, like insert, delete, go to the front or end of the list, and basically i understand the concept of all of that stuff once i have the list i guess but im having trouble setting up the list. 我正在处理一些基本的链接列表内容,例如插入,删除,转到列表的开头或结尾,基本上,一旦我有了列表,我就会理解所有这些东西的概念,但是我在设置时遇到了麻烦名单。 I was wondering of you guys could tell me if im going in the right direction. 我想知道你们能否告诉我即时消息是否朝着正确的方向发展。 (mostly just the setup) this is what i have so far: (主要只是设置)这是我到目前为止所拥有的:

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

} }

I got the node class alright (well i think it works alright), but is it necessary to even have that class? 我的节点类还不错(好吧,我认为它可以正常工作),但是是否有必要拥有该类呢? or can i go about it without even using it (just curious). 或者我可以不使用它就去解决它(只是好奇)。 And for example on the method get() in the list class can i not call that getItem() method from the node class because it's getting an error even though i thought that was the whole point for the node class. 例如,在列表类中的方法get()上,我不能从节点类中调用该getItem()方法,因为即使我认为这是节点类的重点,它也会报错。

bottom line i just wanna make sure im setting up the list right. 最重要的是,我只是想确保即时通讯设置正确的列表。

Thanks for any help guys, im new to linked list's so bear with me! 谢谢大家的帮助,我是链表的新手,请多多包涵!

head , cur and last should be Node s, not List s. headcurlast应该是Node S,未List秒。

Also you should declare Node as Node<T> , then it can contain any type of object (instead of just a char ). 还应该声明Node作为Node<T>那么它可以包含任何类型的对象(而不是只是一个char )。 You'd have to replace all the word char with T , and your class List should therefore also be List<T> . 您必须将所有单词char替换为T ,因此您的类List也应该是List<T>

delete actually needs to remove an element, it doesn't right now. delete实际需要来删除一个元素,它不是现在。

Also, you've given your List iterator functionality (with cur )... you would probably be better off separating that functionality in a separate class (or rename your list to "IteratedList") or something. 另外,您还给了List迭代器功能(带有cur )...最好将功能分开在单独的类中(或将列表重命名为“ IteratedList”)或其他。

Otherwise a pretty decent start! 否则一个不错的开始!

I think you are missing something very important - some more comments. 我认为您缺少一些非常重要的内容-一些其他评论。 In particular, I think you should write a comment block explaining how the list is to be represented, including what it should look like when empty, with one element, and with more than one element. 特别是,我认为您应该编写一个注释块来解释该列表的表示方式,包括该列表在为空,带有一个元素并且包含多个元素时的外观。 Work through a few scenarios with paper and pencil. 使用纸和笔在几种情况下工作。

When you know what an empty list should look like, and are sure that representation does what you need, the constructor will be really easy to write. 当您知道一个空列表应该是什么样子,并确保表示形式满足您的需要时,构造函数将非常容易编写。

As far as I understand. 据我所理解。 You're making a linked list, so a List object should somehow use Node objects, right? 您正在创建一个链表,所以List对象应该以某种方式使用Node对象,对吗? you want to have the list represented by a head (node), current (node), next (also a node) as well the size (int). 您希望列表由头(节点),当前(节点),下一个(也是节点)以及大小(整数)表示。

I don't believe it makes sense to have the private List linkedList; 我不认为拥有private List linkedList;是没有道理的private List linkedList; . To get a better understanding why it doesn't work, try by hand to initialize a list, you will find yourself initializing a new list, which will lead to intializing a new list, ... etc. That's one reason why you get an error regardless of other problems I told you about in the design itself. 为了更好地理解为什么它不起作用,请手动尝试初始化列表,您会发现自己正在初始化一个新列表,这将导致初始化一个新列表,等等。这就是为什么您得到一个列表的原因之一。错误,无论我在设计本身中告诉过您什么其他问题。

Keep working on that. 继续努力。 You will also need to enhance the implementation of delete as well. 您还需要增强delete的实现。 To delete a node from a list, you don't just decrease the size, you should make its previous node references its next node. 要从列表中删除节点,您不仅要减小大小,还应使其前一个节点引用其下一个节点。 something like prev.next = node.next . 类似prev.next = node.next But keep working, you'll learn a lot in this exercise. 但是继续努力,您将在本练习中学到很多东西。

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

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