Skip to content

Instantly share code, notes, and snippets.

@anil477
Created May 29, 2017 08:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/521803a8052b3de32188b77fcb4c525e to your computer and use it in GitHub Desktop.
Save anil477/521803a8052b3de32188b77fcb4c525e to your computer and use it in GitHub Desktop.
Reverse Queue Using Recursion
import java.util.*;
import java.lang.*;
class StackOneQueue{
public Queue<Integer> queue;
public StackOneQueue()
{
this.queue = new LinkedList<Integer>();
}
public void push(int data) {
this.queue.add(data);
}
public void pop() {
this.queue.remove();
}
public void peek() {
System.out.println("Element on top: " + this.queue.element());
}
public void reverse(){
Integer temp;
if(!queue.isEmpty()) {
temp = this.queue.remove();
this.reverse();
this.queue.add(temp);
}
}
public void printQueue()
{
System.out.println(this.queue.toString());
}
}
class StackUsingQueueApp {
public static void main(String[] args) {
StackOneQueue obj = new StackOneQueue();
obj.push(1);
obj.push(2);
obj.push(3);
obj.push(4);
obj.push(5);
obj.printQueue();
obj.reverse();
obj.printQueue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment