微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

直接从主访问链表节点

如何解决直接从主访问链表节点

我有一个列表

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 中使用游标。所以函数 addCursor 将在 SinglyLinkedList 类中,但它会将光标返回到节点。

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

有可能吗?

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

解决方法

首先你可以创建 Cursor 类:

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

该方法会在链表中搜索并返回位置等于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);
    .....
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。