Implement circular queue using linked list

Here given code implementation process.

``````/*
C Program
Implement circular queue using linked list
*/
#include <stdio.h>
#include <stdlib.h>

// Queue node
struct QNode
{
int element;
struct QNode *next;
};
struct CircularQueue
{
struct QNode *front;
struct QNode *rear;
int size;
};
// Returns the new circular queue
struct CircularQueue *newQueue()
{
struct CircularQueue *q = (struct CircularQueue *) malloc(sizeof(struct CircularQueue));
if (q == NULL)
{
printf("Memory overflow, Not constructed new circular queue\n");
}
else
{
q->front = NULL;
q->rear = NULL;
q->size = 0;
}
return q;
}
// Returns the number of active nodes in circular queue
int isSize(struct CircularQueue *q)
{
return q->size;
}
// Add new element into circular queue
void enQueue(struct CircularQueue *q, int element)
{
// Make a new node
struct QNode *node = (struct QNode *) malloc(sizeof(struct QNode));
if (node != NULL)
{
// Set node values
node->element = element;
node->next = q->front;
if (q->front == NULL)
{
//  When inserting a first node of queue
q->front = node;
q->rear = node;
node->next = node;
}
else
{
// Add node at end position
q->rear->next = node;
q->rear = node;
}
q->size++;
}
else
{
printf("\n Memory Overflow, when creating a new Node\n");
}
}
// Remove a front node of circular queue
void deQueue(struct CircularQueue *q)
{
// Make a new node
struct QNode *node = q->front;
if (node != NULL)
{
if (q->front == q->rear)
{
// Remove last node
q->front = NULL;
q->rear = NULL;
}
else
{
q->front = node->next;
// Set the last link to new front
q->rear->next = q->front;
}
printf("\n Remove Node : %d", node->element);
q->size--;
free(node);
}
}
// Get the front node of circular queue
int peek(struct CircularQueue *q)
{
if (q->size == 0)
{
// When queue is empty
printf("\n Queue is empty \n");
return -1;
}
else
{
return q->front->element;
}
}
// Display element
void printData(struct CircularQueue *q)
{
struct QNode *node = q->front;
printf("\n Queue Element \n");
while (node != NULL)
{
printf(" %d ", node->element);
node = node->next;
if (node == q->front)
{
node = NULL;
}
}
}
int main(int argc, char
const *argv[])
{
struct CircularQueue *q = newQueue();
// Add node in circular queue
enQueue(q, 1);
enQueue(q, 4);
enQueue(q, 7);
enQueue(q, 9);
enQueue(q, 5);
enQueue(q, 6);
printData(q);
printf("\n Size : %d", isSize(q));
printf("\n Peek Node : %d", peek(q));
// Remove few front node
deQueue(q);
deQueue(q);
printData(q);
printf("\n Peek Node : %d", peek(q));
printf("\n Size : %d", isSize(q));
return 0;
}``````

Output

`````` Queue Element
1  4  7  9  5  6
Size : 6
Peek Node : 1
Remove Node : 1
Remove Node : 4
Queue Element
7  9  5  6
Peek Node : 7
Size : 4``````
``````/*
Java program
Implement circular queue using linked list
*/
// Queue node
class QNode
{
public int element;
public QNode next;
public QNode(int element)
{
this.element = element;
this.next = null;
}
}
// Define all circular queue functionality
public class CircularQueue
{
public QNode front;
public QNode rear;
public int size;
public CircularQueue()
{
this.size = 0;
this.rear = null;
this.front = null;
}
// Returns the number of active nodes in circular queue
public int isSize()
{
return this.size;
}
// Add new element into circular queue
public void enQueue(int element)
{
// Make a new node
QNode node = new QNode(element);
if (node != null)
{
if (this.front == null)
{
//  When inserting a first node of queue
this.front = node;
this.rear = node;
}
else
{
// Add node at end position
this.rear.next = node;
this.rear = node;
}
node.next = this.front;
this.size++;
}
else
{
System.out.print("\n Memory Overflow, when creating a new Node\n");
}
}
// Remove a front node of circular queue
public void deQueue()
{
// Make a new node
QNode node = this.front;
if (node != null)
{
if (this.front == this.rear)
{
// Remove last node
this.front = null;
this.rear = null;
}
else
{
this.front = node.next;
// Set the last link to new front
this.rear.next = this.front;
}
System.out.print("\n Remove Node : " + node.element);
this.size--;
node = null;
}
}
// Get the front node of circular queue
public int peek()
{
if (this.size == 0)
{
// When queue is empty
System.out.print("\n Queue is empty \n");
return -1;
}
else
{
return this.front.element;
}
}
// Display element
public void printData()
{
QNode node = this.front;
System.out.print("\n Queue Element \n");
while (node != null)
{
System.out.print(" " + node.element);
node = node.next;
if (node == this.front)
{
node = null;
}
}
}
public static void main(String[] args)
{
CircularQueue q = new CircularQueue();
// Add node in circular queue
q.enQueue(1);
q.enQueue(4);
q.enQueue(7);
q.enQueue(9);
q.enQueue(5);
q.enQueue(6);
q.printData();
System.out.print("\n Size : " + q.isSize());
System.out.print("\n Peek Node : " + q.peek());
// Remove few front node
q.deQueue();
q.deQueue();
q.printData();
System.out.print("\n Peek Node : " + q.peek());
System.out.print("\n Size : " + q.isSize());
}
}``````

Output

`````` Queue Element
1 4 7 9 5 6
Size : 6
Peek Node : 1
Remove Node : 1
Remove Node : 4
Queue Element
7 9 5 6
Peek Node : 7
Size : 4``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ program
Implement circular queue using linked list
*/

// Queue node
class QNode
{
public:
int element;
QNode *next;
QNode(int element)
{
this->element = element;
this->next = NULL;
}
};
// Define all circular queue functionality
class CircularQueue
{
public:
QNode *front;
QNode *rear;
int size;
CircularQueue()
{
this->size = 0;
this->rear = NULL;
this->front = NULL;
}
// Returns the number of active nodes in circular queue
int isSize()
{
return this->size;
}
// Add new element into circular queue
void enQueue(int element)
{
// Make a new node
QNode *node = new QNode(element);
if (node != NULL)
{
if (this->front == NULL)
{
//  When inserting a first node of queue
this->front = node;
this->rear = node;
}
else
{
// Add node at end position
this->rear->next = node;
this->rear = node;
}
node->next = this->front;
this->size++;
}
else
{
cout << "\n Memory Overflow, when creating a new Node\n";
}
}
// Remove a front node of circular queue
void deQueue()
{
// Make a new node
QNode *node = this->front;
if (node != NULL)
{
if (this->front == this->rear)
{
// Remove last node
this->front = NULL;
this->rear = NULL;
}
else
{
this->front = node->next;
// Set the last link to new front
this->rear->next = this->front;
}
cout << "\n Remove Node : " << node->element;
this->size--;
delete node;
node = NULL;
}
}
// Get the front node of circular queue
int peek()
{
if (this->size == 0)
{
// When queue is empty
cout << "\n Queue is empty \n";
return -1;
}
else
{
return this->front->element;
}
}
// Display element
void printData()
{
QNode *node = this->front;
cout << "\n Queue Element \n";
while (node != NULL)
{
cout << " " << node->element;
node = node->next;
if (node == this->front)
{
node = NULL;
}
}
}
};
int main()
{
CircularQueue q = CircularQueue();
// Add node in circular queue
q.enQueue(1);
q.enQueue(4);
q.enQueue(7);
q.enQueue(9);
q.enQueue(5);
q.enQueue(6);
q.printData();
cout << "\n Size : " << q.isSize();
cout << "\n Peek Node : " << q.peek();
// Remove few front node
q.deQueue();
q.deQueue();
q.printData();
cout << "\n Peek Node : " << q.peek();
cout << "\n Size : " << q.isSize();
return 0;
}``````

Output

`````` Queue Element
1 4 7 9 5 6
Size : 6
Peek Node : 1
Remove Node : 1
Remove Node : 4
Queue Element
7 9 5 6
Peek Node : 7
Size : 4``````
``````// Include namespace system
using System;
/*
C# program
Implement circular queue using linked list
*/
// Queue node
public class QNode
{
public int element;
public QNode next;
public QNode(int element)
{
this.element = element;
this.next = null;
}
}
// Define all circular queue functionality
public class CircularQueue
{
public QNode front;
public QNode rear;
public int size;
public CircularQueue()
{
this.size = 0;
this.rear = null;
this.front = null;
}
// Returns the number of active nodes in circular queue
public int isSize()
{
return this.size;
}
// Add new element into circular queue
public void enQueue(int element)
{
// Make a new node
QNode node = new QNode(element);
if (node != null)
{
if (this.front == null)
{
//  When inserting a first node of queue
this.front = node;
this.rear = node;
}
else
{
// Add node at end position
this.rear.next = node;
this.rear = node;
}
node.next = this.front;
this.size++;
}
else
{
Console.Write("\n Memory Overflow, when creating a new Node\n");
}
}
// Remove a front node of circular queue
public void deQueue()
{
// Make a new node
QNode node = this.front;
if (node != null)
{
if (this.front == this.rear)
{
// Remove last node
this.front = null;
this.rear = null;
}
else
{
this.front = node.next;
// Set the last link to new front
this.rear.next = this.front;
}
Console.Write("\n Remove Node : " + node.element);
this.size--;
node = null;
}
}
// Get the front node of circular queue
public int peek()
{
if (this.size == 0)
{
// When queue is empty
Console.Write("\n Queue is empty \n");
return -1;
}
else
{
return this.front.element;
}
}
// Display element
public void printData()
{
QNode node = this.front;
Console.Write("\n Queue Element \n");
while (node != null)
{
Console.Write(" " + node.element);
node = node.next;
if (node == this.front)
{
node = null;
}
}
}
public static void Main(String[] args)
{
CircularQueue q = new CircularQueue();
// Add node in circular queue
q.enQueue(1);
q.enQueue(4);
q.enQueue(7);
q.enQueue(9);
q.enQueue(5);
q.enQueue(6);
q.printData();
Console.Write("\n Size : " + q.isSize());
Console.Write("\n Peek Node : " + q.peek());
// Remove few front node
q.deQueue();
q.deQueue();
q.printData();
Console.Write("\n Peek Node : " + q.peek());
Console.Write("\n Size : " + q.isSize());
}
}``````

Output

`````` Queue Element
1 4 7 9 5 6
Size : 6
Peek Node : 1
Remove Node : 1
Remove Node : 4
Queue Element
7 9 5 6
Peek Node : 7
Size : 4``````
``````<?php
/*
Php program
Implement circular queue using linked list
*/
// Queue node
class QNode
{
public \$element;
public \$next;

function __construct(\$element)
{
\$this->element = \$element;
\$this->next = null;
}
}
// Define all circular queue functionality
class CircularQueue
{
public \$front;
public \$rear;
public \$size;

function __construct()
{
\$this->size = 0;
\$this->rear = null;
\$this->front = null;
}
// Returns the number of active nodes in circular queue
public	function isSize()
{
return \$this->size;
}
// Add new element into circular queue
public	function enQueue(\$element)
{
// Make a new node
\$node = new QNode(\$element);
if (\$node != null)
{
if (\$this->front == null)
{
//  When inserting a first node of queue
\$this->front = \$node;
\$this->rear = \$node;
}
else
{
// Add node at end position
\$this->rear->next = \$node;
\$this->rear = \$node;
}
\$node->next = \$this->front;
\$this->size++;
}
else
{
echo "\n Memory Overflow, when creating a new Node\n";
}
}
// Remove a front node of circular queue
public	function deQueue()
{
// Make a new node
\$node = \$this->front;
if (\$node != null)
{
if (\$this->front == \$this->rear)
{
// Remove last node
\$this->front = null;
\$this->rear = null;
}
else
{
\$this->front = \$node->next;
// Set the last link to new front
\$this->rear->next = \$this->front;
}
echo "\n Remove Node : ". \$node->element;
\$this->size--;
\$node = null;
}
}
// Get the front node of circular queue
public	function peek()
{
if (\$this->size == 0)
{
// When queue is empty
echo "\n Queue is empty \n";
return -1;
}
else
{
return \$this->front->element;
}
}
// Display element
public	function printData()
{
\$node = \$this->front;
echo "\n Queue Element \n";
while (\$node != null)
{
echo " ". \$node->element;
\$node = \$node->next;
if (\$node == \$this->front)
{
\$node = null;
}
}
}
}

function main()
{
\$q = new CircularQueue();
// Add node in circular queue
\$q->enQueue(1);
\$q->enQueue(4);
\$q->enQueue(7);
\$q->enQueue(9);
\$q->enQueue(5);
\$q->enQueue(6);
\$q->printData();
echo "\n Size : ". \$q->isSize();
echo "\n Peek Node : ". \$q->peek();
// Remove few front node
\$q->deQueue();
\$q->deQueue();
\$q->printData();
echo "\n Peek Node : ". \$q->peek();
echo "\n Size : ". \$q->isSize();
}
main();``````

Output

`````` Queue Element
1 4 7 9 5 6
Size : 6
Peek Node : 1
Remove Node : 1
Remove Node : 4
Queue Element
7 9 5 6
Peek Node : 7
Size : 4``````
``````/*
Node Js program
Implement circular queue using linked list
*/
// Queue node
class QNode
{
constructor(element)
{
this.element = element;
this.next = null;
}
}
// Define all circular queue functionality
class CircularQueue
{
constructor()
{
this.size = 0;
this.rear = null;
this.front = null;
}
// Returns the number of active nodes in circular queue
isSize()
{
return this.size;
}
// Add new element into circular queue
enQueue(element)
{
// Make a new node
var node = new QNode(element);
if (node != null)
{
if (this.front == null)
{
//  When inserting a first node of queue
this.front = node;
this.rear = node;
}
else
{
// Add node at end position
this.rear.next = node;
this.rear = node;
}
node.next = this.front;
this.size++;
}
else
{
process.stdout.write("\n Memory Overflow, when creating a new Node\n");
}
}
// Remove a front node of circular queue
deQueue()
{
// Make a new node
var node = this.front;
if (node != null)
{
if (this.front == this.rear)
{
// Remove last node
this.front = null;
this.rear = null;
}
else
{
this.front = node.next;
// Set the last link to new front
this.rear.next = this.front;
}
process.stdout.write("\n Remove Node : " + node.element);
this.size--;
node = null;
}
}
// Get the front node of circular queue
peek()
{
if (this.size == 0)
{
// When queue is empty
process.stdout.write("\n Queue is empty \n");
return -1;
}
else
{
return this.front.element;
}
}
// Display element
printData()
{
var node = this.front;
process.stdout.write("\n Queue Element \n");
while (node != null)
{
process.stdout.write(" " + node.element);
node = node.next;
if (node == this.front)
{
node = null;
}
}
}
}

function main()
{
var q = new CircularQueue();
// Add node in circular queue
q.enQueue(1);
q.enQueue(4);
q.enQueue(7);
q.enQueue(9);
q.enQueue(5);
q.enQueue(6);
q.printData();
process.stdout.write("\n Size : " + q.isSize());
process.stdout.write("\n Peek Node : " + q.peek());
// Remove few front node
q.deQueue();
q.deQueue();
q.printData();
process.stdout.write("\n Peek Node : " + q.peek());
process.stdout.write("\n Size : " + q.isSize());
}
main();``````

Output

`````` Queue Element
1 4 7 9 5 6
Size : 6
Peek Node : 1
Remove Node : 1
Remove Node : 4
Queue Element
7 9 5 6
Peek Node : 7
Size : 4``````
``````#   Python 3 program
#   Implement circular queue using linked list

#  Queue node
class QNode :

def __init__(self, element) :
self.element = element
self.next = None

#  Define all circular queue functionality
class CircularQueue :

