Implement the queue ADT using only stacks and the methods pop(), push() and empty() of the java.util.Stack class. We know that Stack is LIFO(Last In First Out). Queue is FIFO(First In First Out). Everything is the same in these two class except the pop() method. In Stack, We pop the element on the top of stack. In queue, we "pop" the front element which is inserted in queue first. This is the only difference between these two structure. Now we can use pop() method in Stack to do deletion in Queue.
Here is a Main program which is used for testing program above.
/*****************************
* Implement Queue by using
* Stack
* Author: QT_pixy
* Date: Feb. 5, 07
*****************************/

public class Main4
{
public static void main(String[] args)
{
q<Integer> a_q = new q<Integer>();

a_q.enqueue(new Integer(3));
a_q.enqueue(new Integer(9));

System.out.println(a_q.dequeue()); // print 3
System.out.println(a_q.dequeue()); // print 9

try
{
Integer a = a_q.dequeue();
System.out.println(a);
}
catch (Exception e)
{
System.err.println(e);
}

}
}
/*****************************
* Implement Queue by using
* Stack
* Author: QT_pixy
* Date: Feb. 5, 07
*****************************/
/*
Implement the queue ADT using only stacks and the methods pop(), push() and empty() of the java.util.Stack class.
*/
import java.util.Stack;
public class q<AnyType>
{
private Stack<AnyType> theItems; /*the queue*/
private int size;
public q()
{
theItems = new Stack<AnyType>();
size = 0;
}
public void enqueue(AnyType e)
{
theItems.push(e);
size++;
}
public AnyType dequeue()
{
if(size == 0)
throw new QueueEmptyException("Empty Queue");
Stack<AnyType> tempItems = new Stack<AnyType>();
for(int i = 0; i < this.size(); i++)
tempItems.push(theItems.pop());
AnyType temp = tempItems.pop();
for(int i = 0; i < this.size() - 1; i++)
theItems.push(tempItems.pop());
size--;
return temp;
}
public int size()
{
return size;
}
public boolean isEmpty()
{
return theItems.empty();
}
public AnyType front()
{
if(size == 0)
throw new QueueEmptyException("Empty Queue");
Stack<AnyType> tempItems = new Stack<AnyType>();
for(int i = 0; i < this.size(); i++)
tempItems.push(theItems.pop());
AnyType temp = tempItems.peek();
for(int i = 0; i < this.size(); i++)
theItems.push(tempItems.pop());
return temp;
}
}
* Implement Queue by using
* Stack
* Author: QT_pixy
* Date: Feb. 5, 07
*****************************/
/*
Implement the queue ADT using only stacks and the methods pop(), push() and empty() of the java.util.Stack class.
*/
import java.util.Stack;
public class q<AnyType>
{
private Stack<AnyType> theItems; /*the queue*/
private int size;
public q()
{
theItems = new Stack<AnyType>();
size = 0;
}
public void enqueue(AnyType e)
{
theItems.push(e);
size++;
}
public AnyType dequeue()
{
if(size == 0)
throw new QueueEmptyException("Empty Queue");
Stack<AnyType> tempItems = new Stack<AnyType>();
for(int i = 0; i < this.size(); i++)
tempItems.push(theItems.pop());
AnyType temp = tempItems.pop();
for(int i = 0; i < this.size() - 1; i++)
theItems.push(tempItems.pop());
size--;
return temp;
}
public int size()
{
return size;
}
public boolean isEmpty()
{
return theItems.empty();
}
public AnyType front()
{
if(size == 0)
throw new QueueEmptyException("Empty Queue");
Stack<AnyType> tempItems = new Stack<AnyType>();
for(int i = 0; i < this.size(); i++)
tempItems.push(theItems.pop());
AnyType temp = tempItems.peek();
for(int i = 0; i < this.size(); i++)
theItems.push(tempItems.pop());
return temp;
}
}
Here is a Main program which is used for testing program above.
/*****************************
* Implement Queue by using
* Stack
* Author: QT_pixy
* Date: Feb. 5, 07
*****************************/
public class Main4
{
public static void main(String[] args)
{
q<Integer> a_q = new q<Integer>();
a_q.enqueue(new Integer(3));
a_q.enqueue(new Integer(9));
System.out.println(a_q.dequeue()); // print 3
System.out.println(a_q.dequeue()); // print 9
try
{
Integer a = a_q.dequeue();
System.out.println(a);
}
catch (Exception e)
{
System.err.println(e);
}
}
}


浙公网安备 33010602011771号