循环队列的实现

include <stdio.h>

include <malloc.h>

typedef struct Queue
{
int * pBase;
int front;
int rear;
}QUEUE;

void init(QUEUE * pQ)
{
pQ->pBase = (int *)malloc(sizeof(int) * 6);

pQ->front = 0;
pQ->rear = 0;

return;

}

bool full_queue(QUEUE * pQ)
{
if ((pQ->rear + 1)%6 == pQ->front)
return true;
else
return false;
}

bool enqueue(QUEUE * pQ, int val)
{
if (full_queue(pQ))
{
return false;
}
else
{
pQ->pBase[pQ->rear] = val;
pQ->rear = (pQ->rear + 1) % 6;

	return true;
}

}

void traverse_queue(QUEUE * pQ)
{
int i = pQ->front;

while (i != pQ->rear)
{
	printf("%d ", pQ->pBase[i]);
	i = (i + 1) % 6;
}
printf("\n");

return;

}

bool dequeue(QUEUE * pQ, int * pVal)
{
if (pQ->front == pQ->rear)
return false;
else
{
*pVal = pQ->pBase[pQ->front];
pQ->front = (pQ->front+1) % 6;
return true;
}
}

int main()
{
QUEUE Q;

init(&Q);
enqueue(&Q, 10);
enqueue(&Q, 89);
enqueue(&Q, 100);
traverse_queue(&Q);
	
int val;
dequeue(&Q, &val);
printf("出队的元素是:%d\n", val);

return 0;

}

上一篇:用优先队列构造Huffman Tree及判断是否为最优编码的应用


下一篇:最短路径问题之Dijkstra算法