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 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( ) );
}

}









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
cannot find symbol class array queue--plzz somebody help..
cannot find symbol class array queue--plzz somebody help..  import... import that package through the jar file or implement the class ArrayQueue. ArrayQueue.java: class ArrayQueue implements Queue { static final int defaultsize
Email queue while sending mail using Struts Class
Email queue while sending mail using Struts Class  Can I maintain a queue of mails being sent from a JSP page in a DB to get its status
Email queue while sending mail using Struts Class
Email queue while sending mail using Struts Class  Can I maintain a queue of mails being sent from a JSP page in a DB to get its status
class
class  Is a class a subclass of itself
queue in java
queue in java  how to performe all queue operation?   Please visit the following link: Queue Example in Java
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
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
JMS QUEUE
JMS QUEUE  how to create queue and queueconnectionfactory in websphere application server
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
Class
to to implement the constructor feature in a class. This program is using two classes... Class, Object and Methods       Class : Whatever we can see in this world all the things
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 Ã
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
Queue - Java Beginners
Queue  i'm working with queue on java. since im beginners im asking for additional example programs on queue using java to enhance my knowledge. thanks so much for the help! God bless
how to create a queue - JMS
how to create a queue  Can u please tell me how to create q queue in JMS and also tell me how to write a program to Send a Static Message for JMS Queue ....please i need it urgently
Super class of an Exception class
Super class of an Exception class  hello,,, What is super class of an Exception class?   hii,,ADS_TO_REPLACE_1 the super class of an exception is throwable class. and the super class of an throwable class
{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
. In the following example we will implement Queue by its subclass LinkedList...; public class MainDemo { public void queueExample() { Queue...Queue Interface is part of java.util package. Queue generally works on FIFO
stack and queue - Java Beginners
stack and queue  write two different program in java 1.) stack 2.) queue   Hi Friend, Please visit the following links: http://www.roseindia.net/java/example/java/util/StackImplement.shtml http
ModuleNotFoundError: No module named 'micropython-queue'
ModuleNotFoundError: No module named 'micropython-queue'  Hi, My... named 'micropython-queue' How to remove the ModuleNotFoundError: No module named 'micropython-queue' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'persist-queue'
ModuleNotFoundError: No module named 'persist-queue'  Hi, My... 'persist-queue' How to remove the ModuleNotFoundError: No module named 'persist-queue' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'pubsub-queue'
ModuleNotFoundError: No module named 'pubsub-queue'  Hi, My Python... 'pubsub-queue' How to remove the ModuleNotFoundError: No module named 'pubsub-queue' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'push-queue'
ModuleNotFoundError: No module named 'push-queue'  Hi, My Python... 'push-queue' How to remove the ModuleNotFoundError: No module named 'push-queue' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'queue_engine'
ModuleNotFoundError: No module named 'queue_engine'  Hi, My Python... 'queue_engine' How to remove the ModuleNotFoundError: No module named 'queue_engine' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'queue-plus'
ModuleNotFoundError: No module named 'queue-plus'  Hi, My Python... 'queue-plus' How to remove the ModuleNotFoundError: No module named 'queue-plus' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'queue-system'
ModuleNotFoundError: No module named 'queue-system'  Hi, My Python... 'queue-system' How to remove the ModuleNotFoundError: No module named 'queue-system' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'queue_utils'
ModuleNotFoundError: No module named 'queue_utils'  Hi, My Python... 'queue_utils' How to remove the ModuleNotFoundError: No module named 'queue_utils' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'simple-queue'
ModuleNotFoundError: No module named 'simple-queue'  Hi, My Python... 'simple-queue' How to remove the ModuleNotFoundError: No module named 'simple-queue' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'sqlite_queue'
ModuleNotFoundError: No module named 'sqlite_queue'  Hi, My Python... 'sqlite_queue' How to remove the ModuleNotFoundError: No module named 'sqlite_queue' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'task-queue'
ModuleNotFoundError: No module named 'task-queue'  Hi, My Python... 'task-queue' How to remove the ModuleNotFoundError: No module named 'task-queue' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'thread-queue'
ModuleNotFoundError: No module named 'thread-queue'  Hi, My Python... 'thread-queue' How to remove the ModuleNotFoundError: No module named 'thread-queue' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'work-queue'
ModuleNotFoundError: No module named 'work-queue'  Hi, My Python... 'work-queue' How to remove the ModuleNotFoundError: No module named 'work-queue' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'aiotarantool_queue'
ModuleNotFoundError: No module named 'aiotarantool_queue'  Hi, My... named 'aiotarantool_queue' How to remove the ModuleNotFoundError: No module named 'aiotarantool_queue' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'boost_queue'
ModuleNotFoundError: No module named 'boost_queue'  Hi, My Python... 'boost_queue' How to remove the ModuleNotFoundError: No module named 'boost_queue' error? Thanks   Hi, In your python environment
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
ModuleNotFoundError: No module named 'aiotarantool_queue'
ModuleNotFoundError: No module named 'aiotarantool_queue'  Hi, My... named 'aiotarantool_queue' How to remove the ModuleNotFoundError: No module named 'aiotarantool_queue' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'iterable-queue'
ModuleNotFoundError: No module named 'iterable-queue'  Hi, My... named 'iterable-queue' How to remove the ModuleNotFoundError: No module named 'iterable-queue' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'micropython-queue'
ModuleNotFoundError: No module named 'micropython-queue'  Hi, My... named 'micropython-queue' How to remove the ModuleNotFoundError: No module named 'micropython-queue' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'parallel-queue'
ModuleNotFoundError: No module named 'parallel-queue'  Hi, My... named 'parallel-queue' How to remove the ModuleNotFoundError: No module named 'parallel-queue' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'pulsar-queue'
ModuleNotFoundError: No module named 'pulsar-queue'  Hi, My Python... 'pulsar-queue' How to remove the ModuleNotFoundError: No module named 'pulsar-queue' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'queue-front'
ModuleNotFoundError: No module named 'queue-front'  Hi, My Python... 'queue-front' How to remove the ModuleNotFoundError: No module named 'queue-front' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'queue_status'
ModuleNotFoundError: No module named 'queue_status'  Hi, My Python... 'queue_status' How to remove the ModuleNotFoundError: No module named 'queue_status' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'queue-utilities'
ModuleNotFoundError: No module named 'queue-utilities'  Hi, My... named 'queue-utilities' How to remove the ModuleNotFoundError: No module named 'queue-utilities' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'slackbot-queue'
ModuleNotFoundError: No module named 'slackbot-queue'  Hi, My... named 'slackbot-queue' How to remove the ModuleNotFoundError: No module named 'slackbot-queue' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'tarantool-queue'
ModuleNotFoundError: No module named 'tarantool-queue'  Hi, My... named 'tarantool-queue' How to remove the ModuleNotFoundError: No module named 'tarantool-queue' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'task-queue'
ModuleNotFoundError: No module named 'task-queue'  Hi, My Python... 'task-queue' How to remove the ModuleNotFoundError: No module named 'task-queue' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'abstract-queue'
ModuleNotFoundError: No module named 'abstract-queue'  Hi, My... named 'abstract-queue' How to remove the ModuleNotFoundError: No module named 'abstract-queue' error? Thanks   Hi, In your python

Ads