Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Everything in your code is static. While this is enough to make your code work, you should generally try to minimize the use of static variables and static methods minimize the use of static variables and static methods and instead use classes and objects. Because everything in your code is static, you can't create a second QueueUsingStack since the stacks are global and do not differ per Queue.

Everything in your code is static. While this is enough to make your code work, you should generally try to minimize the use of static variables and static methods and instead use classes and objects. Because everything in your code is static, you can't create a second QueueUsingStack since the stacks are global and do not differ per Queue.

Everything in your code is static. While this is enough to make your code work, you should generally try to minimize the use of static variables and static methods and instead use classes and objects. Because everything in your code is static, you can't create a second QueueUsingStack since the stacks are global and do not differ per Queue.

Source Link

Use OOP

Everything in your code is static. While this is enough to make your code work, you should generally try to minimize the use of static variables and static methods and instead use classes and objects. Because everything in your code is static, you can't create a second QueueUsingStack since the stacks are global and do not differ per Queue.

Your code can easily be rewritten to make use of OOP:

public class QueueUsingStack {
 private Stack<Integer> s1 = new Stack<>();
 private Stack<Integer> s2 = new Stack<>();
 public void enqueue(int element){
 s1.push(element);
 System.out.println(element + " inserted");
 }
 public void dequeue(){
 if(s2.isEmpty()) {
 while (!s1.isEmpty()) {
 s2.push(s1.pop());
 }
 }
 System.out.println(s2.pop() + " removed");
 }
 public static void main(String args[]) {
 QueueUsingStack q = new QueueUsingStack(); 
 q.enqueue(23);
 q.enqueue(223);
 q.enqueue(423);
 q.dequeue();
 ...
 }
}

Use Generics

Your Queue can only hold Integers. If you want your Queue to support other objects as well, you should use Java Generics.

Rewriting your code to make use of Generics would look like this:

public class QueueUsingStack<T> {
 private Stack<T> s1 = new Stack<>();
 private Stack<T> s2 = new Stack<>();
 public void enqueue(T element){
 s1.push(element);
 System.out.println(element + " inserted");
 }
 public void dequeue(){
 if(s2.isEmpty()) {
 while (!s1.isEmpty()) {
 s2.push(s1.pop());
 }
 }
 System.out.println(s2.pop() + " removed");
 }
 public static void main(String args[]) {
 QueueUsingStack<Integer> integerQueue = new QueueUsingStack<>(); 
 integerQueue.enqueue(23);
 integerQueue.enqueue(223);
 integerQueue.enqueue(423);
 integerQueue.dequeue();
 ...
 QueueUsingStack<String> stringQueue = new QueueUsingStack<>();
 stringQueue.enqueue("string");
 }
}

Furthermore, you're omitting the type when creating the stacks in your code.

Stack<Integer> s1 = new Stack();

should be:

Stack<Integer> s1 = new Stack<Integer>();

If you're you using Java 7 and up you can also make use of the Diamond operator:

Stack<Integer> s1 = new Stack<>();

Naming

Class names should start with an upper case letter in Java.

lang-java

AltStyle によって変換されたページ (->オリジナル) /