def __init__(self) :
self.size = 0
self.rear = None
self.front = None

#  Returns the number of active nodes in circular queue
def isSize(self) :
return self.size

#  Add new element into circular queue
def enQueue(self, element) :
#  Make a new node
node = QNode(element)
if (node != None) :
if (self.front == None) :
#   When inserting a first node of queue
self.front = node
self.rear = node
else :
#  Add node at end position
self.rear.next = node
self.rear = node

node.next = self.front
self.size += 1
else :
print("\n Memory Overflow, when creating a new Node")

#  Remove a front node of circular queue
def deQueue(self) :
#  Make a new node
node = self.front
if (node != None) :
if (self.front == self.rear) :
#  Remove last node
self.front = None
self.rear = None
else :
self.front = node.next
#  Set the last link to new front
self.rear.next = self.front

print("\n Remove Node : ", node.element, end = "")
self.size -= 1
node = None

#  Get the front node of circular queue
def peek(self) :
if (self.size == 0) :
#  When queue is empty
print("\n Queue is empty ")
return -1
else :
return self.front.element

#  Display element
def printData(self) :
node = self.front
print("\n Queue Element ")
while (node != None) :
print(" ", node.element, end = "")
node = node.next
if (node == self.front) :
node = None

def main() :
q = CircularQueue()
#  Add node in circular queue
q.enQueue(1)
q.enQueue(4)
q.enQueue(7)
q.enQueue(9)
q.enQueue(5)
q.enQueue(6)
q.printData()
print("\n Size : ", q.isSize(), end = "")
print("\n Peek Node : ", q.peek(), end = "")
#  Remove few front node
q.deQueue()
q.deQueue()
q.printData()
print("\n Peek Node : ", q.peek(), end = "")
print("\n Size : ", q.isSize(), end = "")

if __name__ == "__main__": main()``````

Output

`````` Queue Element
1  4  7  9  5  6
Size :  6
Peek Node :  1
Remove Node :  1
Remove Node :  4
Queue Element
7  9  5  6
Peek Node :  7
Size :  4``````
``````#   Ruby program
#   Implement circular queue using linked list

#  Queue node
class QNode
# Define the accessor and reader of class QNode
attr_accessor :element, :next

def initialize(element)
self.element = element
self.next = nil
end

end

#  Define all circular queue functionality
class CircularQueue
# Define the accessor and reader of class CircularQueue
attr_accessor :front, :rear, :size

def initialize()
self.size = 0
self.rear = nil
self.front = nil
end

#  Returns the number of active nodes in circular queue
def isSize()
return self.size
end

#  Add new element into circular queue
def enQueue(element)
#  Make a new node
node = QNode.new(element)
if (node != nil)
if (self.front == nil)
#   When inserting a first node of queue
self.front = node
self.rear = node
else
#  Add node at end position
self.rear.next = node
self.rear = node
end

node.next = self.front
self.size += 1
else
print("\n Memory Overflow, when creating a new Node\n")
end

end

#  Remove a front node of circular queue
def deQueue()
#  Make a new node
node = self.front
if (node != nil)
if (self.front == self.rear)
#  Remove last node
self.front = nil
self.rear = nil
else
self.front = node.next
#  Set the last link to new front
self.rear.next = self.front
end

print("\n Remove Node : ", node.element)
self.size -= 1
node = nil
end

end

#  Get the front node of circular queue
def peek()
if (self.size == 0)
#  When queue is empty
print("\n Queue is empty \n")
return -1
else
return self.front.element
end

end

#  Display element
def printData()
node = self.front
print("\n Queue Element \n")
while (node != nil)
print(" ", node.element)
node = node.next
if (node == self.front)
node = nil
end

end

end

end

def main()
q = CircularQueue.new()
#  Add node in circular queue
q.enQueue(1)
q.enQueue(4)
q.enQueue(7)
q.enQueue(9)
q.enQueue(5)
q.enQueue(6)
q.printData()
print("\n Size : ", q.isSize())
print("\n Peek Node : ", q.peek())
#  Remove few front node
q.deQueue()
q.deQueue()
q.printData()
print("\n Peek Node : ", q.peek())
print("\n Size : ", q.isSize())
end

