//array implementation of q
//qarray@standard c library(*SCL*) by ASHISH TIWARI
/*
You may print, store code from this program for your personal,
noncommercial use. By accessing this code, you agree not to reproduce,
distribute,
display or transmit information from this code in any form or by any means
without
the permission of ASHISH TIWARI, with this exception: You may occasionally
reproduce,
distribute, display or transmit an insubstantial portion of the information, for
a
noncommercial purpose, to a limited number of individuals. ASHISH TIWARI does
not warrant
the accuracy, completeness, timeliness, merchantability or fitness for a
particular purpose
of the cose,though every care has been taken in making this code and has been
compiled
on standard gcc compiler provided by GNU. ASHISH TIWARI shall not be liable for
any
errors or omissions in the information or decisions made or actions taken in
reliance
on the code.
"*SCL*" is a registered trademark of ASHISH TIWARI, IITKGP.
All contents of "*SCL*" is copyright ©2000-.... ,ASHISH TIWARI, IITKGP
unless otherwise noted. Reproduction in part or in whole without
permission is expressly prohibited.
*/
#include<stdio.h>
#include<stdlib.h>
#define max 10
struct tag{
int arr[max];
int rear,front;
};
typedef struct tag q;
q* initq(q* s);
int qempty(q* s);
int qfull(q* s);
void addq(q* s,int data);
int delq(q* s);
main()
{
int i;
q* s;
s= initq(s);
for(i=0;i<10;i++)
addq(s,i);
for(i=0;i<10;i++)
printf("%d\n",s->arr[i]);
i=delq(s);
printf("\n%d\n",i);
i=delq(s);
printf("\n%d\n",i); i=delq(s);
}
q* initq(q* s)
{
s=(q*)malloc(sizeof(q));
s->rear=-1;
s->front=-1;
return(s);
}
int qempty(q* s)
{
if(s->front==-1 && s->rear==-1)
return(1);
else
return(0);
}
int qfull(q* s)
{
if(s->rear==max-1)
return(1);
else
return(0);
}
void addq(q* s,int data)
{
if(qfull(s))
{
printf("q is full\n");
exit(1);
}
else
s->arr[++(s->rear)]=data;
if(s->front==-1)
s->front=0;
}
int delq(q* s)
{
int data;
if(qempty(s))
{
printf("q is empty\n");
exit(1);
}
else
data=s->arr[(s->front)++];
if(s->front==s->rear)
{
data=s->arr[(s->front)];
s->front=-1;
s->rear=-1;
}
return(data);
}