Question

give an implementation of the size() method for the circularlylinkedlist class, assuming that we did not...

give an implementation of the size() method for the circularlylinkedlist class, assuming that we did not maintain size as an instance variabl in javae.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
java Give an example of a class that contains at least one class variable and at...
java Give an example of a class that contains at least one class variable and at least one class method. Explain why using a class variable and method rather than an instance variable and method would be the correct choice in the example you select.
Consider a linked implementation of a Stack. Implement the pop() method below. Assume a typical singly...
Consider a linked implementation of a Stack. Implement the pop() method below. Assume a typical singly linked node, with an instance variables data and next. public class LinkedStack<T> implements Stack<T> { private Node<T> head;    public LinkedStack() { head = new Node<T>(); } // pops the top item off the stack // returning the popped value // throw an EmptyStackException if there are no // items in the stack public T pop() { // write your implementation below } }
When we did hypothesis testing for proportions in class, we were able to use a normal...
When we did hypothesis testing for proportions in class, we were able to use a normal distribution in all cases, provided the sample size was large enough. Explain why we can always apply the Central Limit Theorem to a random variable X, if X ∼ Bern(p) and the sample size is sufficiently large.
5. When we did hypothesis testing for proportions in class, we were able to use a...
5. When we did hypothesis testing for proportions in class, we were able to use a normal distribution in all cases, provided the sample size was large enough. Explain why we can always apply the Central Limit Theorem to a random variable and the sample size is sufficiently large. (Hint: Think about what went wrong in the other cases!)
Throughout the semester, we discussed quality management and implementation but little did we discussed issues and...
Throughout the semester, we discussed quality management and implementation but little did we discussed issues and drawbacks of quality systems. Do a literature review using mainly academic journal publications in last 10 years (2, 3 articles) and write about what scholars have said on issues related to implementing quality control, measures, frameworks (such as ISO) in either product- or service-oriented industries.
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java...
Please show fully functioning java code and outputs. Design a Java Animal class (assuming in Animal.java file) and a sub class of Animal named Cat (assuming in Cat.java file).   The Animal class has the following protected instance variables: boolean vegetarian, String eatings, int numOfLegs and the following public instance methods: constructor without parameters: initialize all of the instance variables to some default values constructor with parameters: initialize all of the instance variables to the arguments SetAnimal: assign arguments to all...
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
Imagine that we divided the class into groups of 5, everyone did one speed test on...
Imagine that we divided the class into groups of 5, everyone did one speed test on their own device, and we calculated variance and SD for each group. Then we calculated variance and SD for the entire class (n = about 45). How would variance and SD for the entire class differ from variance and SD for the smaller groups?
Give at least two different examples of polymorphism in Class Insecta. How did polymorphism aid in...
Give at least two different examples of polymorphism in Class Insecta. How did polymorphism aid in the diversification and abundance of insects?
What method did we use in lab to visualize the DNA fragments?
What method did we use in lab to visualize the DNA fragments?