Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - QUEUES CODE




Implementing Queue in Kotlin using Arrays


Example :



val SIZE = 10;

class Queue(size: Int) {

    var queueArray = IntArray(size);

    var frontElement = -1;
    var rearElement = -1;

    init {

        queueArray = IntArray(size);
    }


    fun enQueue(i: Int) {
        if (isFull()) {
            println("Queue is Full");
        }
        else {

            if (frontElement == -1)
                frontElement++;

            rearElement++;
            queueArray[rearElement] = i;
            println("Inserted the element " +i+" to the Queue");
        }
    }

    fun deQueue(): Int {
        var element = 0;
        if (isEmpty()) {

            println("Queue is empty");
            return -1;
        }
        else {
            element = queueArray[frontElement];

            if (frontElement >= rearElement) {
                queueArray[frontElement] = 0;
                frontElement = -1;
                rearElement = -1;
            }
            else {
                queueArray[frontElement] = 0;
                frontElement++;

            }
            println("The element "+element+" is removed from the Queue");
            return (element);
        }
    }

    fun isFull(): Boolean {
        if (frontElement == 0 && rearElement == (SIZE - 1))
            return true;
        else
            return false;
    }

    fun isEmpty(): Boolean {
        if (frontElement == -1)
            return true;
        else
            return false;
    }

    fun front(): Int {

        if (isEmpty()) {
            println("The Queue is empty");
            return -1;
        }
        else {
            return queueArray[frontElement];
        }
    }

    fun rear(): Int {

        if (isEmpty()) {
            println("The Queue is empty");
            return -1;
        }
        else {
            return queueArray[rearElement];
        }
    }
}

fun main(arr: Array<String>) {

    var queue = Queue(SIZE);

    queue.enQueue(10);
    queue.enQueue(20);
    queue.enQueue(30);

    queue.deQueue();
    queue.deQueue();
    queue.deQueue();

    queue.deQueue();

}


Output :



  Inserted the element 10 to the Queue
  Inserted the element 20 to the Queue
  Inserted the element 30 to the Queue
  The element 10 is removed from the Queue
  The element 20 is removed from the Queue
  The element 30 is removed from the Queue
  Queue is empty

Code Explanation for Queue


So, what we have tried in the above code is, we have inserted the the elements 10, 20 and 30 to the Queue.


Then we take the elements 10, 20 and 30 from the Queue.


As we know the most important operations/methods of Queue are Enqueue and Dequeue. However, there are other methods that we will be using here.


So, we have the below methods :

  1. fun enQueue(i: Int)

    The enQueue(i: Int) method puts an element into the Queue.

  2. fun deQueue(): Int

    The deQueue(): Int method takes an element out of the Queue.

  3. fun front(): Int

    The front(): Int method returns the first element of the Queue.

  4. fun rear(): Int

    The rear(): Int method returns the last of the Queue.

  5. fun isEmpty(): Boolean

    The isEmpty() method returns true if an array is empty and returns false if the array is not empty.

So, at first, we define the array where we will be storing the elements of the Stack.


var queueArray = IntArray(size);

Also, we have defined a constant called SIZE where we have defined the size of the array.


val SIZE = 10;

And, then we have initialised the variable frontElement and rearElement with -1.


var frontElement = -1;
var rearElement = -1;

The variable frontElement always points to the first element. Initially frontElement is -1 because it is not pointing to any element yet. When it becomes 0, it should be pointing to the 0th element.


Similarly, the variable rearElement always points to the last element. Initially rearElement is also -1 because it is not pointing to any element yet. When it becomes 0, it should be pointing to the 0th element initially.


Now, let us go to the main(..) method and see the Queue in action.


So, we have represented the Queue as an Array,


var queueArray = IntArray(size);

And initialised the Array in the Constructor.


class Queue(size: Int)

init {

	queueArray = IntArray(size);
}

So, initially we have an empty Queue.

java_Collections
java_Collections

Then we try to insert the element 10 into the Queue, using EnQueue operation,


queue.enQueue(10);

So, the enQueue(i: Int) method is called,


fun enQueue(i: Int) {
	if (isFull()) {
		println("Queue is Full");
	}
	else {

		if (frontElement == -1)
			frontElement++;

		rearElement++;
		queueArray[rearElement] = i;
		println("Inserted the element " +i+" to the Queue");
	}
}

Now, the enQueue(i: Int) method calls the isFull() method to check if the Array is full or not.


if (isFull()) {
	println("Queue is Full");
}

In this case the Array is not full, so we come to the else part.


else {

	if (frontElement == -1)
		frontElement++;

	rearElement++;
	queueArray[rearElement] = i;
	println("Inserted the element " +i+" to the Queue");
}

Now, inside the else condition, there is an if statement, that checks,


if (frontElement == -1)

And in this case the value of frontElement is -1. So, we enter in the block of if statement,


if (frontElement == -1)
	frontElement++;

And increment the value of frontElement by 1. And the value of frontElement becomes 0. So, the frontElement would be pointing to the first location now.


Similarly, we would be incrementing the rearElement as well.


rearElement++;

Then we initialise the element that was asked to be inserted(i.e.), in the first position.


queueArray[rearElement] = i;

So, both frontElement and rearElement would be pointing to the first location now.


Just think, if there are only one element in the Array representing Queue. Then both first and last element would be the same.

java_Collections
java_Collections

Then we come to the main(..) method again and try to insert the next element 20 to the Queue.


queue.enQueue(20);

So, the enQueue(i: Int) method is called,


