In: Computer Science
class LLNode<T> {
public T info;
public LLNode<T> link;
public LLNode(T i, LLNode<T> l) { // constructor
info=i;
link=l;
}
}
class CircularLinkedQueue<T> {
private LLNode<T> rear = null; // rear pointer
public boolean isEmpty() {
/*checks if the queue is empty */
}
public int size() {
/* returns the number of elements in the queue */
}
public void enQueue(T element) {
/* enqueue a new element */
}
public T deQueue() {
/* dequeue the front element */
}
public String toString() {
String str = new String();
/* concatenate elements to a String */
return str;
}
}
public boolean isEmpty() {
if(this.rear == null)
return true;
else
return false;
}
public int size() {
if(this.rear == null)
{
return 0;
}
else
{
LLNode<T> n =
this.rear.link;
int i=1;
while(n!=this.rear)
{
i++;
n=n.link;
}
return i;
}
}
public void enQueue(T element) {
if(this.rear == null)
{
this.rear.info = element;
this.rear.link = this.rear;
}
else
{
LLNode<T> newNode =
null;
newNode.info = element;
newNode.link =
this.rear.link;
this.rear.link=newNode;
this.rear = newNode;
}
}
public T deQueue() {
if(this.rear.link == this.rear)
{
T e = this.rear.info;
this.rear=null;
return e;
}
else
{
T e = (this.rear.link).info;
LLNode<T> newNode =
this.rear.link;
this.rear.link =
(this.rear.link).link;
newNode=null;
return e;
}
}
public String toString() {
String str = new String();
LLNode<T> n = this.rear.link;
while(n!=this.rear)
{
str = str+"->"+n.info;
n=n.link;
}
return str;
}