Add the following node in the same order Ali Sara Omar Eman
Add the following node in the same order: “Ali”, “Sara”, “Omar”, “Eman”
Add at the beginning the node “Ahmad”
Print the whole list
Add the node “Heba” between node “Ali” and node “Sara” using iterator
Remove the last node
Re-print the whole list
Here is the expected output of the program
OUTPUT
Ahmad Ali Sara Omar Eman
------------------- After ----------------
Ahmad Ali Heba sara Omar
Q2. In the ListIterator class, what is the difference between next()and hasNext() methods? [5 points]
Q3.(a) What does the Linked List library Iterator methodset()do? (b) And write just one line of java code just to call the set method given the following: [5 points]
We have a linked list of Student class calledstudents each node in this linked list is an object of typeStudent. And List Iterator calledstdIterpointing 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.:
Q4.Part(a) show the output for the following code the uses a Queue: [5 points]
2 publicclass QueueDemo {
3
4 publicstaticvoid main(String[] args) {
5
6 Queue queue = new LinkedList();
7
8 //add elements to queue using add method
9
10 queue.add(\"Ali\");
11 queue.add(\"Khaled\");
12 queue.add(\"Ahmed\");
13 queue.add(\"Fares\");
14 queue.add(\"Nasser\");
15
16 System.out.println(\"Items in the queue...\" + queue);
17
18 System.out.println(\"remove element: \" + queue.remove() );
19
20 System.out.println(\"retrieve element: \" + queue.peek() );
21
22 queue.add(\"Jameel\");
23
24 System.out.println(\"remove element: \" + queue.remove());
25
26 System.out.println(\"Items in the queue...\"
27 + queue.toString());
28 }
29 }
Q4.Part(b) show the output for the following code the uses a Stack: [5 points]
1 import java.util.*;
2 publicclass StackDemo {
3
4 publicstaticvoid main(String[] args) {
5
6 Stack s = new Stack();
7
8 //add elements to queue using add method
9
10 s.push(\"Ali\");
11 s.push(\"Khaled\");
12 s.push(\"Ahmed\");
13 s.push(\"Fares\");
14 s.push(\"Nasser\");
15
16 System.out.println(\"Items in the stack...\" + s);
17
18 System.out.println(\"remove element: \" + s.pop() );
19
20 System.out.println(\"retrieve element: \" + s.peek() );
21
22 s.push(\"Jameel\");
23
24 System.out.println(\"remove element: \" + s.pop());
25
26 System.out.println(\"Items in the stack...\" +
27 s.toString() );
28 }
29 }
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. [10 points]
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.
please answer all the q . i have sent the q and i have got onoly q5 answer .
Solution
HI, I have answered first two questions.Please re post others.
Please let me know in case of any issue.
1)
import java.util.LinkedList;
import java.util.ListIterator;
public class LinkedListTest {
public static void main(String[] args) {
//creating LinkedList of type String
LinkedList<String> list = new LinkedList<String>();
// adding “Ali”, “Sara”, “Omar”, “Eman”
list.add(\"Ali\");
list.add(\"Sara\");
list.add(\"Omar\");
list.add(\"Eman\");
// adding “Ahmad” at begining
list.add(0, \"Ahmad\");
// printing list
for(String s : list){
System.out.print(s+\" \");
}
System.out.println();
// adding “Heba” between node “Ali” and node “Sara”
ListIterator<String> iterator = list.listIterator(0);
while(iterator.hasNext()){
String s = iterator.next();
if(s.equals(\"Ali\")){ // if current element is equal to \"Ali\", then add \"Heba\" at next index
list.add(iterator.nextIndex(), \"Heba\");
break;
}
}
// removing last element
list.remove(list.size()-1);
System.out.println(list);
}
}
/*
Sample output:
Ahmad Ali Sara Omar Eman
[Ahmad, Ali, Heba, Sara, Omar]
*/
2)
difference between next()and hasNext() methods?
next() : return next element
hasNext() : return true if there is more element, else false



