Java Applets Centre
Cyclic Queue


Description
This applet demonstrates the basic operations on a cyclic queue data structure. The structure uses two pointers "Front" and "Rear" which are updated with insert/delete operations. Both "insert" and "delete" operations take O(1) time.


Code (An array-based cyclic queue)

public void insert(int val){
  if(qfull) return;
  qempty=false;
  tail=(tail+1)%n;
  q[tail]=val;
  if(head==((tail+1)%n))qfull=true;
}
public void delete(){
  if(qempty) return;
  qfull=false;
  head=(head+1)%n;
  if(head==((tail+1)%n))qempty=true;
}
public int front(){
  return q[head];
}
public boolean isEmpty(){
  return qempty;
}


Data Structures and Algorithms
Java Applets Centre


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