繁体   English   中英

直接从 main 访问链表节点

[英]Acces Linked Lists Nodes directly from main

我有一个清单

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 Cursor 指向列表元素 nr。 3

Cursor C2 = new addCursor(5);

此 C2 Cursor 指向列表元素 nr.5(如果存在)

我想在 main.js 中使用游标。 所以 function addCursor 将位于 SinglyLinkedList class 但它会将游标返回到节点。

所以 Curser 就像一只坐在节点上的螃蟹。

可能吗?

如果不是,也许还有其他建议?

首先,您可以创建 Cursor class:

public class Cursor {
    private SinglyLinkedList.Node cursor;

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

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

然后是addCursor中的SinglyLinkedList方法:

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;
}

此方法将在链表中搜索并返回 position 中的元素等于值。

然后从主调用:

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);
    .....
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM