Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

# A algorithm of the queue

No replies to this topic

### #1 smilewithme

smilewithme

• Members
• 11 posts
• OFFLINE
•
• Local time:12:23 PM

Posted 07 April 2005 - 07:18 AM

#include<malloc.h>
#include<stdio.h>
struct qurecord
{int capacity;
int front; int rear; int size;
int *array;
};
typedef struct qurecord *qu;
void error(char *s)
{puts(s);
}
qu creat(int n)
{qu s; s=(qu)malloc(sizeof(struct qurecord));
s->capacity=n;
s->size=0;
s->front=0;
s->rear=n-1;
s->array=(int*)malloc(sizeof(int)*n);
return(s);
}
int isfull(qu s)
{if((s->size)==(s->capacity)) return(1);
else return(0);
}
int isempty(qu s)
{if((s->size)==0) return(1);
else return(0);
}
void getin(int n,qu s)
{ if(isfull(s))
error("The queue is full!\n");
else
{s->size=(s->size+1)%(s->capacity+1);
s->rear=((s->rear+1)%(s->capacity));
s->array[s->rear]=n;
}
}
int getout(qu s)
{int m;
if(isempty(s))
{error("The queue is empty\n");return(0);}
else
{m=s->array[s->front];
(s->size)--;
s->front=((s->front+1)%(s->capacity));
return(m);
}
}
void main()
{qu sl;
int n,i=0,j; printf("Input the capaciyy:");
scanf("%d",&n);
sl=creat(n);
for(i=0;i<n;i++)
{scanf("%d",&j);
getin(j,sl);
}
getin(6,sl);
for(i=0;i<n;i++)
printf("The num (%dth) getout is:%d\n",i,getout(sl));
}
The program is making for a kind of ADT(abstract data type) queue,which follows the rule of FIFO(first-in-first-out).
What about the algorithm of this program? Is it effient?