【问题标题】:Acces Linked Lists Nodes directly from main直接从 main 访问链表节点
【发布时间】: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


    【解决方案1】:

    首先你可以创建 Cursor 类:

    public class Cursor {
        private SinglyLinkedList.Node cursor;
    
        Cursor(SinglyLinkedList.Node cursor){
            this.cursor = cursor;
        }
    
        public SinglyLinkedList.Node getCursor(){
            return cursor;
        }
    }
    

    然后是SinglyLinkedList类中的方法addCursor

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

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 2017-05-12
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多