Queue

less than 1 minute read

Queue Using an ArrayList/Array

Queues

Queues print in an [a, b, c] format from front to back

Heap as a Priority Queue

Queue using two Stacks

One Stack to hold values, the second one to hold the poped values from one

private Stack<Integer> main = new Stack<Integer>();
private final Stack<Integer> temp = new Stack<Integer>();

//O(1) operation
public void enque(int item){
    main.push(item);
}

public int deque(){
    int ret;
    if (main.size() >= 1){
        while(main.size == 1){
            temp.push(main.pop());//put the value into the temp Stack
        }
        ret = main.pop(); // retain the value to return
    }
    // Reset the array for further processing (or interchange the stacks)
    while(temp.size() > 0){
        main.push(temp.pop());
    }

    /* Interchanging the Stack
        private Stack<Integer> interchange = null;
        interchange = main;
        main = temp; //Main to set to the filled Stack
        temp = interchange;
    */

    return ret;
}