循环队列是常用的数据结构,当即时信号转发过程中发生丢帧时,可以将其缓存到队列中来防止丢帧。队列的大小根据实际丢帧情况,以及任务的优先级来定。
/*顺序循环队列 C语言实现*/
#include <iostream>
typedef int elementType;
typedef struct QueueFIFO {
int capbility;
int queTop;
int rear;
elementType* array;
}que;
que* CreateQueue(int maxQueSize)
{
que* q;
q = (que*)malloc(sizeof(struct QueueFIFO));
q->capbility = maxQueSize+1;
q->array = (elementType*)malloc(sizeof(elementType) * maxQueSize);
q->queTop = 0;
q->rear = 0;
return q;
}
bool isQueFull(que* q)
{
if (((q->rear + 1) % (q->capbility)) == q->queTop)
{
return true;
}
else
{
return false;
}
}
bool isQuEempty(que* q)
{
if (q->queTop == q->rear)
{
return true;
}
else
{
return false;
}
}
void pushQue(que* q, elementType ele)
{
int i;
elementType temp;
if (isQueFull(q))
{
printf("full que, can't push");
}else
{
q->array[q->rear] = ele;
q->rear = (q->rear + 1) % (q->capbility);
}
}
elementType popQue(que* q)
{
elementType val;
if (isQuEempty(q))
{
printf("empty que, can't pop");
}
else
{
val = q->array[q->queTop];
q->queTop = (q->queTop+1) % (q->capbility);
}
return val;
}
void test01()
{
int i;
elementType a[9] = { 1,2,3,4,5,6,7,8,9 };
int m;
m = sizeof(a) / sizeof(elementType);
que* q = CreateQueue(m);
if (q == nullptr)
{
printf("create queue error");
return;
}
printf("入队 \n");
for (i = 0; i < m; i++)
{
pushQue(q, a[i]);
}
for (i = 0; i < m; i++)
{
a[i] = popQue(q);
}
for (i = 0; i < m; i++)
{
// printf("%d \t",a[i]);
printf("出队后数组a[%d] = %d \n", i, a[i]);
}
}