Draw a UML class diagram for the following code: class Node {     E data;     Node next;       public Node(E data) {         this.data = data;         this.next = null;     } }   class Queue {    Node front;    Node rear;      public Queue() {        front = null;        rear = null;    }    public boolean isEmpty() {        return front == null;    }    public void enqueue(E item) {        Node newNode = new Node(item);        if (isEmpty()) {            front = newNode;            rear = newNode;        } else {            rear.next = newNode;            rear = newNode;        }    }    public E dequeue() {        if (isEmpty()) {            System.out.println("Queue is empty.");            return null;        } else {            E item = front.data;            front = front.next;            if (front == null) {                rear = null;            }            return item;        }    }    public void dequeueAll() {        front = null;        rear = null;    }    public E peek() {        if (isEmpty()) {            System.out.println("Queue is empty.");            return null;        } else {            return front.data;        }    } }   public class Main {    public static void printQueue(Queue queue) {        Node currentNode = queue.front;        System.out.print("Queue: ");        while (currentNode != null) {            System.out.print(currentNode.data + " ");            currentNode = currentNode.next;        }        System.out.println();    }      public static void main(String[] args) {        Queue queue = new Queue();        System.out.println("Is the queue empty? " + queue.isEmpty());        printQueue(queue);        queue.enqueue(1);        System.out.println("Enqueued: 1");        printQueue(queue);                queue.enqueue(2);        System.out.println("Enqueued: 2");        printQueue(queue);        queue.enqueue(3);        System.out.println("Enqueued: 3");        printQueue(queue);                System.out.println("Is the queue empty? " + queue.isEmpty());        printQueue(queue);        System.out.println("Front of the queue: " + queue.peek());        printQueue(queue);        System.out.println("Dequeue: " + queue.dequeue());         printQueue(queue);                System.out.println("Front of the queue: " + queue.peek());        printQueue(queue);        queue.dequeueAll();        System.out.println("Dequeued all items");        printQueue(queue);                System.out.println("Is the queue empty? " + queue.isEmpty());    } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Draw a UML class diagram for the following code:

class Node<E> {

    E data;

    Node<E> next;

 

    public Node(E data) {

        this.data = data;

        this.next = null;

    }

}

 

class Queue<E> {

   Node<E> front;

   Node<E> rear;

 

   public Queue() {

       front = null;

       rear = null;

   }

   public boolean isEmpty() {

       return front == null;

   }

   public void enqueue(E item) {

       Node<E> newNode = new Node<E>(item);

       if (isEmpty()) {

           front = newNode;

           rear = newNode;

       } else {

           rear.next = newNode;

           rear = newNode;

       }

   }

   public E dequeue() {

       if (isEmpty()) {

           System.out.println("Queue is empty.");

           return null;

       } else {

           E item = front.data;

           front = front.next;

           if (front == null) {

               rear = null;

           }

           return item;

       }

   }

   public void dequeueAll() {

       front = null;

       rear = null;

   }

   public E peek() {

       if (isEmpty()) {

           System.out.println("Queue is empty.");

           return null;

       } else {

           return front.data;

       }

   }

}

 

public class Main {

   public static void printQueue(Queue<Integer> queue) {

       Node<Integer> currentNode = queue.front;

       System.out.print("Queue: ");

       while (currentNode != null) {

           System.out.print(currentNode.data + " ");

           currentNode = currentNode.next;

       }

       System.out.println();

   }

 

   public static void main(String[] args) {

       Queue<Integer> queue = new Queue<Integer>();

       System.out.println("Is the queue empty? " + queue.isEmpty());

       printQueue(queue);

       queue.enqueue(1);

       System.out.println("Enqueued: 1");

       printQueue(queue);

       

       queue.enqueue(2);

       System.out.println("Enqueued: 2");

       printQueue(queue);

       queue.enqueue(3);

       System.out.println("Enqueued: 3");

       printQueue(queue);

       

       System.out.println("Is the queue empty? " + queue.isEmpty());

       printQueue(queue);

       System.out.println("Front of the queue: " + queue.peek());

       printQueue(queue);

       System.out.println("Dequeue: " + queue.dequeue()); 

       printQueue(queue);

       

       System.out.println("Front of the queue: " + queue.peek());

       printQueue(queue);

       queue.dequeueAll();

       System.out.println("Dequeued all items");

       printQueue(queue);

       

       System.out.println("Is the queue empty? " + queue.isEmpty());

   }

}

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Eight puzzle problem
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education