Question

Write a Java program that traces the following methods for a linked list queue, starting from...

Write a Java program that traces the following methods for a linked list queue, starting from an empty queue and performing them in sequence:

  • 1: enqueue("C")
  • 2: enqueue("D")
  • 3: dequeue()
  • 4: dequeue()
0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.lang.*;
class LinkList
{
char ch;
LinkList next;
  
// constructor to create a new node
public LinkList(char c)
{
this.ch = c;
this.next = null;
}
}
//class queue using link list
class QueueTest
{
LinkList front, rear;
//default constructor
public QueueTest()
{
this.front = this.rear = null;
}
  
//method to insert a character into queue
void enqueue(char c)
{
  
// creaet a linklist object
LinkList lobj = new LinkList(c);
  
// condition for insert a first element
if (this.rear == null) {
this.front = this.rear = lobj;
return;
}
  
// insert the node at the end of queue
this.rear.next = lobj;
this.rear = lobj;
}
  
// Function to delete an element from queue
LinkList dequeue()
{
// condition for empty queue
if (this.front == null)
return null;
  
  
LinkList lobj = this.front;
this.front = this.front.next;
  
//link list having single element
if (this.front == null)
this.rear = null;
return lobj;
}
}
public class Test
{

   //driver program
public static void main(String[] args)
{
   //declare the queue object
QueueTest qobj = new QueueTest();
       //inpsert c into queue
qobj.enqueue('c');
//insert d into queue      
qobj.enqueue('d');
//perform dequeue() operation
System.out.println("Dequeued item is " + qobj.dequeue().ch);
       System.out.println("Dequeued item is " + qobj.dequeue().ch);
  
         
}
}

output

Add a comment
Know the answer?
Add Answer to:
Write a Java program that traces the following methods for a linked list queue, starting from...
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