Help please write a method that determines the length of a single-linked list.
The task:
Develop a class library for working with singly linked lists. Namely, 3 methods:

public class SingleLLUtils_1 { public static int length(Node tail) {...} public static int max(Node tail) {...} public static int sum(Node tail) {...} } 

My code here is:

 public class SingleLLUtils_1 { public static Node create (int value){ if (value >=0 ){ return new Node(value, create(value - 1)); }else { return null; } } public static int length(Node tail) { return tail > 0 ? length(tail-1) int count++ : count; } } 
  • 2
    Announce the whole list code, please. - post_zeew
  • @post_zeew public static Node create (int value) {if (value> = 0) {return new Node (value, create (value - 1)); } else {return null; }} - Man
  • Add the code for the entire list to the question itself (you can edit it). - post_zeew
  • @post_zeew added)) - Man

1 answer 1

Good evening, gentlemen! He wrote a similar task. In my case, the method for obtaining the length of a single-linked list looked like this:

As a result, returns the number of items in the entire list If there is a need I can share my code.

head is the initial element of the list, head.next is a link to the next element.

 public int getSize(){ int count = 0; if(head != null){ count++; while(head.nextN != null){ count++; head = head.nextN; } }else{ return 0; } return count; } 

Full sample code:

 /* Pr9-2* Разработать класс для работы с односвязными списками. 4 метода из DynamicArray являются обязательными: public class DynamicArray { public Node add(int elem) {...} public Node remove() {...} public Node add(int index, int elem) {...} public Node remove(int index) {...} } */ package pr9.pkg2; class Node{ public int index; public Node nextN; } class Container{ private Node head; private Node tail; public int getSize(){ int count = 0; if(head != null){ count++; while(head.nextN != null){ count++; head = head.nextN; } }else{ return 0; } return count; } public void printContainer(){ Node n = head; while(n!=null){ System.out.print(n.index+" "); n = n.nextN; } System.out.println(""); } public void addFirst(int element){ Node n = new Node(); n.index = element; if(head == null){ head = n; tail = n; }else{ n.nextN = head; head = n; } } public void addLast(int element){ Node n = new Node(); n.index = element; if(tail == null){ head = n; tail = n; }else{ tail.nextN = n; tail = n; } } public void addMidle(int element, int index){ Node n = head; Node n1 = new Node(); n1.index = element; while(n.nextN != null){ if(n.index == index){ n1.nextN = n.nextN; n.nextN = n1; return; } n = n.nextN; } } public void setNodeIndex(int element, int index){ if(head == null){ System.out.println("Список пуст!"); return; } if(head.index == index){ head.index = element; return; } if(tail.index == index){ tail.index = element; return; } Node n = head.nextN; while(n != null){ if(n.index == index){ n.index = element; return; } n = n.nextN; } } public void removeFirstNode(){ if(head == null){ System.out.println("Список пуст!"); }else head = head.nextN; } public void removeNode(int index){ if(head == null){ System.out.println("Список пуст"); return; } if(head == tail){ head = null; tail = null; return; } if(head.index == index){ head = head.nextN; return; } Node n = head; while(n.nextN != null){ if(n.nextN.index == index){ if(tail == n.nextN){ tail = n; } n.nextN = n.nextN.nextN; return; } n = n.nextN; } } } public class Pr92 { public static void main(String[] args) { Container c = new Container(); c.addFirst(1); c.addLast(2); c.addFirst(5); c.addLast(7); c.addMidle(8, 2); c.printContainer(); c.setNodeIndex(77, 2); c.printContainer(); c.addFirst(55); System.out.println(c.getSize()); } } 
  • Please explain why public Node nextN; - This is a link to the next, private Node head; private Node tail; - and this is a link to the head and tail, how do they differ? I don’t understand why one link is the next and the others, albeit private, are current links, even if this and that are objects of the node class, perhaps because of the location, anyway, explain. - Man
  • public Node nextN; -a field class that contains a reference to an object of its type. This field is necessary to maintain the connection between the elements of the list (point to the next). private node head tail; - fields of another class: Container. They are instances of the Node class and point to the first and last elements of the list. And they are private because they are closed (encapsulated) and the ability to work with them is provided by methods. It follows that the types of the fields are the same because they are objects of the same class, but their function is different depending on what class they are used in. - Mr.Inpus