Example program for queue
Program:
/*
* This program is
written to obtain the job application form and to later verify the forms
* using queue
implementation.
#include
<stdio.h>
#include
<conio.h>
#include
<stdlib.h>
enum BOOL{ FALSE, TRUE
};
typedef int BOOL;
struct HEAD_q;
struct NODE_q;
struct JobApplication;
typedef struct HEAD_q*
Head_q;
typedef struct NODE_q*
ptrNode_q;
typedef struct NODE_q
Node_q;
typedef struct HEAD_q*
Position_hq;
typedef struct NODE_q*
Position_nq;
typedef Head_q Queue;
typedef struct
JobApplication JobApp;
typedef JobApp
DataType; // the employee record data type
struct JobApplication{
unsigned int AppNo; // application number
char name[50];
char age[5];
char designation[50];
char MarksScoredIn10th[5];
char MarksScoredIn12th[5];
char MarksScoredInUniversity[5];
char UniversitySubject[100];
};
struct HEAD_q{ // in
this implementation
// there is a separate structure for the
header and the other nodes
ptrNode_q front;
ptrNode_q rear;
};
struct NODE_q{
DataType data;
Position_nq next;
Position_nq previous;
};
BOOL IsEmpty_q(Head_q
Q)
{
return (Q->front == NULL &&
Q->rear == NULL);
}
Head_q NewQueue()
{// returns a new
header
Head_q q = malloc(sizeof(struct HEAD_q));
q->front = q->rear = NULL; // Queue
is empty
return q;
}
ptrNode_q NewNode()
{// returns a new node
unlike the linked list implementation were the
// header is same as the node.
return malloc(sizeof(Node_q));
}
void
Delete_q(ptrNode_q n)
{
// to do: add code to clear the other parts
of the
// values field present in n, and which are
dynamically
// allocated (in this example its not
needed)
free(n);
}
void Enqueue(Head_q Q,
DataType data)/// this operation runs in O(1)
{
ptrNode_q temp;
temp = NewNode();
if (temp == NULL)
{
fprintf(stderr, "error: request
to allocate memory using function \"malloc\" declined");
return;
}
temp->data = data;
if (Q->front == NULL &&
Q->rear == NULL)
{ // if the front and the rear are null.
Q->front = Q->rear =
temp->next = temp->previous = temp;
return; // returns when insertion is
over
}
temp->next = Q->front;
Q->front = temp; // modify the header
pointer
temp->next->previous = temp;
temp->previous = Q->rear; //
connects back to the rear circularly
Q->rear->next = temp;
}
DataType
DeQuieue(Head_q Q) /// this operation runs in O(1)
{// this deletes the
dataField in the rearer and returns its containing
// value.
// its more like a circularly linked list
implementation
if (Q->front == NULL || Q->rear ==
NULL)
{
fprintf(stderr, "error: queue
is empty");
DataType null_JobApp;
null_JobApp.AppNo = 0;
return null_JobApp;
}
ptrNode_q temp = Q->rear;
DataType retVal = temp->data;
if (temp->next == temp)
{
Q->front = Q->rear = NULL;
Delete_q(temp);
return retVal;
}
temp->previous->next =
temp->next;
temp->next->previous =
temp->previous;
Q->rear = temp->previous;
Delete_q(temp);
return retVal;
}
void
disposeQueue(Head_q Q)
{// deletes all
elements in the queue
ptrNode_q n = Q->front;
ptrNode_q temp;
Q->rear->next = NULL;
while (n != NULL)
{
temp = n->next;
Delete_q(n);
n = temp;
}
Q->front = Q->rear = NULL;
}
JobApp
GetJobApplication()
{
JobApp app;
printf("\nenter the name of the
applicant: ");
fflush(stdin); gets(app.name);
printf("enter the age of the
applicant: ");
fflush(stdin); gets(app.age);
printf("enter the designation:
");
fflush(stdin); gets(app.designation);
printf("enter the marks scored in 10th
exam: ");
fflush(stdin); gets(app.MarksScoredIn10th);
printf("enter the marks scored in the
12th exam: ");
fflush(stdin); gets(app.MarksScoredIn12th);
printf("enter the marks scored in
university: ");
fflush(stdin); gets(app.MarksScoredInUniversity);
printf("enter the subject opted for in
the university: ");
fflush(stdin); gets(app.UniversitySubject);
return app;
}
void
DisplayjobApp(JobApp app)
{
printf("application no.%d",
app.AppNo);
printf("\nname of the applicant:
%s",app.name );
printf("\nage of the applicant:
%s",app.age);
printf("\nenter the designation:
%s", app.designation);
printf("\nmarks scored in 10th exam:
%s", app.MarksScoredIn10th);
printf("\nmarks scored in the 12th
exam: %s", app.MarksScoredIn12th);
printf("\nmarks scored in university:
%s", app.MarksScoredIn12th);
printf("\nsubject opted for in the
university: %s", app.UniversitySubject);
}
BOOL
WriteAppToFile(JobApp app)
{
FILE* f = fopen("accepted applications.storage",
"a+b");
if(f == NULL)
{
fprintf(stderr, "Error opening
file applications.storage for writing");
return FALSE;
}
fwrite(&app,sizeof(app), 1, f);
fclose(f);
return TRUE;
}
void DisplayAcceptedApplications()
{
JobApp app;
FILE* f = fopen("accepted
applications.storage", "rb");
if(f == NULL)
{
fprintf(stderr, "Error opening
file applications.storage for reading");
return;
}
while(1)
{
fread(&app, sizeof(app), 1, f);
printf("\n");
if(feof(f)!=0)
break;
DisplayjobApp(app);
printf("\n");
}
fclose(f);
}
char menu()
{
char ch;
system("cls");
printf("menu:");
printf("\n1. to enter
application");
printf("\n2. to accept or reject
application");
printf("\n3. to display all accepted
applications");
printf("\n4. exit");
ch = getch();
return ch;
}
int getApplicationNo()
{
int finalAppno;
FILE* f= fopen("accepted
applications.storage","rb");
if(f==NULL)
return 0;
fseek(f, -(int)sizeof(JobApp), SEEK_END);
fread(&finalAppno,sizeof(finalAppno),1,
f);
return finalAppno;
}
int main()
{
Queue Q = NewQueue();
char ch, ch2; int noOfApplicants =
getApplicationNo();
JobApp temp;
while(1)
{
ch = menu();
switch(ch)
{
case '1':
temp = GetJobApplication();
temp.AppNo = ++noOfApplicants;
Enqueue(Q, temp);
break;
case '2':
temp = DeQuieue(Q);
if(temp.AppNo == 0)
{
printf("\napplication
queue is empty. Please wait for more applications:");
getch();
continue;
}
DisplayjobApp(temp);
printf("\n do you want to
accept application (Y/N)?");
ch2 = getch();
if(ch2 == 'Y' || ch2 == 'y')
WriteAppToFile(temp);
break;
case '3':
printf("list of all accepted
applications: ");
DisplayAcceptedApplications();
getch();
break;
case '4':
return 0;
default:
printf("invalid option
%c",ch);
getch();
}
}
return 0;
No comments:
Post a Comment