Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education