EmbLogic's Blog

program for simple queue

@this a program for simple queue to push pop and display the elements of the queue
@

1.1
log
@Initial revision
@
text
@#include”header.h”
int main()
{
void *queue;
int f,r;
f=r=-1;
int ch;
do
{
printf(“1:push\n”);
printf(“2:pop\n”);
printf(“3:display\n”);
printf(“4:exit\n”);
printf(“enter your choice\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: push(&queue,&f,&r);
break;
case 2: pop(&queue,&f,&r);
break;
case 3: display(&queue,&f,&r);
break;
default: printf(“wrong choice\n”);
}
}while(ch != 4);
return 0;
}

#include”header.h”
static char ch=1;
int push(void **qa,int *fa,int *ra)
{
if(*ra >= MAX-1)
{
printf(“queue overflow\n”);
return -1;
}
if(*fa ==-1 && *ra == -1)
{
*qa=(char *)malloc(1);
(*fa)++;
(*ra)++;
*(char *)(*qa + *ra)=ch;
ch++;
return 0;
}
*qa=(char *)realloc(*qa, (*ra)+2);
(*ra)++;
*(char *)(*qa + *ra)=ch;
ch++;
return 0;
}

int pop(void **qa,int *fa,int *ra)
{
if(*fa >= MAX)
{
printf(“queue underflow\n”);
return -1;
}
printf(“the %d popped element is %d\n”,*fa, *(char *)(*qa + *fa));
(*fa)++;
return 0;
}
void display(void **qa,int *fa,int *ra)
{
int i;
for(i= *fa; i <= *ra ; i++)
{
printf(“the %d element is %d\n”,i,*(char *)(*qa + i));
}
}

@

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>