Professional Documents
Culture Documents
Name: Aditya Kumar Roll No: 02 (O) University Roll No: 191500057
Name: Aditya Kumar Roll No: 02 (O) University Roll No: 191500057
Question No 20 Write a program to copy all the elements of one queue into another
queue in reverse using stack and print both the queues.
Solution
/*
Name: Aditya Kumar
Section: O
University Roll No.: 191500057
Class Roll No.: 02
*/
import java.util.Stack;
Node head;
Node tail;
int size;
public QueueUsingLL() {
head = null;
tail = null;
size = 0;
}
public int getSize() {
return size;
}
public boolean isEmpty() {
return (size==0);
}
public void enqueue(int data) {
QueueUsingLL.Node newnode = new QueueUsingLL.Node(data);
if(head==null){
head = newnode;
tail = newnode;
}
else{
tail.next = newnode;
tail = newnode;
}
size++;
}
public int dequeue() {
if(head == null){
return -1;
}
int data = head.data;
head = head.next;
size--;
return data;
}
public int front() {
if(head==null){
return -1;
}
return head.data;
}
public void moveToRear(){
QueueUsingLL.Node curr = head;
if(head == null){
System.out.println("Not possible queue is empty");
return;
}
head = head.next;
tail.next = curr;
tail = curr;
while(!st.isEmpty()){
q2.enqueue(st.pop());
}
System.out.println("\nAfter copying");
while(!q2.isEmpty()){
System.out.print(q2.dequeue()+" ");
}
}
}