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