2
\$\begingroup\$

I have implemented a queue by using a stack data structure. How could I improve it?

package _amazonAskedQuestions.dataStructures;

import java.util.Iterator;
import java.util.Stack;

public class Queue_Stack<T> implements  Iterable<T>{

    Stack<T> stack = new Stack<>();

    public void enqueue(T item){
        Stack<Integer> temp = new Stack<Integer>();
        while(!stack.empty()){
            temp.push((Integer) stack.pop());
        }
        stack.push(item);
        while(!temp.empty()){
            stack.push((T) temp.pop());
        }
    }

    public T dequeue(){
        return stack.pop();

    }

    @Override
    public Iterator<T> iterator() {
        return (Iterator<T>) stack.lastElement();
    }


    public static void main(String args[]){

        Queue_Stack<Integer>  qs = new Queue_Stack<>();

        qs.enqueue(9);
        qs.enqueue(8);
        qs.enqueue(1);
        qs.enqueue(3);
        qs.enqueue(4);
        qs.enqueue(5);

       System.out.println( qs.dequeue());

    }
}
\$\endgroup\$
2
\$\begingroup\$

Your Queue_Stack only works with Integer. I tried using String and got a ClassCastException.

I made some modifications to your code. I added a test to make sure you don't get an error if you dequeue more than you enqueue. I used the underlying iterator to get rid of the temp Stack.

import java.util.Iterator;
import java.util.Optional;
import java.util.Stack;

public class Queue_Stack<T> implements Iterable<T> {

    Stack<T> stack = new Stack<>();

    public void enqueue(T item) {
        stack.push(item);
    }

    public Optional<T> dequeue() {
        if (!stack.empty()) {
            T item = stack.iterator().next();
            stack.remove(0);
            return Optional.of(item);
        } else {
            return Optional.empty();
        }
    }

    @Override
    public Iterator<T> iterator() {
        return (Iterator<T>) stack.iterator();
    }

    public static void main(String args[]) {
        Queue_Stack<String> qs = new Queue_Stack<>();

        qs.enqueue("zeta");
        qs.enqueue("alpha");
        qs.enqueue("beta");
        qs.enqueue("gamma");

        Iterator<String> iter = qs.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }

        System.out.println(qs.dequeue());
        System.out.println(qs.dequeue());
        System.out.println(qs.dequeue());
        System.out.println(qs.dequeue());
        System.out.println(qs.dequeue());
    }
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Gilbert Le Blanc's answer already covered all the aspects of your question, I'm adding just one consideration about the Stack class from documentation:

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. 

So instead of a Stack instance stack in your code you should consider to use :

Deque<T> stack = new ArrayDeque<>();
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.