[英]setting up linked list 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()方法,因為即使我認為這是節點類的重點,它也會報錯。
最重要的是,我只是想確保即時通訊設置正確的列表。
謝謝大家的幫助,我是鏈表的新手,請多多包涵!
head
, cur
和last
應該是Node
S,未List
秒。
還應該聲明Node
作為Node<T>
那么它可以包含任何類型的對象(而不是只是一個char
)。 您必須將所有單詞char
替換為T
,因此您的類List
也應該是List<T>
。
delete
實際需要來刪除一個元素,它不是現在。
另外,您還給了List
迭代器功能(帶有cur
)...最好將功能分開在單獨的類中(或將列表重命名為“ IteratedList”)或其他。
否則一個不錯的開始!
我認為您缺少一些非常重要的內容-一些其他評論。 特別是,我認為您應該編寫一個注釋塊來解釋該列表的表示方式,包括該列表在為空,帶有一個元素並且包含多個元素時的外觀。 使用紙和筆在幾種情況下工作。
當您知道一個空列表應該是什么樣子,並確保表示形式滿足您的需要時,構造函數將非常容易編寫。
據我所理解。 您正在創建一個鏈表,所以List對象應該以某種方式使用Node對象,對嗎? 您希望列表由頭(節點),當前(節點),下一個(也是節點)以及大小(整數)表示。
我不認為擁有private List linkedList;
是沒有道理的private List linkedList;
。 為了更好地理解為什么它不起作用,請手動嘗試初始化列表,您會發現自己正在初始化一個新列表,這將導致初始化一個新列表,等等。這就是為什么您得到一個列表的原因之一。錯誤,無論我在設計本身中告訴過您什么其他問題。
繼續努力。 您還需要增強delete的實現。 要從列表中刪除節點,您不僅要減小大小,還應使其前一個節點引用其下一個節點。 類似prev.next = node.next
。 但是繼續努力,您將在本練習中學到很多東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.