1. Given a class ‘Node’ and ‘Queue’, that contains the below information diagram
ID: 3686526 • Letter: 1
Question
1. Given a class ‘Node’ and ‘Queue’, that contains the below information diagram.
Implement add(String), remove() and element() methods in the Queue class which is provided to you,
The methods should work as:
add(String item) It has to take a ‘new item (String)’ and add it to the tail of a queue, if there are no nodes, then the ‘new item (String)’ should the first node
remove() It has to remove and return the ‘first Item’ from the Queue, if there are no items then return ‘NoSuchElementException’.
element() It has to return the ‘first Item’ from the Queue, if there are no items then return ‘NoSuchElementException’.
Size() It should return the size of the Queue
Iterate() It should traverse the queue from head to tail.
If you use the following main method:
public static void main(String[] args) {
list.add("Book");
list.add("Lappy");
System.out.println("Queue Length : "+list.size());
System.out.println("Peek or Element in Queue : "+list.element());
System.out.println("List Of Elements in Queue :");
list.iterate();
System.out.println("Remove element from Queue : "+list.remove()); System.out.println("Queue Length : "+list.size());
System.out.println("Peek or Element in Queue : "+list.element()); list.add("Computer"); System.out.println("Length : "+list.size());
System.out.println("List Of Elements in Queue : "); list.iterate(); System.out.println("Remove element from Queue : "+list.remove()); System.out.println("Length : "+list.size()); System.out.println("Remove element from Queue : "+list.remove()); System.out.println("Length : "+list.size()); System.out.println("Remove element from Queue : "+list.remove()); System.out.println("Length : "+list.size()); }
Explanation / Answer
########### Node.java ###################
public class Node {
private String name = "";
private Node next;
public Node(String name) {
this.name = name;
this.next = null;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return " Name : "+this.name;
}
}
################################ Queue.java ####################################
import java.util.NoSuchElementException;
public class Queue {
private int size = 0;
private Node root = null;
/*
* It has to return the size of the Queue
*
* @return size
*/
public int size() {
return size;
}
/*
* The method should add an element to the tail of the queue
*
* @Param - String, New element to the root.
*/
public void add(String item) {
Node newNode = new Node(item);
if(root == null)
root = newNode;
else{
Node temp = root;
while(temp.getNext() != null)
temp = temp.getNext();
temp.setNext(newNode);
}
size++;
}
/*
* The method should remove and return the head of the queue
*
* @Return - NoSuchElementException, if the queue is empty
* - String,if the queue contains elements
*/
public String remove() {
if(root == null)
throw new NoSuchElementException();
Node temp = root;
root = root.getNext();
size--;
return temp.getName();
}
/*
* The method should return the head of the queue
*
* @Return - NoSuchElementException, if the queue is empty
* - String,if the queue contains elements
*/
public String element() {
if(root == null)
throw new NoSuchElementException();
return root.getName();
}
/*
* is the Queue empty?
*
*/
public boolean isEmpty() { return root == null; }
/**
* Start with the head and traverse till you reach nul l.
*/
public void iterate(){
Node temp = root;
while(temp != null){
System.out.println(temp.toString());
temp = temp.getNext();
}
}
}
###################### Main.java#########################
public class Main {
public static void main(String[] args) {
Queue list = new Queue();
list.add("Book");
list.add("Lappy");
System.out.println("Queue Length : "+list.size());
System.out.println("Peek or Element in Queue : "+list.element());
System.out.println("List Of Elements in Queue : ");
list.iterate();
System.out.println("Remove element from Queue : "+list.remove());
System.out.println("Queue Length : "+list.size());
System.out.println("Peek or Element in Queue : "+list.element());
list.add("Computer");
System.out.println("Length : "+list.size());
System.out.println("List Of Elements in Queue : ");
list.iterate();
System.out.println("Remove element from Queue : "+list.remove());
System.out.println("Length : "+list.size());
System.out.println("Remove element from Queue : "+list.remove());
System.out.println("Length : "+list.size());
System.out.println("Remove element from Queue : "+list.remove());
System.out.println("Length : "+list.size());
}
}
/*
Output:
Queue Length : 2
Peek or Element in Queue : Book
List Of Elements in Queue :
Name : Book
Name : Lappy
Remove element from Queue : Book
Queue Length : 1
Peek or Element in Queue : Lappy
Length : 2
List Of Elements in Queue :
Name : Lappy
Name : Computer
Remove element from Queue : Lappy
Length : 1
Remove element from Queue : Computer
Length : 0
Exception in thread "main" java.util.NoSuchElementException
at firstWeek.Queue.remove(Queue.java:51)
at firstWeek.Main.main(Main.java:32)
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.