Help for Problem in C programming regarding Queues..

spstephen09's Avatar, Join Date: Jan 2008
Newbie Member
Hey, i'm still a student, and we have this homework about creating the queue functions: createQueue, destroyQueue, enqueue, dequeue, queueFront, queueRear, queueCount, emptyQueue, and fullQueue.. in Turbo C

we partially made a program of this, but it has a problem, the destroyQueue function won't work.. we are really having a hard time on this since it's our first time handling this kind of ADT, plus, using Linked Lists.. we really need help on how to solve this one.. hope somebody could help us..

(btw, a friend of mine made this, he just shared it with us..)
Code:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1

typedef int *eltyp;
typedef struct queue *queuePtr;
typedef struct node *nodePtr;

struct queue{
	int count;
	nodePtr front;
	nodePtr rear;
};
struct node{
	eltyp *dataPtr;
	nodePtr link;
};

queuePtr createQueue();
fullQueue();
emptyQueue(queuePtr j);
queueCount(queuePtr j);
dequeue(queuePtr j);
enqueue(eltyp x,queuePtr j);
queueFront(queuePtr j);
queueRear(queuePtr j);
destroyQueue(queuePtr j);

main()
{
queuePtr *j;
int num,enq,emq,deq,desq,front,fuq,rear,count;
char choice;
nodePtr temp;
eltyp x,y;
x=&num;
for(;;)
{
	clrscr();
	gotoxy(18,4); printf("If you don't have a node, please create one.");
	gotoxy(38,7); printf("Menu");
	gotoxy(30,9); printf("[0]Create Node");
	gotoxy(30,10); printf("[A]Enqueue");
	gotoxy(30,11); printf("[B]Dequeue");
	gotoxy(30,12); printf("[C]Queue Front");
	gotoxy(30,13); printf("[D]Queue Rear");
	gotoxy(30,14); printf("[E]Queue Count");
	gotoxy(30,15); printf("[F]Empty Queue");
	gotoxy(30,16); printf("[G]Full Queue");
	gotoxy(30,17); printf("[H]Destroy Queue");
	gotoxy(30,18); printf("[X]Exit");
	choice=toupper(getch());
	switch(choice)
	{
		case '0':
		{
			clrscr();
			j=createQueue();
			printf("\nNode Successfully Created");
			getch();
		}
		break;
		case 'A':
		{
			clrscr();
			printf("Enter number to be stored: ");
			scanf("%d",&num);
			enq=enqueue(x,j);
			if(enq==TRUE)
			{
				printf("\nData Storage Successful");
			}
			else
				printf("\nData Storage Unsuccessful");
			getch();
		}
		break;
		case 'B':
		{
			clrscr();
			if(!emptyQueue(j))
			{
				deq=dequeue(j);
				printf("\nData from queue: %d",deq);
			}
			else
				printf("\nQueue is empty");
			getch();
		}
		break;
		case 'C':
		{
			clrscr();
			if(!emptyQueue(j))
			{
				front=queueFront(j);
				printf("\nQueue front is: %d",front);
			}
			else
				printf("\nQueue is empty");
			getch();
		}
		break;
		case 'D':
		{
			clrscr();
			if(!emptyQueue(j))
			{
				rear=queueRear(j);
				printf("\nQueue rear is: %d",rear);
				getch();
			}
		}
		break;
		case 'E':
		{
			clrscr();
			count=queueCount(j);
			printf("\nQueue count is: %d",count);
			getch();
		}
		break;
		case 'F':
		{
			clrscr();
			emq=emptyQueue(j);
			if(emq==TRUE)
				printf("Queue is empty");
			else
				printf("Queue is not empty");
			getch();
		}
		break;
		case 'G':
		{
			clrscr();
			fuq=fullQueue(j);
			if(fuq==TRUE)
				printf("Queue is full");
			else
				printf("Queue is not full");
			getch();
		}
		break;
		case 'H':
		{
			clrscr();
			destroyQueue(j);
			printf("\nNode Destroyed");
			getch();
		}
		case 'X':
			exit(1);
		default:
		{
			gotoxy(30,20); printf("Invalid Choice");
			getch();
		}
		break;
}
}
}

queuePtr createQueue()
{
queuePtr j;
j=(queuePtr)malloc(sizeof(struct queue));
j->front=NULL;
j->rear=NULL;
j->count=0;
return j;
}

enqueue(eltyp x,queuePtr j)
{
nodePtr node;
node=(nodePtr)malloc(sizeof(struct node));
if(!node)
	return FALSE;
node->dataPtr=*x;
node->link=NULL;

if(j->count==0)
{
	j->front=node;
	j->rear=node;
}
else
{
	j->rear->link=node;
	j->rear=node;
}
(j->count)++;
return TRUE;
}

dequeue(queuePtr j)
{
int val;
nodePtr temp;
val=j->front->dataPtr;
temp=j->front;
j->front=j->front->link;
free(temp);
(j->count)--;
return val;
}
emptyQueue(queuePtr j)
{
if(j->count==0)
	return TRUE;
else
	return FALSE;
}

queueFront(queuePtr j)
{
int val;
if(!emptyQueue(j))
{
	val=j->front->dataPtr;
	return val;
}
}

queueRear(queuePtr j)
{
int val;
if(!emptyQueue(j))
{
	val=j->rear->dataPtr;
	return val;
}
}

queueCount(queuePtr j)
{
return j->count;
}

destroyQueue(queuePtr j)
{
if(j->count!=0)
{
	while(j->count!=0)
	{
		free(j->front);
	}
}
free(j);
}

fullQueue()
{
if(!malloc(sizeof(struct node)))
	return TRUE;
else
	return FALSE;
}


Last edited by shabbir; 14Jan2008 at 09:12.. Reason: Code block
0
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
Perhaps if you'd read this
http://www.go4expert.com/faq.php?faq=guidelines
or even bothered to press "preview post", you would have noticed that some of your code has been turned into smilies.
Not to mention the fact that all the indentation has been lost.