我正在研究一些基本的链接列表的东西,比如插入、删除、转到列表的前面或末尾,基本上我理解了所有这些东西的概念,一旦我有了我想的列表,但我在设置时遇到了麻烦名单。我想知道你们能告诉我我是否朝着正确的方向前进。(主要是设置)这是我到目前为止所拥有的:
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() 方法,因为即使我认为这是节点类的重点,它也会出错。
底线我只是想确保即时设置列表正确。
感谢您的帮助,我是链表的新手,所以请多多包涵!