데이터 구조의 -- 배열 기반 순환 대기 열
2083 단어 데이터 구조
#include
#include
#include
#define LEN 6 //
#define true 1
#define false 0
typedef int bool;
// ,
typedef struct Queue
{
int front;
int rear;
int * pBase;
}QUEUE,PQUEUE;
void init(QUEUE *);
bool en_queue(QUEUE *,int val);
void traverse_queue(QUEUE*);
bool full_queue(QUEUE *);
bool out_queue(QUEUE*,int *);
bool isempty_queue(QUEUE *);
//
int main()
{
QUEUE Q;
init(&Q);
en_queue(&Q,1);
en_queue(&Q,2);
en_queue(&Q,3);
en_queue(&Q,4);
en_queue(&Q,5);
en_queue(&Q,6);
traverse_queue(&Q);
int val;
out_queue(&Q,&val);
printf(" :%d
",val);
traverse_queue(&Q);
out_queue(&Q,&val);
printf(" :%d
",val);
traverse_queue(&Q);
en_queue(&Q,3);
en_queue(&Q,4);
en_queue(&Q,5);
en_queue(&Q,6);
traverse_queue(&Q);
return 0;
}
//
void init(QUEUE * pQ)
{
pQ->pBase = (int *)malloc(sizeof(int)*LEN);
pQ->front = pQ->rear = 0;
}
//
bool en_queue(QUEUE* pQ,int val)
{
if(full_queue(pQ))
{
return false;
}
else{
pQ->pBase[pQ->rear] = val;
pQ->rear = (pQ->rear+1)%LEN;
}
}
//
void traverse_queue(QUEUE* pQ)
{
if(isempty_queue(pQ))
return;
int i = pQ->front;
while(i!=pQ->rear)
{
printf("%d ",pQ->pBase[i]);
i = (i+1)%LEN;
}
printf("
");
}
//
bool full_queue(QUEUE * pQ)
{
if((pQ->rear+1)%LEN == pQ->front)
return true;
else
return false;
}
//
bool isempty_queue(QUEUE *pQ)
{
if(pQ->front == pQ->rear)
return true;
else
return false;
}
//
bool out_queue(QUEUE* pQ ,int *pVal)
{
if(isempty_queue(pQ))
return;
*pVal = pQ->pBase[pQ->front];
pQ->front = (pQ->front+1)%LEN;
}