Can you help me find out error

Revision en1, by ShauryaH4X, 2024-11-12 18:08:23
#include<stdio.h>
#include<stdlib.h>

typedef int itype;

typedef struct Cq{
    int size,count,front,rear;
    itype *a;
}Cq;

void initialize(Cq * ref,int n) {
    ref->count=0;
    ref->size=n;
    ref->front=0;
    ref->rear=-1;
    ref->a=(int *)malloc(n*sizeof(int));
    if(ref->a==NULL) {
        printf("Memory not allocated");
        exit(1);
    }
    printf("1");
}

char isfull(Cq *ref) {
    return ref->count==ref->size;
}

char isempty(Cq *ref) {
    return ref->count==0;
}

void insert(Cq *ref,itype x) {
    if(isfull(ref)) {
        printf("Queue full");
        exit(2);
    }
    ref->rear=(ref->rear+1)%(ref->size);
    ref->a[ref->rear]=x;
    ref->count=ref->count+1;
    printf("2");
}
int size(Cq *ref) {
    return ref->count;
}

int main() {
    Cq a;
    initialize(&a,5);
    insert(&a,10);
    insert(&a,20);
    insert(&a,30);
    insert(&a,40);
    return 0;
}

I was asked to make circular queue with count in my proficiency test today but it was giving me error. Can you help me find out error in this code.

Tags data structures, queue

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English ShauryaH4X 2024-11-12 18:10:58 639 Tiny change: 'd\n\n~~~~~' -> 'd\n\n~~~~~\n\nLooks like segmentation fault but where' (published)
en1 English ShauryaH4X 2024-11-12 18:08:23 1147 Initial revision (saved to drafts)