【发布时间】:2020-12-18 20:23:36
【问题描述】:
我有一个列表
public class SinglyLinkedList {
//---------------- nested Node class ----------------
private static class Node {
private String element; // reference to the element stored at this node
private Node next; // reference to the subsequent node in the list
public Node(String e, Node n) {
element = e;
next = n;}
public String getElement( ) { return element; }
public Node getNext( ) { return next; }
public void setNext(Node n) { next = n; }
}
// instance variables of the SinglyLinkedList
private Node head = null; // head node of the list (or null if empty)
private Node tail = null; // last node of the list (or null if empty)
private int size = 0; // number of nodes in the list
public SinglyLinkedList( ) { } // constructs an initially empty list
// access methods
public int size( ) { return size; }
public boolean isEmpty( ) { return size == 0; }
public String first( ) {
// returns (but does not remove) the first element
if (isEmpty( )) return null;
return head.getElement( );
}
public String last( ) {
// returns (but does not remove) the last element
if (isEmpty( )) return null;
return tail.getElement( );
}
// update methods
public void addFirst(String e) {
// adds element e to the front of the list
head = new Node(e, head); // create and link a new node
if (size == 0)
tail = head; // special case: new node becomes tail also
size++;
}
public void addLast(String e) {
// adds element e to the end of the list
Node newest = new Node(e, null); // node will eventually be the tail
if (isEmpty( ))
head = newest; // special case: previously empty list
else
tail.setNext(newest); // new node after existing tail
tail = newest; // new node becomes the tail
size++;
}
}
我的主要看起来像:
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
SinglyLinkedList Liste1 = new SinglyLinkedList();
//Bam funktioniert
Liste1.addFirst("Hello world!");
}
}
我想在主要内容中添加游标:
Cursor C1 = new addCursor(3);
这个 C1 光标指向列表元素 nr。 3
Cursor C2 = new addCursor(5);
此 C2 光标指向列表元素 nr.5(如果存在)
我想在 main.js 中使用游标。所以函数 addCursor 将在 SinglyLinkedList 类中,但它会将游标返回到节点。
所以 Curser 就像一只坐在节点上的螃蟹。
有可能吗?
如果不是,也许还有其他建议?
【问题讨论】:
标签: java list data-structures linked-list