/*main.c*/
#include "stdio.h"
#include "Queue.h"
#include <iostream>
using namespace std;
int num = 0;
int main()
{
CQueue Test1;
static int number = 0;
while(1)
switch (num)
{
case 1:
number++;
Test1.QueuePush(number);
cout << "\nHead is " << Test1.QueueGetFirstIdx() << endl;
num = 0;
break;
case 2:
Test1.QueuePop();
cout << "\nHead is " << Test1.QueueGetFirstIdx() << endl;
num = 0;
break;
case 3:
cout << "\nHead is " << Test1.QueueGetFirstIdx() << endl;
break;
default:
break;
}
}
/*Queue.cpp*/
#include "Queue.h"
bool CQueue::QueuePush(long date)
{
unsigned short tmp;
tmp = (tail + 1) & (QUEUE_ELEM_NUM - 1);
if (tmp == head)
return false;
tail = tmp;
Data[tmp] = date;
num++;
return true;
}
//------------------------------------------------------------------------------
bool CQueue::QueuePop()
{
if (tail == head)
return false;
head = (head + 1) & (QUEUE_ELEM_NUM - 1);
Data[head] = 0;
num--;
return true;
}
//------------------------------------------------------------------------------
unsigned short CQueue::QueueGetFirstIdx()
{
return ((head + 1) & (QUEUE_ELEM_NUM - 1));
}
/*Queue.h*/
#ifndef __QUEUE_H__
#define __QUEUE_H__
#ifdef __cplusplus
extern "C" {
#endif
#define QUEUE_ELEM_NUM (4) //队列元素数量
class CQueue
{
public:
bool QueuePush(long date);
bool QueuePop();
unsigned short QueueGetFirstIdx();
private:
int Data[QUEUE_ELEM_NUM] = { 0 };
unsigned short head = 0;
unsigned short tail = 0;
unsigned short num = 0;
};
#ifdef __cplusplus
}
#endif
#endif //__QUEUE_H__