Fixed capacity Queue

Fixed capacity Queue

What is a Queue?

queue is another common data structure that places elements in a sequence, similar to a stack. A queue uses the FIFO method (First In First Out), by which the first element that is enqueued will be the first one to be dequeued.


Basic operation’s of a queue

·       Enqueue ( ) : Adding items to a queue.

·       Dequeue( ) : Using the first item and removing it from queue.

·       Peek ( ) : retreving the first item from the queue

·       isEmpty ( ) : If queue is empty it return True else False

·       isTrue ( ) : if queue is full it return True else False

·       Delete ( ) : Deleting all the items from the queue

 

No alt text provided for this image


What is a Fixed capacity Queue?

A Queue has a fixed size, that is we can only store a limited number of items. The queue also follows FIFO (first-in-first-out) rule for storing and removing elements from the queue.


 

 

Differences between a fixed capacity queue and no fixed limit queue


No alt text provided for this image

 

Why We Need It


In a normal queue or we can say a dynamic queue where its capacity is not pre-defined or fixed, there are many challenges with it. Because the queue is implemented with the help of an array, so it requires contiguous space in the memory. and suppose a situation arises when we want to insert data in the queue but there is no space in the next memory block of the memory, so here is what happens all current data of the array is transferred to a new memory address where contiguous memory space is available and this process is not efficient in terms of resources because its time complexity would be O(n) for each data insertion, that is a huge loss of CPU. So to save our CPU time we pre-plan the size of the array so that the above problem does not appear because by defining the size of the array we reserve the space for the array and no other program can use that space. This is in the implementation of the queue we define the size of the array(size of the queue).

 


Use Cases:

In real life there are many use cases of the queue, some of them are listed below:

·       In CPU processing, it processes every process in a sequence and that sequence is FIFO. It means that the CPU processes those processes first which come first and those last which came last.

·       FIFOs/Queues are commonly used in electronic circuits for buffering and flow control between hardware and software.

·       Queues are used on networks for arranging data flow of data packets.

     Other than these there are many use cases for Queues.


Additional Point -

Implementation of Queue with AI

So basically queue is implemented using arrays, so where is Artificial Intelligence in it. let's understand it.

AI is one of the most emerging fields of computer science and technology, today almost every company uses AI for building/getting suggestions/understanding/ products and customer requirements. And AI can also help in implementing queues, How ?

As we saw the benefits of a fixed capacity queue, so defining the size of the queue is one of the major tasks in implementing a queue. And here AI plays an important role.

With the help of AI we can understand the current requirement of the system and according we can set the size of the queue. We can also change the size of the queue with AI when the requirement increases/decreases.


Simple python code to construct a fixed capacity queue


class Queue:
    def __init__(self,Qlen):
        self.maxsize = Qlen
        self.items = [None] * self.maxsize
        self.start = -1
        self.end = -1
        
    def isEmpty(self):
        if self.start == -1 and self.end == -1:
            return True
        else:
            return False 
        
    def isFull(self):
        if self.end == self.start - 1:
            return True
        elif self.start == 0 and self.end == self.maxsize - 1:
            return True
        else:
            return False
                

#the value of start is fixed and only end changes

    def enque(self,value):
        if not self.isFull():
            if self.end + 1 == self.maxsize:
                self.end = -1
            elif self.isEmpty():
                self.start = 0
            self.end = self.end + 1
            self.items[self.end] = value
        else:
            print("Queue is full...")
            

#here end_pos is fixed and start_pos changes

    def deque(self):
        if not self.isEmpty():        

first_pos = self.items[self.start] #we are using a temporary variable to store our data

self.items[self.start] = None

            if self.start == self.end:
                self.start = -1
                self.end = -1
            elif self.start + 1 == self.maxsize:
                self.start = 0
            else:
                self.start += 1
            return first_pos
        else:
            print("No item to deque")
def peek(self):
        if self.isEmpty():
            print("Queue is Empty...")
        else:
            return self.items[self.start]
        
    def delete(self):
        self.start = -1
        self.end = -1
        self.items = [None] * self.maxsize
        
    def printQueue(self):
        for i in self.items:
            print(i)
print(i)        


So that's all in this article, I hope you found it useful and knowledgeable.

Thanks for reading 🙂

To view or add a comment, sign in

More articles by Aniket Yadav

  • Template and Do While Loop in Python3

    the concept of a do-while loop in python. do-while loop: the do-while loop is a looping construct in programming…

    2 Comments
  • Running GUI Applications in Docker Container.

    In this article, we are going to launch a GUI application in the Docker container. According to your use case, you can…

    2 Comments

Insights from the community

Others also viewed

Explore topics