Sri2110
Sri2110

Reputation: 335

Circular Queue Operations Using Array

Code shows basic operations on Circular Queue.

#define maxsize 10
typedef struct queue
{
    int data[maxsize];
    int f,r;
}myQueue;
myQueue q;  

void init(myQueue *q);
int full(myQueue *q);
int empty(myQueue *q);
void enqueue(myQueue *q,int num);
void dequeue(myQueue *q);
void print(myQueue *q);

void main()
{
    init(&q);
    int op;
    do
    {
        printf("\nCircular queue operations: Press:\n");
        printf("1 for enqueue\n");
        printf("2 for dequeue\n");
        printf("3 to print Circular Queue\n");

        int num,choice;
        printf("\nEnter choice:\n");
        scanf("%d",&choice);
        switch(choice)
        {
             case 1: printf("Enter number to insert :\n");
                     scanf("%d",&num);
                     if(full(&q))
                     {
                         printf("\nQueue is full\n");
                         exit(0);
                     }
                     enqueue(&q,num);
                     break;
             case 2: if(empty(&q))
                     {
                         printf("\nQueue is empty\n");
                         exit(0);
                     }
                     dequeue(&q);
                     break;
             case 3: printf("Printing current queue: \n");
                     print(&q);
                     break;
             default:break;
        }
        printf("Press 1 to continue or 0 to exit:\n");
        scanf("%d",&op);
    }
    while(op);
}

void init(myQueue *q)
{
    q->f=-1;
    q->r=-1;
}

int full(myQueue *q)
{
    if((q->r+1)%maxsize==q->f)
    {
        return 1;
    }
    else
        return 0;
}

int empty(myQueue *q)
{
    if(q->r==-1)
    {
        return 1;
    }
    else
        return 0;
}   

enqueue function is used to add the elements into queue.

void enqueue(myQueue *q,int num)
{
    if(empty(&q))
    {
        q->f=0;
        q->r=0;
    }
    else
    {
        q->r=(q->r+1)%maxsize;
    }
    q->data[q->r]=num;
    printf("\n%d is enqueued\n",q->data[q->r]);
}

Dequeue function is used to delete elements from the stack.

void dequeue(myQueue *q)
{
    int del_num;
    del_num=q->data[q->f];
    if(q->f==q->r)
    {
        init(&q);
    }
    else
    {  //To move front to the next position in the circular array.
        q->f=(q->f+1)%maxsize;
    }
    printf("\n%d is dequeued\n",del_num);
}

void print(myQueue *q)
{
    int i;
    for(i=q->f;i!=q->r;i=(i+1)%maxsize)
    {
        printf("%d\n",q->data[i]);
    }
    printf("%d\n",q->data[q->r]);
}
    

Issue: Circular queue is automatically enqueuing the 0 element in it initially. However, rest of the operations are working just fine.

I am not able to identify, why it is automatically inserting 0 in the circular queue, without me enqueuing it.

Upvotes: 0

Views: 1572

Answers (3)

Shekhar Kumar
Shekhar Kumar

Reputation: 11

#include <stdio.h>
#define SIZE 5

int items[SIZE];
int front = -1, rear = -1;
int isFull() {
  if ((front == rear + 1) || (front == 0 && rear == SIZE - 1)) return 1;
  return 0;
}
int isEmpty() {
  if (front == -1) return 1;
  return 0;
}
void enQueue(int element) {
  if (isFull())
    printf("\n Queue is full!! \n");
  else {
    if (front == -1) front = 0;
    rear = (rear + 1) % SIZE;
    items[rear] = element;
    printf("\n Inserted -> %d", element);
  }
}
int deQueue() {
  int element;
  if (isEmpty()) {
    printf("\n Queue is empty !! \n");
    return (-1);
  } else {
    element = items[front];
    if (front == rear) {
      front = -1;
      rear = -1;
    } 
    else {
      front = (front + 1) % SIZE;
    }
    printf("\n Deleted element -> %d \n", element);
    return (element);
  }
}
void display() {
  int i;
  if (isEmpty())
    printf(" \n Empty Queue\n");
  else {
    printf("\n Front -> %d ", front);
    printf("\n Items -> ");
    for (i = front; i != rear; i = (i + 1) % SIZE) {
      printf("%d ", items[i]);
    }
    printf("%d ", items[i]);
    printf("\n Rear -> %d \n", rear);
  }
}

int main() {
  deQueue();
  enQueue(1);
  enQueue(2);
  enQueue(3);
  enQueue(4);
  enQueue(5);
  enQueue(6);
  display();
  deQueue();
  display();
  enQueue(7);
  display();
  enQueue(8);
  return 0;
}

Upvotes: 0

Sri2110
Sri2110

Reputation: 335

1st Code Snippet requiring change:

void enqueue(myQueue *q,int num)
{

The below line is changed.

    if(empty(q)==1)
     {
      q->f=0;
      q->r=0;
     }
    else
     {
      q->r=(q->r+1)%maxsize;
     }
 q->data[q->r]=num;
 printf("\n%d is enqueued\n",q->data[q->r]);
}

2nd code snippet requiring changed:

void dequeue(myQueue *q)
 {
  int del_num;
  del_num=q->data[q->f];
  if(q->f==q->r)
   {

Line below is changed.

    init(q);
   }
  else
   {  //To move front to the next position in the circular array.
    q->f=(q->f+1)%maxsize;
   }
   printf("\n%d is dequeued\n",del_num);
 }

3rd snippet requiring change:

void print(myQueue *q)
 {
  int i;

Line below is changed.

  if(empty(q))
  {
    printf("Queue empty");
    exit(0);
  }
  else
  {
   printf("Printing current queue: \n");
   for(i=q->f;i!=q->r;i=(i+1)%maxsize)
   {
    printf("%d\n",q->data[i]);
   }
   printf("%d\n",q->data[q->r]);
  }
 }

That makes it perfectly alright. :)

Upvotes: 1

Costantino Grana
Costantino Grana

Reputation: 3418

Your print() function always prints q->data[q->r] as its last operation. This makes no sense when your queue is empty. Maybe you can avoid it like this:

void print(myQueue *q)
{
    if (empty(q))
      return;

    int i;
    for(i=q->f;i!=q->r;i=(i+1)%maxsize)
    {
        printf("%d\n",q->data[i]);
    }
    printf("%d\n",q->data[q->r]);
}

Anyway there are many more problems with your code, which I doubt it is compiling correctly. Just as an example, the function enqueue() receives a parameter of type myQueue*. Then it provides function empty with the address of it, but this is wrong. you have to pass the function the pointer itself, so q instead of &q. The same mistake is repeated over and over again.

Upvotes: 1

Related Questions