How would this code in java be changed to make it a circular queue using linked list? -
using rear external pointer
public void enqueue(object element) // adds element rear of queue. { llobjectnode newnode = new llobjectnode(element); if (rear == null) front = newnode; else rear.setlink(newnode); rear = newnode; } public object dequeue() // throws queueunderflowexception if queue empty; // otherwise, removes front element queue , returns it. { if (isempty()) throw new queueunderflowexception("dequeue attempted on empty queue."); else { object element; element = front.getinfo(); front = front.getlink(); if (front == null) rear = null; return element; } } public boolean isempty() // returns true if queue empty; otherwise, returns false. { if (front == null) return true; else return false; }
public void enqueue(object element) ... rear.setlink(newnode); rear = newnode; rear.setlink(front); // make circular } ... public object dequeue() ... object element; element = front.getinfo(); if (front.getlink() == front) { // break circle last element front = null; rear = null; } else { front = front.getlink(); rear.setlink(front); // make circular } ...
Comments
Post a Comment