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());
 }
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Apr 23, 2020 at 14:45
\$\endgroup\$
0

2 Answers 2

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());
 }
}
answered Apr 23, 2020 at 15:43
\$\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<>();
answered Apr 24, 2020 at 8:55
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.