Question

Write a class that implements the Queue interface, as shown below. A queue is a data...

Write a class that implements the Queue interface, as shown below. A queue is a data structure that accepts data and then returns it in the order in which it was received (first-in, first-out order). Items are added to the tail of the queue and removed from the head.

Your queue implementation must be accessible and usable from any package. However, any attempt to extend your class should produce a compile-time error.

*In java*

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

Please find my implementation.

Please let me know in case of any issue.

final public class Queue

{

   //A linked list (LL) node to store a queue entry

   class QNode

   {

       int key;

       QNode next;

       // constructor to create a new linked list node

       public QNode(int key) {

           this.key = key;

           this.next = null;

       }

   }

  

   QNode front, rear;

   public Queue() {

       this.front = this.rear = null;

   }

   // Method to add an key to the queue.

   void enqueue(int key)

   {

       // Create a new LL node

       QNode temp = new QNode(key);

       // If queue is empty, then new node is front and rear both

       if (this.rear == null)

       {

           this.front = this.rear = temp;

           return;

       }

       // Add the new node at the end of queue and change rear

       this.rear.next = temp;

       this.rear = temp;

   }

   // Method to remove an key from queue.

   QNode dequeue()

   {

       // If queue is empty, return NULL.

       if (this.front == null)

           return null;

       // Store previous front and move front one node ahead

       QNode temp = this.front;

       this.front = this.front.next;

       // If front becomes NULL, then change rear also as NULL

       if (this.front == null)

           this.rear = null;

       return temp;

   }

}

###########

public class QueueTest

{

   public static void main(String[] args)

   {

       Queue q=new Queue();

       q.enqueue(10);

       q.enqueue(20);

       q.dequeue();

       q.dequeue();

       q.enqueue(30);

       q.enqueue(40);

       q.enqueue(50);

       System.out.println("Dequeued item is "+ q.dequeue().key);

   }

}

Add a comment
Know the answer?
Add Answer to:
Write a class that implements the Queue interface, as shown below. A queue is a data...
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
  • Create a CircularArrayQueue<E> implementation of the Queue<E> interface defined in class. Hints: You will need two...

    Create a CircularArrayQueue<E> implementation of the Queue<E> interface defined in class. Hints: You will need two index variables to keep track of the start and the end of the queue. Consider the queue to be empty when the front/back index pointers are equal (which means that the 'back' pointer will always be pointing at the next place to add an item). Be careful of index increments and decrements; make sure you perform the operations necessary to make the pointers loop...

  • Your assignment is to create and test a class for a queue of objects. You may...

    Your assignment is to create and test a class for a queue of objects. You may use any object class of your choice as the data for the queue.   The instances of the class should have at least one field that distinguishes each instance from other instances of the class (key property, also called a key field). You should complete the software to implement and test your queue and submit the software along with a project report. Your queue class...

  • In a file named LLBag.java, write a class called LLBag that implements the Bag interface using...

    In a file named LLBag.java, write a class called LLBag that implements the Bag interface using a linked list instead of an array. You may use a linked list with or without a dummy head node. Bag interface code: /* * Bag.java * * Computer Science 112, Boston University */ /* * An interface for a Bag ADT. */ public interface Bag { /*    * adds the specified item to the Bag. Returns true on success    * and...

  • CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class....

    CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...

  • Given the Interface Code write a java class that implements this interface and show the working...

    Given the Interface Code write a java class that implements this interface and show the working functionality in the main method: public interface CustomList<T> { /** * This method should add a new item into the <code>CustomList</code> and should * return <code>true</code> if it was successfully able to insert an item. * @param item the item to be added to the <code>CustomList</code> * @return <code>true</code> if item was successfully added, <code>false</code> if the item was not successfully added (note: it...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have...

    q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have a public constructor that takes a JTextArea and a JLabel as parameters and stores these in instance variables. Override the mouseEntered method to . display the text from the JTextArea on the JLabel in all upper case letters. Then, override the mouseReleased method to display the text from the JTextArea on the JLabel in all lower case letters. The other three methods from the...

  • This class implements a doubly linked list in which the nodes are of the class DLNode....

    This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...

  • In java, create class BNode(T), which implements the interface TreeNode(T) interface methods: void setRight(TreeNode(T) right) -...

    In java, create class BNode(T), which implements the interface TreeNode(T) interface methods: void setRight(TreeNode(T) right) - sets right side of node, where right is the node to the right of this node void setRight(TreeNode(T) left) - sets left side of node, where left is the node to the left of this node TreeNode getRight() - gets the right node TreeNode getLeft() - gets the left node T getData() - gets the data within the node THEN, create class BT(E), which...

  • You must implement a BlockedList class that implements the List interface. You may use any of...

    You must implement a BlockedList class that implements the List interface. You may use any of the classes in JCF or in the textbook code. The constructor for this class takes an integer block size b and the implementation should have the following performance characteristics: a) get(i) and set(i,x) should run in O(1) time per operation b) add(i,x) and remove(i) should run in O(b+ min{i, n-i}/b) amortized time per operation. Any solution matching this specification is acceptable. However, the runtime...

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