Question

Hi IN JAVA please Implement an the ADT List-Based ADT Queue and verify ""isEmpty()", "enqueue()", "dequeue()",...

Hi

IN JAVA please Implement an the ADT List-Based ADT Queue and verify ""isEmpty()", "enqueue()", "dequeue()", "dequeueAll()", and "peak()" operations.

please write main method as well and show output

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

//QNode java
package com.study.HomeworkLib;
class QNode {
int key;
QNode next;
//constructor
public QNode(int key)
{
this.key = key;
this.next = null;
}
}//Queuejava
package com.study.HomeworkLib;
class Queue {
QNode front, rear;
public Queue() {
this.front = this.rear = null;
}
void dequeueAll() {
if (IsEmpty())
return;
//just make front and rear null. Java Garbage collector will take care of cleaning memory
this.front = this.rear = null;
}
QNode peak() {
if (IsEmpty())
return null;
return this.front;
}
//Method to add an element to the queue.
void enqueue(int key) {
QNode temp = new QNode(key);
//check if queue is empty
if (this.rear == null) {
this.front = this.rear = temp;
return;
}
//add element ti the end
this.rear.next = temp;
this.rear = temp;
}
boolean IsEmpty() {
return this.front == null;
}
//Method to remove an element from queue.
QNode dequeue() {
//check emptinesss
if (IsEmpty())
return null;
// Store previous front and move front one node ahead
QNode temp = this.front;
this.front = this.front.next;
//handle single node case
if (this.front == null)
this.rear = null;
return temp;
}
}
//Test.java
package com.study.HomeworkLib;
public class Test {
public static void main(String[] args) {
Queue q = new Queue();
q.enqueue(100);
q.enqueue(200);
System.out.println("Element Dequed is :" + q.dequeue().key);
q.dequeue();
q.enqueue(300);
q.enqueue(400);
q.enqueue(500);
System.out.println("Element Dequed is :" +q.dequeue().key);
}
}

PROGRAM SCREENSHOTS:

output:

Add a comment
Know the answer?
Add Answer to:
Hi IN JAVA please Implement an the ADT List-Based ADT Queue and verify ""isEmpty()", "enqueue()", "dequeue()",...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT