2012-11-05 25 views
0

我正在處理一些基本鏈接列表的東西,比如插入,刪除,到列表的前端或末尾,基本上我明白所有這些東西的概念,一旦我有了列表我想,但我很難設置清單。我想知道你們是否可以告訴我是否正朝着正確的方向前進。 (大多隻是設置)這是我到目前爲止有:設置鏈表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()方法,因爲即使我認爲這是節點類的整個點,它也會發生錯誤。

底線我只是想確保即時建立清單的權利。

感謝任何幫助傢伙,即時通訊新的鏈接列表,所以忍受着我!

+0

這聽起來像是我10年前做的家庭作業。 –

+0

爲什麼頭等全部列出?這是搞砸了:( – smk

+0

是不是呢?:(我試圖理解,但從每個人都在說,你聽起來像我沒有希望! – erp

回答

0

headcurlastNode S,不List秒。

此外,您應該聲明NodeNode<T>,那麼它可以包含任何類型的對象(而不僅僅是一個char)。您必須將char全部替換爲T,因此您的類List也應該是List<T>

delete實際上需要刪除一個元素,它現在不正確。另外,你已經給出了List迭代器的功能(用cur)......你可能會更好地將單獨的類中的功能分開(或者將你的列表重命名爲「IteratedList」)或其他東西。

否則一個相當體面的開始!

+0

嘿謝謝你的答覆!我知道刪除必須刪除哈哈這不是一個蹩腳的嘗試,這是我沒有做到這一點!還有關於沒關係,因爲我只需要char's。這只是一個簡單的列表來完成這些簡單的任務。你能否詳細說明CUR的情況,因爲我需要它在這個類中作爲一種遍歷方式和類似的方式(就像我錯誤地使用它那樣?) – erp

+0

爲什麼'List'需要知道如何遍歷自己?List是一堆東西。例如設計筆記本以配備筆;或者,您可以分別提供一個筆記本和一支筆.Java做它的方式,'java.util.List'(筆記本,有效地)有一個方法'.iterator() '給你一個爲你工作的'Iterator'對象(筆)你可能想爲你的用戶提供一個「筆記本+筆」... – durron597

+0

開始有意義,所以如果我不使用cur,那麼我會如何去做下一個元素(cur = cur.next )或get方法的全部點是返回當前元素,那麼如何在沒有當前的情況下工作? – erp

0

我想你錯過了一些非常重要的東西 - 一些更多的評論。特別是,我認爲你應該寫一個註釋塊來解釋列表如何表示,包括空白時應該看起來像什麼,一個元素和多個元素。用紙和鉛筆練習幾個場景。

當你知道什麼是空列表應該看起來像,並確保表示做你所需要的,構造函數將非常容易編寫。據我所知,

0

。你正在創建一個鏈表,所以List對象應該以某種方式使用Node對象,對吧?你想有頭(節點),當前(節點),下一個(也是一個節點)以及大小(int)表示的列表。我不相信private List linkedList;有意義。爲了更好地理解爲什麼它不起作用,請嘗試手動初始化列表,您將發現自己正在初始化一個新列表,這將導致初始化一個新列表,...等等。這就是爲什麼你得到一個不管我在設計本身告訴過你的其他問題。

繼續努力。您還需要加強刪除的實施。要從列表中刪除一個節點,您不僅要減小大小,還應該讓其前一個節點引用其下一個節點。像prev.next = node.next。但繼續工作,你會在這個練習中學到很多東西。

+0

嘿,謝謝你的回覆!就像我對另一個人說的,我應該評論說,在刪除方法哈哈我沒有完成,我不是thaaaaat骨頭:p所以你說什麼關於「列表頭,cur,next和LinkedList」是不必要的?我使用這些的原因是我可以說像cur.next等這樣的東西,還是我只是做這一切都錯了?喜歡而不是說我使用節點類方法,如cur.setNext(next)? – erp

+0

這真的取決於「next」和「curr」是什麼意思,用什麼來使用它們?我沒有說他們是不必要的(如果我要實現這個類,我會使用類似的東西),我說他們應該是節點而不是列表。 – yahiaelgamal

+0

我想我不會用它來做任何事情,我只是認爲你必須聲明一些如何在public void goLast()中聲明一些東西「)''cur = cur.next'」我的意思是現在我想到了它我想cur只是一個指針,用於跟蹤我在列表中的位置 – erp