直接从main访问链表节点

5kgi1eie  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(300)

我有一张单子

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光标指向列表元素3

Cursor C2 = new addCursor(5);

此c2光标指向列表元素nr.5(如果存在)
我想用光标在主要。因此,函数addcursor将位于singlylinkedlist类中,但它将向节点返回游标。
所以诅咒者就像一只螃蟹坐在节点上。
有可能吗?
如果没有,也许还有其他建议?

nc1teljy

nc1teljy1#

首先可以创建游标类:

public class Cursor {
    private SinglyLinkedList.Node cursor;

    Cursor(SinglyLinkedList.Node cursor){
        this.cursor = cursor;
    }

    public SinglyLinkedList.Node getCursor(){
        return cursor;
    }
}

然后是方法 addCursorSinglyLinkedList 班级:

Cursor addCursor(int value){
    int count = 0;
    Node tmp = head;
    while(tmp != null){
        count++;
        if(count == value)
            return new Cursor(head);
        tmp = tmp.next;
    }
    return null;
}

此方法将在链表中搜索并返回位置等于value的元素。
然后从总台呼叫:

public static void main(String[] args) {
    System.out.println("Hello world!");
    SinglyLinkedList Liste1 = new SinglyLinkedList();
    //Bam  funktioniert
    Liste1.addFirst("Hello world!");
    Cursor C1 = Liste1.addCursor(3);
    .....
}

相关问题