java - Remove and Insert in a circular queue -


in college professor has asked devise algorithm implement circular queue. have write algorithms 'remove()' , 'insert()' functions. came after hours of thinking.

declarations: q = circular queue structure contains 3 elements                  --> x[max] = array of max integers                 --> rear = logical pointer used inserting elements @ particular index                 --> front = logical pointer used deleting elements @ particular index predefined functions:                 --> incr (int y) : special function set y 0 once contains max else y++                 --> decr (int y) : special function set y max if contains 0 else y--  preconditions : @ initial time of defining structure set rear , front both @ 0  algorithm remove(q): returns int 1.  set <- q.x[q.front] 2.  incr (q.front)  3.  if q.front >= q.rear      1.  decr (q.front)     2.  print "queue empty"     else     1. return  algorithm insert(q,a) : returns nothing 1.  incr (q.rear) 2.  if q.rear = q.front     1.  decr (q.rear)     2.  print "queue full"     else     1.  set q.x[q.rear] <- 

this algorithm uses fact 'front' never overtakes 'rear'. on increasing 'front' if 'front = rear' means queue empty. , on increasing 'rear' if 'rear = front' means queue full.

but when showed professor said not solution.

is logic incorrect? if so, flaw in algorithm? if possible please suggest improvements.

(ps: reason have not googled solutions because want implement myself.)

your setup fails if first operation after initialization of queue remove request. reason first increment front index , check whether equals rear index detect whether queue empty or full. however, after initialization both indexes equal while queue empty.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -