简体   繁体   中英

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.

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.

Also you should declare Node as Node<T> , then it can contain any type of object (instead of just a char ). You'd have to replace all the word char with T , and your class List should therefore also be List<T> .

delete actually needs to remove an element, it doesn't right now.

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.

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? 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; . 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. 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 . But keep working, you'll learn a lot in this exercise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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