Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners java: implement a class queue
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


mayzhu
java: implement a class queue
1 Answer(s)      2 years and 2 months ago
Posted in : Java Beginners


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.


Advertisement
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
queue in java
queue in java  how to performe all queue operation?   Please visit the following link: Queue Example in Java
JMS QUEUE
JMS QUEUE  how to create queue and queueconnectionfactory in websphere application server
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
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 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
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
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
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
{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
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
Implement the Queue in Java
Implement the Queue in Java       In this section, you will learn how to implement the queue. A queue... program implements a queue. It takes all elements as input by user. These values
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
Queue implementation using array.
Description: In this tutorial you will see how to implement queue using array and queue insert & delete operations. Code: #include <stdio.h> #define MAX 5 #include <stdlib.h> void insert(int queue[], int *rear
The queue, dequeue & clearQueue effect of jQuery
The queue, dequeue & clearQueue effect of jQuery In this tutorial, we will discuss about the queue, dequeue & clearQueue effects of jQuery. Given below the functionality of the queue, dequeue & clearQueue effects : queue
Java Programming Implement a virtual print queue
. You must implement the queue insert and remove methods as described in the text... print job at the beginning of the queue. You must implement a stack with the push...Java Programming Implement a virtual print queue  Implement
JavaScript array queue
;  In the JavaScript array we can implement the Queue by using the push() and shift() methods. It is a very simple way to implement queues. Queue is a data structure of type FIFO( First In First Out) so we can implement Queue
Circular queue implementation using array.
; void insert(int queue[], int *rear, int front, int value) { *rear= (*rear +1) % MAX; if(*rear == front) { printf("The queue is full\n"); exit(0); } queue[*rear] = value; } void deleteQ(int queue[], int *front
Queue implementation using linked list.
Description: The advantage of using linked list is that there is no size limit. The size of queue grow and shrink as per insertion and deletion takes... == NULL)) { printf(" The queue is empty can not delete Error\n"
class
class  Is a class a subclass of itself
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
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
How to match a string using the INDEX and the method use Stack or Queue?
How to match a string using the INDEX and the method use Stack or Queue?  Design a parser that parse the given string and counts the member of character that match the given data by INDEX position.using the STACK or QUEUE
how i can print the elements before a spicific value in array queue?
how i can print the elements before a spicific value in array queue?  how i can printall elements befor the spicific value and print this value also   Hi Friend, Try this: <html> <script> var arr=new Array
SCJP Module-9 Question-10
static void show(Queue q) { que.add(new Integer(12)); while (!que.isEmpty... java.util.Queue; public class Check { public static void main(String... args) { Queue<...; originally . But it is passed as an untyped Queue in show() method ,after
Advertisements
 

 

 

DMCA.com