give an implementation of the size() method for the circularlylinkedlist class, assuming that we did not maintain size as an instance variabl in javae.
Circularlylinkedlist: Circular linked list is a linked list where all nodes are connected in the form of a circle.There is no NULL at the end.Any node can be a starting point and user can traverse whole list by starting from any point.
Program of size() method for the circularlylinkedlist class, assuming that we did not maintain size as an instance variabl in java
public int size(){
node n = head;
node m = tail;
if(n == null){
return 0;
}
int size = 1;
While(n!=m){
size++;
n=n.getNext();
}
return size;
}
Explanation:
Initialize node n equal to head and node m equal to tail. also initialize size to 1 so that user can check the last element of the list. If we use 0 instead of 1 then it will return the number of elements of the linked list minus one.
Get Answers For Free
Most questions answered within 1 hours.