java: implement a class queue
Implement a class Queue which supports the following operations:
boolean isEmpty()
Returns true if the queue is empty, and false otherwise.
int peak()
Returns the value at the front of a non-empty queue without deleting it.
int pop()
Removes the item at the front of a non-empty queue and returns it.
void push(int o)
Inserts the argument o at the back of the queue.
int size()
Returns the total number of elements in the queue.
You can use an array as your basic data structure and set its size to 100, i.e. assuming that the
user will never enter more than 100 items.
Your not allowed to move around any elements inside the array. Instead use two integers front, size
which indicates the front and length of the queue. Then wrap around at the end of the array when
accessing elements, using the mod operator.
View Answers
October 10, 2012 at 3:36 PM
Here is a code that implements a class Queue.
public class Queue{
public Queue( )
{
this( DEFAULT_CAPACITY );
}
public Queue( int capacity )
{
theArray = new Object[ capacity ];
makeEmpty( );
}
public boolean isEmpty( )
{
return currentSize == 0;
}
public boolean isFull( )
{
return currentSize == theArray.length;
}
public void makeEmpty( )
{
currentSize = 0;
front = 0;
back = -1;
}
public Object getFront( )
{
if( isEmpty( ) )
return null;
return theArray[ front ];
}
public Object dequeue( )
{
if( isEmpty( ) )
return null;
currentSize--;
Object frontItem = theArray[ front ];
theArray[ front ] = null;
front = increment( front );
return frontItem;
}
public void enqueue( Object x ) throws Overflow
{
if( isFull( ) )
throw new Overflow( );
back = increment( back );
theArray[ back ] = x;
currentSize++;
}
private int increment( int x )
{
if( ++x == theArray.length )
x = 0;
return x;
}
private Object [ ] theArray;
private int currentSize;
private int front;
private int back;
static final int DEFAULT_CAPACITY = 10;
public static void main( String [ ] args )
{
Queue q = new Queue( );
try
{
for( int i = 0; i < 10; i++ )
q.enqueue( new MyInteger( i ) );
}
catch( Overflow e ) { System.out.println( "Unexpected overflow" ); }
while( !q.isEmpty( ) )
System.out.println( q.dequeue( ) );
}
}
Ads
Related Tutorials/Questions & Answers:
java: implement a class queue
java:
implement a
class queue
Implement a
class Queue which supports the following operations:
boolean isEmpty()
Returns true if the
queue...-empty
queue without deleting it.
int pop()
Removes the item at the front of a non
AJAX Queue Class
AJAX
Queue Class
This is a JavaScript
class that makes using AJAX really, REALLY simple and
robust. It supports every browser capable of AJAX (including IE5/Win98) and
manages
Advertisements
Queue in java
Queue in java
In this section we will discuss about
queue in java.
Queue...
which is added first into the
queue will be removed first from the
queue. Whatever the ordering is, head of the
queue is
removed first from the
queue
queue in java
queue in java how to performe all
queue operation?
Please visit the following link:
Queue Example in Java
Java collection Queue Example
class MainDemo {
public void queueExample() {
Queue queue = new...Java collection
Queue Example How can we use
Queue in java... implemented
Queue by its subclass LinkedList.We have created a method
Java Queue Example
Java
Queue Example how to use
queue in java ?
import java.util.LinkedList;
import java.util.Queue;
public
class MainDemo {
public void queueExample() {
Queue queue = new LinkedList
Collection : Queue Example
Collection :
Queue Example
In this tutorial we will describe implementation of
Queue with example.
Queue :
A java.util.Queue interface is type... as List.
It is based on concept of FIFO(First In First Out).
Queue contains
class
is a
class to represent a country in medal tally. It has an attribute of
country...
medals. In this
class, you should also define constructors, and assessor, mutator
methods.
Task 2
MedalTally.java is a
class to model a medal tally, containing
Stack and Queue
Configuration
class should
implement the following methods:
Class Configuration... functions will behave differently depending on whether your
class is a
Queue...Stack and Queue Consider a
class called Configuration. This
class Ã
Class
that how to to
implement
the constructor feature in a
class. This program...
Class, Object and Methods
Class : Whatever we can see in this world all
the things
Java Queue
Java
Queue
A
queue.... It is independent of the order used, while the
head of the
queue is that element which would be removed by a call to remove()
or poll(). In case of FIFO
queue
{create stack with queue}
{create stack with
queue} how can create
queue that accept all data type ??
meanse that this array is used accept all type for example(int ,char,String,float).??
please help me ....which output is :
1- to create
queue
2-Equeue
Java Queue example
will
implement Queue by its subclass LinkedList.
add()and offer() methods are used...
Queue Interface is part of java.util package.
Queue generally works on FIFO... is used to insert an element in the
queue but it will throw an
exception
ModuleNotFoundError: No module named 'fast_queue'
ModuleNotFoundError: No module named 'fast_
queue' Hi,
My Python... 'fast_
queue'
How to remove the ModuleNotFoundError: No module named 'fast_
queue' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'job_queue'
ModuleNotFoundError: No module named 'job_
queue' Hi,
My Python... 'job_
queue'
How to remove the ModuleNotFoundError: No module named 'job_
queue' error?
Thanks
Hi,
In your python environment you