can i have the code in java Introduction: A Queue (or line) helps people to be s
ID: 3782749 • Letter: C
Question
can i have the code in java Introduction: A Queue (or line) helps people to be served in the order they request a service; For example, people form a Queue (line) as they enter the post office. When a person arrives, he or she goes to the “End of the Queue (line)” and the person at the “Front of the Queue (line)” is the person who is served next. Question: The Queue.java class simulates a queue of integer numbers. A new integer is added to the end of the queue using the addToEndOfQueue(int val) method. The getNext() method returns the next integer in front of the queue. The size() method returns the number of integers currently in the queue. 1- Complete the following methods in Queue.java: a) public Queue() creates a queue (The constructor method). b) public void addToEndOfQueue(int val) adds the integer val to the end of the queue. c) public int getNext() removes the next integer from the front of the queue and returns it. d) public int size() returns the number of elements (integer numbers) that are currently in the queue. 2- Run QueueDemo.java and verify that the following output is generated: Current Queue size: 3 Next number in the queue: 10 Current Queue size: 2 Next number in the queue: 5 Next number in the queue: 18 Current Queue size: 0
Explanation / Answer
/*************************Queue.java*******************/
class Queue{
//variable declaration
int val;
Queue next;
static int size=0;
Queue head=null;
//default constructor
public Queue(){
}
/**
*This method is used for adding element at end of the queue
*/
public void addToEndOfQueue(int val){
//if head node is null
if(head==null){
head=new Queue();
head.val=val;
head.next=null;
size++;
}else{//adding element at the end of the queue
Queue temp=head;
//traversing up to last element in queue
while(temp.next!=null){
temp=temp.next;
}
//creating new node to be add at the last
Queue newNode=new Queue();
newNode.val=val;
newNode.next=null;
temp.next=newNode;
//increasing size of list
size++;
}
}
//Returning next element from Queue
public int getNext(){
Queue temp=head;
head=head.next;
size--;
return temp.val;
}
//returning size of Queue
public int size(){
return size;
}
}
/*********************QueueDemo.java****************/
class QueueDemo
{
public static void main (String[] args) {
//Queue Object Creation
Queue front=new Queue();
//Adding Element at the end of Queue
front.addToEndOfQueue(10);
front.addToEndOfQueue(5);
front.addToEndOfQueue(18);
System.out.println("Current Queue size:
"+front.size());
System.out.println("Next number in the queue:
"+front.getNext());
System.out.println("Current Queue size:
"+front.size());
System.out.println("Next number in the queue:
"+front.getNext());
System.out.println("Current Queue size:
"+front.size());
System.out.println("Next number in the queue:
"+front.getNext());
System.out.println("Current Queue size:
"+front.size());
}
}
/**********************output**********************/
D:UserssurvivalDesktop>javac QueueDemo.java
D:UserssurvivalDesktop>java QueueDemo
Current Queue size: 3
Next number in the queue: 10
Current Queue size: 2
Next number in the queue: 5
Current Queue size: 1
Next number in the queue: 18
Current Queue size: 0
Thanks a lot
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.