[10 points] Add the following node in the same order: “Ali”, “Sara”, “Omar”, “Em
ID: 3779903 • Letter: #
Question
[10 points]
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 type Student. 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.
Explanation / Answer
Q5
1) A car wash facility.
Queue
2) Batteries in Flashlight.
Stack
3) Pringles (pack of chips).
Stack
4) Customers at Al-Baik.
Queue
5) Execution of a recursive method.
Stack
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.