1 Replies - 1944 Views - Last Post: 08 December 2009 - 12:05 AM Rate Topic: -----

#1 chocrose   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-April 09

Circular Queue in C

Post icon  Posted 07 December 2009 - 03:24 PM

My assignment is to write a circular queue in C, my problem is that when I have filled the queue and then emptied it the program crashes when I then try to add another value to the queue. Can anybody see why. Thanks.
// pre-processor directives
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define MAXQ 5
#define TRUE 1
#define FALSE 0
// user-defined types
typedef struct 
 {
		int head;
		int tail;
		int size;
		float entry[MAXQ];
 }circularQADT;

//Function prototypes
void displayMenu();
void menuControl();
void gotoxy(int x, int y);
void initialiseQ(circularQADT*);
void displayQ(circularQADT*);
void addToQ(circularQADT * myqueue, float value);
void addControl(circularQADT *myqueue);
void deleteControl(circularQADT *myqueue);
void deleteFromQ(circularQADT *myqueue);
int fullQ(circularQADT*);
int emptyQ(circularQADT*);



int main()
{
	menuControl();
	return 0;
}
/******************************************************************************/
void displayMenu()
{
	 printf("\n\t\t Software Development: Array Data Structures");
	 printf("\n\t\t Assessment Task 2.1 - circular queue with counter");
	 printf("\n\n\t\t\t1. Initialise queue. ");
	 printf("\n\t\t\t2. Add to queue (append). ");
	 printf("\n\t\t\t3. Delete from queue (serve).");
	 printf("\n\t\t\t9. Quit\n");
}
/******************************************************************************/
void menuControl()//Displays the menu and q contents. Uses gotoxy
{
	 circularQADT oneQueue;
	 int choice;
	 initialiseQ(&oneQueue);
	 
	 do{
			 system("cls");
			 displayMenu();
			 gotoxy(1,19);
			 displayQ (&oneQueue);
			 gotoxy(17,11);
			 printf ("Make your selection > ");
			 choice = toupper(getch());
			 switch (choice)
			 {
				  case '1' : initialiseQ(&oneQueue);
							 printf("\nClearing queue\n");
							 system("pause");
							 break;
				  case '2' : addControl(&oneQueue);
							 break;
				  case '3' : deleteControl(&oneQueue);
							 break;
				  case '9' : printf("Exiting...");
							 sleep(800);
							 break;
				  default  : printf("Please enter a number between 1 and 3\n");
							 system("pause");
							 break;
				  
			 } //endswitch
	   } while (choice != '9');
} // end menuControl
/*******************************************************************************/
void initialiseQ(circularQADT* myqueue)
{
	 myqueue->tail = 0;
	 myqueue->head = 0;
	 myqueue->size = 0;
}
/*******************************************************************************/

void displayQ(circularQADT* myqueue)
{
	 int indx;
	 int i;
	 printf("\nQueue Size : %d", myqueue->size);
	 printf("\nQueue Head : %d", myqueue->head);
	 printf("\nQueue Tail : %d", myqueue->tail);
	 printf("\nItems in Queue : ");
	 if(myqueue->size == 0)
		 printf("Empty");
	 else
	 {
		 for(i=myqueue->tail-1; indx != myqueue->head; i--)
		 {
			   if( i < 0 )
					indx = i+MAXQ;
			   else
					indx = i;
			   printf("->%.2f", myqueue->entry[indx]);
		 }//end for
	 }//end else
}//end function
/*******************************************************************************/

void addControl(circularQADT *myqueue)
{
	 /*	Displays an error message if the queue
		is full, otherwise, prompts for a value
		and calls addToQ(). */
		float value;
		printf ("\nPlease enter a value to add to the queue\n");
			   scanf("%f", & value);
			   if (fullQ (myqueue))
				  {   
					  printf(" Sorry, the queue is full \n");
					  system ("PAUSE");
				  }
				  else
					  addToQ(myqueue, value);

}//end function
/*******************************************************************************/

void addToQ(circularQADT * myqueue, float value)
{
	myqueue->entry[myqueue->tail]=value;
	myqueue->tail++;
	myqueue->size++;
	
	 if (myqueue->tail > MAXQ)
		myqueue->tail =0;
		
		
}
/********************************************************************************/

void deleteControl(circularQADT *myqueue)
{
	/*	Displays an error message if the queue
		is empty, otherwise, calls deleteFromQ().*/
	if (emptyQ (myqueue))
	   {
			   printf(" Sorry, the queue is empty \n");
			   system ("PAUSE");
	   } 
	else
		deleteFromQ(myqueue);


}
/********************************************************************************/
void deleteFromQ(circularQADT *myqueue)
{
	 
   myqueue->entry [myqueue->head++];
   myqueue->size--;
   
   if (myqueue->head > MAXQ)
	  myqueue->head =0;
			  
}	
/********************************************************************************/  
	 
int fullQ(circularQADT* myqueue)
{
	if (myqueue->size < MAXQ)
	return FALSE;
	else
	return TRUE;

}
/*********************************************************************************/
int emptyQ(circularQADT* myqueue)
{
	if (myqueue->size <= 0)
	return TRUE;
	else
	return FALSE;

}
/*********************************************************************************/

void gotoxy(int x, int y)//gotoxy is a Borland only function so i've written a version for Dev
{
   COORD coord;
   coord.X = x;
   coord.Y = y;
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
/**************************************************************************************/





Is This A Good Question/Topic? 0
  • +

Replies To: Circular Queue in C

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5523
  • View blog
  • Posts: 14,523
  • Joined: 18-April 07

Re: Circular Queue in C

Posted 08 December 2009 - 12:05 AM

I am not quite sure you are implementing it this way, a circular queue typically has pointers to the next structure, not numbers. But anyways... when you are deleting you have to make sure that you are not going to hit your head value because then you will spin into an endless cycle. Remember they are circular after all. I would take a look at your delete function and also your display function seems to be pretty awkward.

Somewhere in here I should be seeing a point where you are checking to see if your head is equal to your tail and if so, reset back to an empty list. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1