fun enQueue(i: Int) {
	if (isFull()) {
		println("Queue is Full");
	}
	else {

		if (frontElement == -1)
			frontElement++;

		rearElement++;
		queueArray[rearElement] = i;
		println("Inserted the element " +i+" to the Queue");
	}
}

Now, the enQueue(i: Int) method calls the isFull() method to check if the Array is full or not.


if (isFull()) {
	println("Queue is Full");
}

In this case the Array is not full, so we come to the else part.


else {

	if (frontElement == -1)
		frontElement++;

	rearElement++;
	queueArray[rearElement] = i;
	println("Inserted the element " +i+" to the Queue");
}

Now, inside the else condition, there is an if statement, that checks,


if (frontElement == -1)

And in this case the value of frontElement is 0. So, we do not enter in the block of if statement and do not touch the frontElement.


So, we would be incrementing the rearElement only.


rearElement++;

Then we initialise the element that was asked to be inserted(i.e. 20).


queueArray[rearElement] = i;

Now, frontElement would be pointing to the first element as usual and rearElement would be pointing to the last location now.

java_Collections
java_Collections

So, we come to the main(..) method again and try to insert the next element 30 to the Queue.


queue.enQueue(30);
java_Collections

java_Collections

Now, that we have inserted all the three elements to the Queue. So, it is time for us to take the elements out of the Queue using the DeQueue operation.


And we are back in the main(..) method, where we have performed the DeQueue operation.


queue.deQueue();

And the deQueue() is called,


fun deQueue(): Int {
	var element = 0;
	if (isEmpty()) {

		println("Queue is empty");
		return -1;
	}
	else {
		element = queueArray[frontElement];

		if (frontElement >= rearElement) {
			queueArray[frontElement] = 0;
			frontElement = -1;
			rearElement = -1;
		}
		else {
			queueArray[frontElement] = 0;
			frontElement++;

		}
		println("The element "+element+" is removed from the Queue");
		return (element);
	}
}

At first we check if the Queue is empty or not, using the isEmpty() method.


if (isEmpty()) {

	println("Queue is empty");
	return -1;
}

In this case, the Queue is not empty. So, we come to the else part.


else {
	element = queueArray[frontElement];

	if (frontElement >= rearElement) {
		queueArray[frontElement] = 0;
		frontElement = -1;
		rearElement = -1;
	}
	else {
		queueArray[frontElement] = 0;
		frontElement++;

	}
	println("The element "+element+" is removed from the Queue");
	return (element);
}

Inside the else block, we get the front element in the variable element.


Guess why ?


Because we are trying to remove the front element from the Queue. So, we store the first element in a variable called element. And return it at the end of method. So, that we could know which element is removed.


Fair enough !


Now, we check if the frontElement is greater than or equal to rearElement. i.e. If both frontElement and rearElement points to the first element, then there is only one element in the Queue.


And in such case we have to remove the front element and make the Queue empty. And as we have seen, when the Queue is empty, both frontElement and rearElement is -1.


if (frontElement >= rearElement) {
	queueArray[frontElement] = 0;
	frontElement = -1;
	rearElement = -1;
}

But in this case, we are trying to remove the third element. And this condition is not satisfied and we come to the else part of it.


else {
	queueArray[frontElement] = 0;
	frontElement++;

}

Where we increase the frontElement, so that it points to the next location and replace 10 with 0.


queueArray[frontElement] = 0;
frontElement++;
java_Collections

java_Collections

Again, we come back to the main(..) method, where we have performed the DeQueue operation once again.


queue.deQueue();

And the deQueue() is called,


fun deQueue(): Int {
	var element = 0;
	if (isEmpty()) {

		println("Queue is empty");
		return -1;
	}
	else {
		element = queueArray[frontElement];

		if (frontElement >= rearElement) {
			queueArray[frontElement] = 0;
			frontElement = -1;
			rearElement = -1;
		}
		else {
			queueArray[frontElement] = 0;
			frontElement++;

		}
		println("The element "+element+" is removed from the Queue");
		return (element);
	}
}

The same way, we check if the Queue is empty or not, using the isEmpty() method.


if (isEmpty()) {

	println("Queue is empty");
	return -1;
}

In this case, the Queue is not empty. So, we come to the else part.


else {
	element = queueArray[frontElement];

	if (frontElement >= rearElement) {
		queueArray[frontElement] = 0;
		frontElement = -1;
		rearElement = -1;
	}
	else {
		queueArray[frontElement] = 0;
		frontElement++;

	}
	println("The element "+element+" is removed from the Queue");
	return (element);
}

Next, we come to the if statement to check, if there is only one element in the Queue or not?


if (frontElement >= rearElement) {
	queueArray[frontElement] = 0;
	frontElement = -1;
	rearElement = -1;
}

But in this case, we are trying to remove the second element. And this condition is not satisfied and we come to the else part of it.


else {
	queueArray[frontElement] = 0;
	frontElement++;

}

Where we increase the frontElement, so that it points to the next location and replace 20 with 0.


queueArray[frontElement] = 0;
frontElement++;
java_Collections

java_Collections

Now, both frontElement and rearElement is pointing to the same element i.e. 30.


So, the next time when we run the DeQueue operation.


queue.deQueue();

The below condition is satisfied,


if (frontElement >= rearElement) {
	queueArray[frontElement] = 0;
	frontElement = -1;
	rearElement = -1;
}

And both frontElement and rearElement is -1 now.

java_Collections
java_Collections