G
G
gleendo2016-11-29 00:40:27
Java
gleendo, 2016-11-29 00:40:27

Why doesn't the queue use the zero index in the array?

Here is the code that implements the queue:

public class Queue {
    private char q[];
    private int putLoc, getLoc;

    public Queue(int size) {
        q = new char[size + 1]; // Зачем создавать массив на единицу больше?
        putLoc = getLoc = 0;
    }

    public Queue(Queue obj) {
        putLoc = obj.putLoc;
        getLoc = obj.getLoc;
        q = new char[obj.q.length];

        for (int i = getLoc + 1; i <= putLoc; i++) { // Почему не используется нулевой индекс в массиве?
            q[i] = obj.q[i];
        }
    }

    public Queue(char a[]) {
        putLoc = 0;
        getLoc = 0;
        q = new char[a.length + 1];

        for (int i = 0; i < a.length; i++) {
            put(a[i]);
        }
    }

    public void put(char ch) {
        if (putLoc == q.length - 1) {
            System.out.println(" - Очередь заполнена!");

            return;
        }

        q[++putLoc] = ch;
    }

    public char get() {
        if (getLoc == putLoc) {
            System.out.println(" - Очередь пуста!");

            return (char) 0;
        }
        return q[++getLoc];
    }
}

Questions:
1 . Why is the zero index in the array not used in this implementation? What is it for?
2. Why does the get method return (char) 0?
return (char) 0:

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question