'Insert and Delete element on Circular Queue
I'm studying about circular queue in data structure . As you can see from the code below, I try to delete a specific data and insert data on Circular queue. However, when I try to run it there's a problem when deleting data and insert a new one. I had no clue about it. I was trying to solve this for many hours but I can't find anything. Any help would be appreciated.
#include <stdio.h>
#define SIZE 3
typedef struct queue{
int val[SIZE]={NULL};
int front;
int rear;
}queue;
void display(struct queue *q);
void enqueue(struct queue *q){
int ins,i=1;
if((q->rear == SIZE-1 && q->front == 0) || (q->rear == q->front-1)){
printf("Queue is full!\n");
}
else if (q->front == -1)
{
printf("Enqueue data : ");
scanf("%d",&ins);
q->front = q->rear = 0;
q->val[q->rear] = ins;
}
else if (q->rear == SIZE-1)
{
printf("Enqueue data : ");
scanf("%d",&ins);
q->rear = 0;
q->val[q->rear] = ins;
}
else
{
printf("Enqueue data : ");
scanf("%d",&ins);
q->rear++;
q->val[q->rear] = ins;
}
display(q);
};
void dequeue(struct queue *q);
int main(){
queue *q= new queue;
q->front = -1;
q->rear = -1;
char select;
flag1:
printf("\n------- Please Select Operations ---------\n");
printf("Press e: Enqueue\n");
printf("Press d: Dequeue\n");
printf("Press x: Exit Program\n");
printf("------------------------------------------\n");
printf("Select Menu : ");
scanf(" %c",&select);
switch(select){
case 'e' : enqueue(q); goto flag1;
case 'd' : dequeue(q); goto flag1;
case 'x' : break;
}
return 0;
}
void dequeue(struct queue *q){
int deq,hold;
if (q->front == -1)
{
printf("Queue is Empty");
}
else
{
printf("Dequeue data : ");
scanf("%d",&deq);
for(int i=0;i<SIZE;i++){
if(deq==q->val[i]){
if(i==q->front){
q->val[q->front]=NULL;
q->front = i;
q->rear=q->rear+1;
if(q->rear=SIZE-1){
q->rear=0;
}
}
else
q->val[q->front]=NULL;
}
}
display(q);
}
};
void display(struct queue *q){
int i;
printf("Queue : |");
for (i= 0; i<SIZE; i++){
if(q->val[i]==NULL){
printf(" |");
}
else
printf("%d|", q->val[i]);
}
};
Solution 1:[1]
GIGO!
Your code is overly complex.
Complex code requires complex testing and debugging.
Try the following code:
void enqueue( struct queue *q, int v) {
int r = (q.rear + 1) % SIZE
if(( r == q.front) {
printf( "Queue is full!\n");
} else {
q.val[ q.rear] = v;
q.rear = r;
}
};
int dequeue( struct queue *q) {
if( q.front == q.rear) {
printf( "Queue is Empty");
v = NULL; # or whatever (required as a return value)
} else {
v = q.val[ q.front];
q.front = ( q.front + 1) % SIZE;
}
return v;
};
int main() {
queue *q = new queue;
q->front = q->rear = 0;
...
};
To summarize:
- rear is index of the youngest element
- front is the index of the oldest element
- % (the modulus operator) take care of the index overwrapping.
- (front == rear) means empty buffer
- ((rear +1) % SIZE == front) means full buffer.
Please note that this simple algorithm always leave an unused element in the buffer. This is required to distinguish "full" from "empty".
Solution 2:[2]
Circular Queue in Java
public class CircularQueue<T> {
private Object[] arr;
private int front = -1, rear = -1;
public CircularQueue(int initialCapacity) {
this.arr = new Object[initialCapacity];
}
public void add(T val) throws Exception {
if (isEmpty()) {
rear++;
front++;
} else if (isFull()) {
throw new Exception("Queue is full");
}
arr[rear] = val;
rear = (rear + 1) % arr.length;
}
public T delete() throws Exception {
if (isEmpty()) {
throw new Exception("No elements in Queue");
}
T temp = (T) arr[front];
front = (front + 1) % arr.length;
return temp;
}
public boolean isEmpty() {
return (front == -1 && rear == -1);
}
public boolean isFull() {
return (front == rear);
}
@Override
public String toString() {
String ret = "[ ";
int temp = front;
do {
ret += arr[temp] + " ";
temp = (temp + 1) % arr.length;
} while (temp != rear);
ret += "]";
return ret;
}
}
Solution 3:[3]
Your code is overly and dumbly complex. I think you don't understand circular-queues well.
Here's my simplified code. You can check it out and learn something.
#include<stdio.h>
#include<stdlib.h>
typedef struct _node {
int size,front,rear,*q;
} node;
node *pu;
void initialize() {
if(pu!=NULL)
free(pu);
pu = (node *)malloc(sizeof(node));
printf("\nEnter the size of the queue :- ");
scanf(" %d",&pu->size);
pu->q = (int *)malloc(sizeof(int) * pu->size +1);
pu->front = pu->rear = 0;
}
int isempty() {
return (pu->front == pu->rear);
}
int isfull() {
return ((pu->rear + 1) % pu->size == pu->front);
}
void enqueue(int x) {
if(isfull())
return;
else {
pu->q[pu->rear=(pu->rear +1) % pu->size] = x;
}
}
int dequeue() {
if(isempty())
return '$';
else {
return pu->q[ pu->front = (pu->front + 1) % pu->size];
}
}
void display() {
if(isempty())
return;
else {
for( int i = pu->front + 1; i != (pu->rear +1)%pu->size ; i = ( i +1) % pu->size)
printf("\n %d",pu->q[i]);
}
}
int main() {
// do something in here with the functions.
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user3435121 |
Solution 2 | Nisab Mohd |
Solution 3 | the trickster |