简体   繁体   中英

Acces Linked Lists Nodes directly from main

I have a List

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

}



  

My main looks like:

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

  
  }
}

I would like to add Cursors in the main something like:

Cursor C1 = new addCursor(3); 

This C1 Cursor point to list element nr. 3

Cursor C2 = new addCursor(5);

This C2 Cursor points to list element nr.5 ( if exists)

I would like to use cursors in main. So the function addCursor would be in the SinglyLinkedList class but it would return Cursors to Nodes.

So Curser would be like a crab sitting on the node.

Is it possible?

If not maybe other suggestion?

First you can create the Cursor class:

public class Cursor {
    private SinglyLinkedList.Node cursor;

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

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

then the method addCursor in the SinglyLinkedList class:

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

this method will search in the linked list and return the element in the position equals to value.

Then calling from the main:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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