Question

In: Computer Science

Program in Java Create a queue class to store integers and implement following methods: 1- void...

Program in Java

Create a queue class to store integers and implement following methods:

1- void enqueue(int num): This method will add an integer to the queue (end of the queue).

2- int dequeue(): This method will return the first item in the queue (First In First Out).

3- void display(): This method will display all items in the queue (First item will be displayed first).

4- Boolean isEmpty(): This method will check the queue and if it is empty, this will return true, otherwise false.

Solutions

Expert Solution


class Queue {
        private static int front=0, rear=0, capacity=50;
        private static int queue[]=new int[50];;
        // function to insert an element
        // at the rear of the queue
        void enqueue(int data) {
                // check queue is full or not
                if (capacity == rear) {
                        System.out.printf("\nQueue is full\n");
                        return;
                }

                // insert element at the rear
                else {
                        queue[rear] = data;
                        rear++;
                }
                return;
        }

        boolean isEmpty() {
                return (front == rear);
        }

        

        // function to delete an element
        // from the front of the queue
        void dequeue() {
                // if queue is empty
                if (front == rear) {
                        System.out.printf("\nQueue is empty\n");
                        return;
                }

                else {
                        for (int i = 0; i < rear - 1; i++) {
                                queue[i] = queue[i + 1];
                        }

                        // store 0 at rear indicating there's no element
                        if (rear < capacity)
                                queue[rear] = 0;

                        // decrement rear
                        rear--;
                }
                return;
        }

        public void display() {
                int i;

                for (i = front; i < rear; i++) {
                        System.out.print(queue[i] + " ");
                }
                System.out.println();
        }

}

public class TestQueue {

        public static void main(String ar[]) {
                Queue q = new Queue();

                // inserting elements in the queue
                System.out.println(q.isEmpty());
                q.enqueue(10);
                q.enqueue(20);
                q.enqueue(30);
                q.enqueue(40);
                q.enqueue(50);
                q.display();
                q.dequeue();
                q.dequeue();
                q.display();
                System.out.println(q.isEmpty());
        }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

Please Like and Support me as it helps me a lot


Related Solutions

Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
C++ program homework question 1 1. Create and implement a class called clockType with the following...
C++ program homework question 1 1. Create and implement a class called clockType with the following data and methods (60 Points.): Data: Hours, minutes, seconds Methods: Set and get hours Set and get minutes Set and get seconds printTime(…) to display time in the form of hh:mm:ss default and overloading constructor Overloading Operators: << (extraction) operator to display time in the form of hh:mm:ss >> (insertion) operator to get input for hours, minutes, and seconds operator+=(int x) (increment operator) to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT