O
O
Oleg2017-03-11 17:44:11
Java
Oleg, 2017-03-11 17:44:11

Data structure. How to implement a queue in Java (Queue)?

I have such a task. There is a Key (for example, 10 digits). I need to put it in the queue and take the first element from the queue N times, do an operation on it and put it back in the queue and so on a very large number of times.
What is the problem? - The implementation of the queue that I wrote only works up to a certain point. That is, all objects of the Node class are stored in memory, and when the memory is full, the program freezes and is not executed further.
Here is an example, can someone explain to me how to fix this?
Main class

import java.io.IOException;
public class Main {
    public static void main(String[] args) throws IOException {
        DynamicQueue<Integer> queue = new DynamicQueue<>();
        int[] key = {4,2,2,5,6,1,4,2,3};
        for(int i=0; i<key.length; i++){
            queue.enqueue(i);
        }
        int a;
        for(int i=0; i<1_000_000_000; i++){
            a = queue.dequeue();
            queue.enqueue(a);
        }
        System.out.println("finish");
    }
}

DynamicQueue class
import java.io.IOException;
public class DynamicQueue<T> {
    private Node<T> back;
    private Node<T> front;
    private int size;
    public DynamicQueue() {
        back = null;
        size = 0;
    }
    public void enqueue(T e) {
        if (isEmpty()){
            Node<T> n = new Node<T>(null, null, e);
            back = n;
            front = n;
            size++;
        }
        else {
            Node<T> n = new Node<T>(null, front, e);
            front.setNext(n);
            size++;
            front = n;
        }
    }
    public T dequeue() throws IOException {
        if (!isEmpty()){
            T aux = (T) back.getElement();
            size--;
            back = back.getNext();
            return aux;
        }
        else throw new NullPointerException();
    }
    public boolean isEmpty() {
        return size == 0;
    }
    public int size() {
        return size;
    }
    public boolean isFull() {
        return false;
    }
}

Node class
public class Node<T> {
    private Node<T> next;
    private Node<T> prev;
    private T element;
    public Node(Node<T> prox, Node<T> ant, T element) {
        this.next = prox;
        this.prev = ant;
        this.element = element;
    }
    public Node<T> getNext() {
        return next;
    }
    public void setNext(Node<T> prox) {
        this.next = prox;
    }
    public Node<T> getPrev() {
        return prev;
    }
    public void setPrev(Node<T> ant) {
        this.prev = ant;
    }
    public T getElement() {
        return element;
    }
    public void setElement(T element) {
        this.element = element;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Axian Ltd., 2017-03-11
@AxianLTD

The first commandment of a programmer is not to reinvent the wheel. There are a bunch of out-of-the-box implementations for managing queues. For example https://www.rabbitmq.com/tutorials/tutorial-one-ja... And here about all more or less significant projects of queues.io queue managers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question