Java Applets Centre
Queue


Description
This applet demonstrates the basic operations on a queue data structure. The "Insert" operation adds an element to the "rear" end of the queue, and takes O(1) time. Deletions take place at the "front" end of the queue, and take O(n) time.


Code (An array-based queue)

public void insert(int val){
  q[++tail]=val;
}
public void delete(){
  if(!isEmpty()){
   for(int i=0; i< tail; i++)
    q[i]=q[i+1];
    tail--;
   }
  }
}
public int front(){
  return q[0];
}
public boolean isEmpty(){
  return (tail==-1)? true:false;
}


Data Structures and Algorithms
Java Applets Centre


R. Mukundan
Department of Computer Science
University of Canterbury
Private Bag 4800, Christchurch
New Zealand.