main()``````

Output

`````` Queue Element
1 4 7 9 5 6
Size : 6
Peek Node : 1
Remove Node : 1
Remove Node : 4
Queue Element
7 9 5 6
Peek Node : 7
Size : 4``````
``````/*
Scala program
Implement circular queue using linked list
*/
// Queue node
class QNode(var element: Int , var next: QNode)
{
def this(element: Int)
{
this(element, null);
}
}
// Define all circular queue functionality
class CircularQueue(var front: QNode , var rear: QNode , var size: Int)
{
def this()
{
this(null, null, 0);
}
// Returns the number of active nodes in circular queue
def isSize(): Int = {
return this.size;
}
// Add new element into circular queue
def enQueue(element: Int): Unit = {
// Make a new node
var node: QNode = new QNode(element);
if (node != null)
{
if (this.front == null)
{
//  When inserting a first node of queue
this.front = node;
this.rear = node;
}
else
{
// Add node at end position
this.rear.next = node;
this.rear = node;
}
node.next = this.front;
this.size += 1;
}
else
{
print("\n Memory Overflow, when creating a new Node\n");
}
}
// Remove a front node of circular queue
def deQueue(): Unit = {
// Make a new node
var node: QNode = this.front;
if (node != null)
{
if (this.front == this.rear)
{
// Remove last node
this.front = null;
this.rear = null;
}
else
{
this.front = node.next;
// Set the last link to new front
this.rear.next = this.front;
}
print("\n Remove Node : " + node.element);
this.size -= 1;
node = null;
}
}
// Get the front node of circular queue
def peek(): Int = {
if (this.size == 0)
{
// When queue is empty
print("\n Queue is empty \n");
return -1;
}
else
{
return this.front.element;
}
}
// Display element
def printData(): Unit = {
var node: QNode = this.front;
print("\n Queue Element \n");
while (node != null)
{
print(" " + node.element);
node = node.next;
if (node == this.front)
{
node = null;
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var q: CircularQueue = new CircularQueue();
// Add node in circular queue
q.enQueue(1);
q.enQueue(4);
q.enQueue(7);
q.enQueue(9);
q.enQueue(5);
q.enQueue(6);
q.printData();
print("\n Size : " + q.isSize());
print("\n Peek Node : " + q.peek());
// Remove few front node
q.deQueue();
q.deQueue();
q.printData();
print("\n Peek Node : " + q.peek());
print("\n Size : " + q.isSize());
}
}``````

Output

`````` Queue Element
1 4 7 9 5 6
Size : 6
Peek Node : 1
Remove Node : 1
Remove Node : 4
Queue Element
7 9 5 6
Peek Node : 7
Size : 4``````
``````/*
Swift 4 program
Implement circular queue using linked list
*/
// Queue node
class QNode
{
var element: Int;
var next: QNode? ;
init(_ element: Int)
{
self.element = element;
self.next = nil;
}
}
// Define all circular queue functionality
class CircularQueue
{
var front: QNode? ;
var rear: QNode? ;
var size: Int;
init()
{
self.size = 0;
self.rear = nil;
self.front = nil;
}
// Returns the number of active nodes in circular queue
func isSize()->Int
{
return self.size;
}
// Add new element into circular queue
func enQueue(_ element: Int)
{
// Make a new node
let node: QNode? = QNode(element);
if (node  != nil)
{
if (self.front == nil)
{
//  When inserting a first node of queue
self.front = node;
self.rear = node;
}
else
{
// Add node at end position
self.rear!.next = node;
self.rear = node;
}
node!.next = self.front;
self.size += 1;
}
else
{
print("\n Memory Overflow, when creating a new Node");
}
}
// Remove a front node of circular queue
func deQueue()
{
// Make a new node
var node: QNode? = self.front;
if (node  != nil)
{
if (self.front === self.rear)
{
// Remove last node
self.front = nil;
self.rear = nil;
}
else
{
self.front = node!.next;
// Set the last link to new front
self.rear!.next = self.front;
}
print("\n Remove Node : ", node!.element, terminator: "");
self.size -= 1;
node = nil;
}
}
// Get the front node of circular queue
func peek()->Int
{
if (self.size == 0)
{
// When queue is empty
print("\n Queue is empty ");
return -1;
}
else
{
return self.front!.element;
}
}
// Display element
func printData()
{
var node: QNode? = self.front;
print("\n Queue Element ");
while (node  != nil)
{
print(" ", node!.element, terminator: "");
node = node!.next;
if (node === self.front)
{
node = nil;
}
}
}
}
func main()
{
let q: CircularQueue = CircularQueue();
// Add node in circular queue
q.enQueue(1);
q.enQueue(4);
q.enQueue(7);
q.enQueue(9);
q.enQueue(5);
q.enQueue(6);
q.printData();
print("\n Size : ", q.isSize(), terminator: "");
print("\n Peek Node : ", q.peek(), terminator: "");
// Remove few front node
q.deQueue();
q.deQueue();
q.printData();
print("\n Peek Node : ", q.peek(), terminator: "");
print("\n Size : ", q.isSize(), terminator: "");
}
main();``````

Output

`````` Queue Element
1  4  7  9  5  6
Size :  6
Peek Node :  1
Remove Node :  1
Remove Node :  4
Queue Element
7  9  5  6
Peek Node :  7
Size :  4``````
``````/*
Kotlin program
Implement circular queue using linked list
*/
// Queue node
class QNode
{
var element: Int;
var next: QNode ? ;
constructor(element: Int)
{
this.element = element;
this.next = null;
}
}
// Define all circular queue functionality
class CircularQueue
{
var front: QNode ? ;
var rear: QNode ? ;
var size: Int;
constructor()
{
this.size = 0;
this.rear = null;
this.front = null;
}
// Returns the number of active nodes in circular queue
fun isSize(): Int
{
return this.size;
}
// Add new element into circular queue
fun enQueue(element: Int): Unit
{
// Make a new node
var node: QNode ? = QNode(element);
if (node != null)
{
if (this.front == null)
{
//  When inserting a first node of queue
this.front = node;
this.rear = node;
}
else
{
// Add node at end position
this.rear?.next = node;
this.rear = node;
}
node.next = this.front;
this.size += 1;
}
else
{
print("\n Memory Overflow, when creating a new Node\n");
}
}
// Remove a front node of circular queue
fun deQueue(): Unit
{
// Make a new node
var node: QNode ? = this.front;
if (node != null)
{
if (this.front == this.rear)
{
// Remove last node
this.front = null;
this.rear = null;
}
else
{
this.front = node.next;
// Set the last link to new front
this.rear?.next = this.front;
}
print("\n Remove Node : " + node.element);
this.size -= 1;
}
}
// Get the front node of circular queue
fun peek(): Int
{
if (this.size == 0)
{
// When queue is empty
print("\n Queue is empty \n");
return -1;
}
else
{
return this.front!!.element;
}
}
// Display element
fun printData(): Unit
{
var node: QNode ? = this.front;
print("\n Queue Element \n");
while (node != null)
{
print(" " + node.element);
node = node.next;
if (node == this.front)
{
node = null;
}
}
}
}
fun main(args: Array < String > ): Unit
{
var q: CircularQueue = CircularQueue();
// Add node in circular queue
q.enQueue(1);
q.enQueue(4);
q.enQueue(7);
q.enQueue(9);
q.enQueue(5);
q.enQueue(6);
q.printData();
print("\n Size : " + q.isSize());
print("\n Peek Node : " + q.peek());
// Remove few front node
q.deQueue();
q.deQueue();
q.printData();
print("\n Peek Node : " + q.peek());
print("\n Size : " + q.isSize());
}``````

Output

`````` Queue Element
1 4 7 9 5 6
Size : 6
Peek Node : 1
Remove Node : 1
Remove Node : 4
Queue Element
7 9 5 6
Peek Node : 7
Size : 4``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.