0

So, i tried to delete a particular element in a circular queue, and used its index as front and back.
When I ran the code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define size 6

int cir_queue[size];
int front = -1, rear = -1;

// Helper functions
int isEmpty();
int isFull();

// Driver functions
void enqueue(int val);
void dequeue(int val);
void display();

void main(){
    int i;
    int arr[] = {5, 7, 0, 6, 3, 9};
    int to_del[] = {6, 9, 5};
    int m = sizeof(arr) / sizeof(arr[0]);
    int n = sizeof(to_del) / sizeof(to_del[0]);

    for(i = 0; i < m; i++){
        enqueue(arr[i]);
        display();
        getch();
    }

    for(i = 0; i < n; i++){
        dequeue(to_del[i]);
        display();
        getch();
    }
    return;
}


int isEmpty(){
    if(front == -1){
        return 1;
    }
    return 0;
}

int isFull(){
    if(front == rear + 1 || (front == 0 && rear == size - 1)){
        return 1;
    }
    return 0;
}

void enqueue(int val){
    if(isFull()){
        printf("\nThe queue is full...");
    } else {
        if(front == -1){
            front = 0;
        }
        rear = (rear + 1) % size;
        cir_queue[rear] = val;
    }
    printf("\nElement inserted:\t%d", val);
    return;
}

void dequeue(int val){
    int element;
    if(isEmpty()){
        printf("\nEmpty Queue...\n");
    } else {
        while (cir_queue[front] != val){
            front = (front + 1) % size;
            rear = (rear - 1) % size;
        }
        element = cir_queue[front];
        if (front == rear){
            front = -1;
            rear = -1;
        } else {
            front = (front + 1) % size;
        }
        printf("\nDeleted element is: %d\n", element);
    }
    return;
}

void display(){
    int i;
    if(isEmpty()){
        printf("Queue is empty...");
    } else {
        printf("\n");
        printf("Front:\t%d\nRear:\t%d\nItems:\t{", front, rear);
        for(i = front; i != rear; i = (i + 1) % size){
            printf("\t%d,", cir_queue[i]);
        }
        printf("\t%d\t}\n\n", cir_queue[i]);
    }
    return;
}

I got output something like this (Only the deletion part):-

$ Deleted element is: 6

$ Front: 4
$ Rear: 2
$ Items: { 3, 9, 5, 7, 0 }


$ Deleted element is: 9

$ Front: 0
$ Rear: 1
$ Items: { 5, 7 }


$ Deleted element is: 5

$ Front: 1
$ Rear: 1
$ Items: { 7 }

I can not understand why this is happening... Any explanations and solutions?

  • 3
    Removing elements from anywhere but the back of a queue (and/or the front, if using a double-ended queue) is not a queue operation. So your `dequeue` function seems quite bogus to me. It searches the queue for an element and pops everything up to and including that element. Despite this not being "normal", there also seems to be an issue updating your front/rear values there. But you should be specific about what you don't understand about the behavior of your program, perhaps by describing what you _expect_ your program to output. – paddy Feb 11 '22 at 03:13
  • 1
    I just took a brief look at the loop in the `dequeue` function and it's very weird. Explain why you are modifying `rear` in that loop. Also explain what will happen if the value you're searching for doesn't exist in the queue. – paddy Feb 11 '22 at 03:17
  • I got the question: Write a program to inert the elements {5,7,0,6,3,9} into a circular queue and delete 6,9&5 from it. That is why I need to search the element, the front and rear are modified until the element is found. And there is no question of the element not being there as it is already clear in the question what to insert and delete. – Pratik Choudhuri Feb 11 '22 at 13:24

0 Answers0