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