Sunday, February 16, 2020

Data Structure- 5 Queue Concept using Circular Array


// In this article we will Learn concept of circular array to

implement the queue concept because as we can see the in linear array there is a no way to store new item even if there is a space after deleting an element ! 



with circular array w will use MAX-1 index of array and initially we will put front and rear to index zero position !


  1. when we need to insert a data we just increment the rear and insert the data !
  2. When we need to delete the data we just increase the front and print the data!

Only important point is that when during insertion if rear== front then it means the queue is full now and we will put rear one position back , if rear =0 then one position back is MAX-1; and if rear != 0 then one position back is rear-1;

In order to move rear and front we will use mod operator!
/******************************************************************************
Queue Concept using Circular Array !               
@Mukesh Mann dated 16-02-2020
*******************************************************************************/
#define MAX 4
enqueue(int);
//void delete();
void display();
int queue_array[MAX];
int rear = 0;
int front = 0;
 enqueue(int item)
{
    //int item;
     rear=(rear+1)%MAX;
    if (front==rear)
    {
        printf("Queue Overflow \n");
        if(rear==0)
        {
          rear=MAX-1; 
        }
        else {
        rear=rear-1;}
        return;
    }
    else
    {
    queue_array[rear]= item;
    printf("iTEM INSERTED is %d\n",queue_array[rear]);
       return;
    }
} /* End of Enqueue() */

 deqeue()
{
    if(front==rear)
    {
         printf("Queue is empty on");
         return -1;
    }
    else
    {
          front=(front+1)%MAX;
         int item= queue_array[front];
         printf("Deleted Item is %d\n",item);
          return item;
    }
  
} /* End of deqeue() */

 main()
 {
    
// Here we try to insert 3 item using a loop
 // initially front and rear are point to zero and we will utilize only max-1 index of circular array
     for (int i = front; i <= MAX-1; i++){
     enqueue(i);
      }
     
      for (int i = front; i <= MAX-1; i++){
       deqeue();
      }
    
}

No comments:

Post a Comment

9_Regular Expressions

Regular expressions- Sometimes in HTML, the developer defines more than one class name ( that’s class input has more than one name Here ...