Thread: Queue testing problems

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    69

    Queue testing problems

    New to the C language and trying to write a test program for my queue, just wondering what I'm doing wrong. Im not really sure if i'm referencing things correctly.

    here are my compiler errors:

    gcc -Wall -ansi -pedantic queue.c queue.h test.c
    queue.c: In function âdequeueâ:
    queue.c:34: warning: passing argument 1 of âemptyâ from incompatible pointer type
    queue.c:48: warning: cast from pointer to integer of different size
    queue.c: In function âprintArrâ:
    queue.c:73: warning: format â%dâ expects type âintâ, but argument 2 has type âint *â
    queue.c: In function âfreeArrâ:
    queue.c:84: error: expected declaration or statement at end of input
    queue.c:84: error: expected declaration or statement at end of input

    queue.c
    Code:
    #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->arr))
    	{
    		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 (int)qq->arr;
    }
    
    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);
    	} 
    }
    
    void freeArr( Queue * qq)
    {
    	if(qq != NULL)
    	{
    		free(qq->arr);
    		free(qq);
    	{
    }

    queue.h
    Code:
    typedef struct
    {
    	int cap;
    	int *arr;
    	int front;
    	int back;
    	int elements;
    } Queue;
    
    void init (Queue * qq);
    void enqueue( Queue * qq, int num );
    int dequeue( Queue * qq);
    int empty (Queue * qq);
    int length ( Queue * qq);
    void printArr(Queue * qq);
    void freeArr ( Queue * qq);
    test.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "queue.h"
    
    int main()
    {
    
    	int choose = 0;
    	int num;
    	Queue q;
    
    	printf("welcome to the queue ........");
    	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");
    	
    	scanf("%d", &choose);
    
    	switch(choose)
    	{
    		case 1:
    			scanf("Enter a number to engueue: %d\n", &num);
    			enqueue(&q, num);
    			break;
    		case 2:
    			dequeue(&q);
    			break;
    		case 3:
    			printArr(&q);
    			break;
    		case 4:
    			empty(&q);
    			break;
    		case 5:
    			length(&q);
    			break;
    		case 6:
    			freeArr(&q);
    			break; 
    	}
    return 0;
    }

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    In dequeue(): if(empty(qq->arr)) should probably be if(empty(qq)) since empty expects Queue*
    You probably want to change the return statement of this function so you're actually returning the first value of qq->arr instead of an int cast of the pointer qq->arr.
    
    In printArr(): You have to dereference qq->arr and somehow use i so that you can iterate through the array...you don't want to print out the same pointer address qq->element times.
    
    In freeArr():  You have two {'s for the if statement, the last one needs to be a }
    The nags about using code tags are really annoying. I shouldn't have had to of used them in this case.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    	if(empty(qq->arr))
    You've declared "empty" to accept parameters of type "Queue*", but your passing qq->arr, which is type "int*". Either just pass "qq" or change "empty" to accept the different parameter.
    Code:
    return (int)qq->arr;
    "dequeue" is declared to return type "int" but your returning "qq->arr" which is type "int*". Also, after you fix it (by deciding what you want to return--and stick with it, you shouldn't have to cast anything.).
    Code:
    		printf("%d ", qq->arr);
    Again, same problem. Your telling "printf" to print an integer, but passing an "int*".

    In "freeArr", you have a "{" when it should be a closing "}".

  4. #4
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    By the way, you don't compile header files.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    Thanks ill fix my code and report back to let you know how things are going.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    Why don't you compile headers?

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    They just get included, they aren't directly compiled. They contain type definitions and function prototypes for some other .c file. In any case, headers are taken care of simply by using #include. You don't need to supply them at the command-line.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    ahhh ok, so i got all the errors corrected but i don't think im calling my queue methods correctly in my test. cause when i try to enqueue i get *** glibc detected *** ./a.out: free(): invalid pointer: 0x000000386241bbc0. and a bunch of other jargon

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I'd post all of your updated code. When changing from "int*"s to "int"s did you blindly make the change, or change the logic also?

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    test.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "queue.h"
    
    int main()
    {
    
    	int choose = 0;
    	int num;
    	Queue q;
    
    	printf("welcome to the queue ........\n");
    	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("7. Exit\n");
    	
    	scanf("%d", &choose);
    
    	switch(choose)
    	{
    		case 1:
    			scanf("Enter a number to engueue: %d\n", &num);
    			enqueue(&q, num);
    		case 2:
    			dequeue(&q);
    		case 3:
    			printArr(&q);
    		case 4:
    			empty(&q);
    		case 5:
    			length(&q);
    		case 6:
    			freeArr(&q);
    		case 7: 
    			break;
    	}
    return 0;
    }
    queue.c
    Code:
    #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)
    {
    	if(qq != NULL)
    	{
    		free(qq->arr);
    		free(qq);
    	}
    }

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Well you have an "init" function, to properly initialize all of the fields of a "Queue", but you never call it. Give that a shot.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    ok lemme try that

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Also, in "freeArr", you're "free"ing "qq", which is not correct, since you never "malloc"ed it. Only free stuff that you malloc/realloc. Since its called "freeArr", it should only free the array!

  14. #14
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    is this what you ment?

    Code:
    int main()
    {
    
    	int choose = 0;
    	int num;
    	Queue q;
    	init(&q);
    
    	printf("welcome to the queue ........\n");
    	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("7. Exit\n");
    	
    	scanf("%d", &choose);
    
    	switch(choose)
    	{
    		case 1:
    			scanf("Enter a number to engueue: %d\n", &num);
    			enqueue(&q, num);
    		case 2:
    			dequeue(&q);
    		case 3:
    			printArr(&q);
    		case 4:
    			empty(&q);
    		case 5:
    			length(&q);
    		case 6:
    			freeArr(&q);
    		case 7: 
    			break;
    	}
    return 0;
    }

  15. #15
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    ok its not erroring out but i still have to fix some things, if i need some help ill be back. Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pushing a Queue onto a Stack?
    By MiroMage in forum C Programming
    Replies: 5
    Last Post: 10-14-2008, 09:23 PM
  2. Need help quick, compiler/file problem.
    By alex0486 in forum C++ Programming
    Replies: 7
    Last Post: 09-28-2005, 01:03 AM
  3. ADT queue
    By ^xor in forum C Programming
    Replies: 4
    Last Post: 06-15-2005, 05:43 AM
  4. Queue and Stack: Comparing help!!! (palindrome)
    By advocation in forum C++ Programming
    Replies: 8
    Last Post: 03-09-2005, 08:46 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM