我正在處理一些基本鏈接列表的東西,比如插入,刪除,到列表的前端或末尾,基本上我明白所有這些東西的概念,一旦我有了列表我想,但我很難設置清單。我想知道你們是否可以告訴我是否正朝着正確的方向前進。 (大多隻是設置)這是我到目前爲止有:設置鏈表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()方法,因爲即使我認爲這是節點類的整個點,它也會發生錯誤。
底線我只是想確保即時建立清單的權利。
感謝任何幫助傢伙,即時通訊新的鏈接列表,所以忍受着我!
這聽起來像是我10年前做的家庭作業。 –
爲什麼頭等全部列出?這是搞砸了:( – smk
是不是呢?:(我試圖理解,但從每個人都在說,你聽起來像我沒有希望! – erp