Answer to Question #182211 in Java | JSP | JSF for Sawaira

Question #182211

How we Implement a generic Queue class in java and work with different data types (complete code)


1
Expert's answer
2021-04-17T12:58:32-0400
//DLLQueue.java
import java.util.LinkedList;

public class DLLQueue<T> {
        // using java.util.LinkedList as underlying data structure (as specified in
        // the question)
        private LinkedList<T> queue;

        // constructor, initializing underlying queue
        public DLLQueue() {
                queue = new LinkedList<T>();
        }

        // adds an element to the last of the queue
        public boolean enqueue(T elem) {
                queue.addLast(elem);
                // returning true if no issues occur
                return true;
        }

        // removes and returns the front element
        public T dequeue() {
                // returning null if queue is empty
                if (queue.isEmpty()) {
                        return null;
                }
                // else removing and returning first element
                return queue.removeFirst();
        }

        // returns front element without removing
        public T peek() {
                // returning null if queue is empty
                if (queue.isEmpty()) {
                        return null;
                }
                // else fetching and returning first element
                return queue.getFirst();
        }

        // method to display queue contents
        public void showAll() {
                // using default toString implementation of the queue.
                // if you want to print everything by iterating through elements, I can
                // update the code. let me know if you need that.
                System.out.println(queue);
        }

        // method not mentioned in the diagram, but felt this is an important method
        // not to ignore. returns true if queue is empty, else false
        public boolean isEmpty() {
                return queue.isEmpty();
        }
}


//Test.java
public class Test {
        public static void main(String[] args) {
                // creating a queue
                DLLQueue<Integer> q = new DLLQueue<Integer>();
                // adding numbers from 1 to 10
                for (int i = 1; i <= 10; i++) {
                        q.enqueue(i);
                }
                // printing queue
                q.showAll();
                // looping and removing each element from queue until queue is empty
                while (!q.isEmpty()) {
                        // displaying front element
                        System.out.println(q.peek());
                        // removing front element
                        q.dequeue();
                }

        }
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog