My queue and test file all compile fine and run fine. but when i try to free everything in my queue not gets freed. other than that everything enqueue's fine and dequeue's fine and prints fine.
queue.c
test.cCode:#include <stdio.h> #include <stdlib.h> #include "queue.h" void init ( Queue * qq) { qq->cap = 26; qq->arr = malloc( sizeof(int) * qq->cap); qq->front = 0; qq->back = 0; qq->elements = 0; } void enqueue( Queue * qq, int num) { if(qq->cap == qq->elements) { qq->arr = realloc(qq->arr, sizeof(int) * qq->cap); qq->arr[qq->back++] = num; qq->elements++; } else { qq->arr[ qq->back++ ] = num; qq->elements++; } } int dequeue( Queue * qq) { int i; if(empty(qq)) { printf("ITS EMPTY!!\n"); } else { qq->elements--; qq->arr[qq->front] = 0; for(i=0; i<qq->elements; i++) { qq->arr[i] = qq->arr[i+1]; } } return qq->arr[i]; } int empty( Queue * qq) { int test = 0; if(qq->arr == NULL) { test = 1; } return test; } int length(Queue * qq) { return qq->elements; } void printArr(Queue * qq) { int i; for(i=0; i<qq->elements; i++) { printf("%d ", qq->arr[i]); } } void freeArr( Queue * qq) { free(qq->arr); }
Code:#include <stdio.h> #include <stdlib.h> #include "queue.h" int main() { int choose = 1; int num; char nl; Queue q; init(&q); printf("welcome to the queue ........\n"); while ( choose != 0 ) { printf("1. enqueue\n"); printf("2. dequeue\n"); printf("3. print\n"); printf("4. Check if queue is empty\n"); printf("5. get length\n"); printf("6. free queue\n"); printf("0. Exit\n"); scanf("%d%c", &choose, &nl); switch(choose) { case 1: printf("\n"); printf("Enter a number to enqueue:"); scanf("%d", &num); enqueue(&q, num); break; case 2: dequeue(&q); break; case 3: printArr(&q); printf("\n"); break; case 4: empty(&q); break; case 5: length(&q); break; case 6: freeArr(&q); break; case 0: break; } } return 0; }



LinkBack URL
About LinkBacks


