Q2. In the ListIterator class, what is the difference between next()and hasNext(
ID: 3779540 • Letter: Q
Question
Q2. In the ListIterator class, what is the difference between next()and hasNext() methods?
Q3.(a) What does the Linked List library Iterator method set()do? (b) And write just one line of java code just to call the set method given the following:
We have a linked list of Student class called students each node in this linked list is an object of type Student. And List Iterator called stdIter pointing to the first node as given by the following code:
LinkedList <Student>students = new LinkedList <Student> ();
ListIterator <Student> stdIter = students.listIterator();
stdIter.next(); // move the iterator once
Student std1 = new Student(); // construct a new Student object called std1
Now write the one line code to call the set()method on the iterator passing it the new object.:
Q5. Read about the following real life situations and decide whether you will use a stack or a queue. Provide valid reason of your choice.
1) A car wash facility.
2) Batteries in Flashlight.
3) Pringles (pack of chips).
4) Customers at Al-Baik.
5) Execution of a recursive method.
Explanation / Answer
Q2)
next(): next() method returns the next element and also moves cursor pointer to the next element. So if there is a next element, it will be returned. If there isn't it'll throw a 'NoSuchElementException', but if you use iterators properly you should never encounter it.
Ex:
Iterator itr = list.iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
hasNext(): hasNext() method returns true if iterator have more elements. So you can check if there are more objects you can get from the iterator. If there are no more objects it will return false.
Q3)
a) This method doesn't return any value. It is an optional operation, this method substitutes the element which is lastly backed by the iterator's next() with a specific element. We can change value object in the list while traversing it.
b) The required one line of code:
stdIter.set(std1);
Q5)
1) A car wash facility.
Queue : At car wash facility, the customer who visits the facility first will be served first, the customer won't be satisfied if its other way around. Follows First-In-First-Out order.
2) Batteries in Flashlight.
Stack : In Flashlight we have only one end open for inserting batteries, the other end is fixed with light so we can't use that end. So if we insert batteries from one end whatever we insert at last position(at the top) comes first while removing. Follows Last-In-First-Out order.
3) Pringles (pack of chips).
Stack : In Pringles, we use only end for adding chips as other end is fixed. So while removing we pick the chip which was inserted at the last position. Chip from the top position. Follows Last-In-First-Out order.
4) Customers at Al-Baik.
Queue : At Al-Baik, the customers who visit first will be served first, the customer won't be satisfied if its other way around. Follows First-In-First-Out order.
5) Execution of a recursive method.
Stack : Memory for the function is allocated on the stack space of the process and it is a run time activity. Hence when the function is called and activation record i.e. a stack frame is created and is pushed on top of the stack, and when the function returns the activation(i.e. when control hits the terminations condition) it pops the record and returns its value to next activation record. Follows Last-In-First-Out order.
Ex: int Factorial(int N) {
if(N == 1)
return 1;
else
return N * Factorial (N-1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.