queue.c
#pragma GCC optimize 0
#include "sys.h"
#define MAXNUM 10
struct testQuene arrayQuene[MAXNUM];
int first_in, last_out;
char *str;
void _quene()
{
struct testQuene *quene;
quene = arrayQuene;
_initQuene(quene);
test(quene, 1, 10);
_findQuene(quene, 588);
test(quene, 2, 1);
test(quene, 0, 4);
test(quene, 1, 3);
test(quene, 0, 9);
}
void ss(struct testQuene *q)
{
q->data = 55;
}
void _initQuene(struct testQuene *q)
{
first_in = 0;
last_out = 0;
for (int i = 0; i < MAXNUM; i++)
{
q[i].data = i;
}
}
struct testQuene *_addQuene(struct testQuene *q, int addNum)
{
str = "overflow";
if (!_isOverflow(1))
return q;
q[first_in].data = addNum;
_loopPointer(1);
str = "successful";
return q;
}
struct testQuene *_deleteQuene(struct testQuene *q)
{
str = "failed,lack of data!";
if (!_isOverflow(0))
return q;
q[last_out].data = 0;
_loopPointer(0);
str = "successful";
return q;
}
int _isOverflow(int n)
{
switch (n)
{
case 0:
return last_out == first_in ? 0 : 1;
case 1:
return (first_in + 1) % MAXNUM == last_out ? 0 : 1;
}
}
void _loopPointer(int n)
{
switch (n)
{
case 0:
last_out++;
last_out %= MAXNUM;
break;
case 1:
first_in++;
first_in %= MAXNUM;
break;
}
}
void _findQuene(struct testQuene *q, int findNum)
{
for (int i = last_out; i < first_in; i++)
{
if (q[i].data == findNum)
{
str = "I found it already!";
break;
}
else
str = "not exist!!";
}
}
struct testQuene *_changeQuene(struct testQuene *q, int oldNum, int newNum)
{
for (int i = last_out; i < first_in; i++)
{
if (q[i].data == oldNum)
{
q[i].data = newNum;
str = "change successful!";
break;
}
else
str = "not exist!!";
}
return q;
}
void test(struct testQuene *quene, int n, int num)
{
switch (n)
{
case 0:
for (int i = 0; i < num; i++)
quene = _deleteQuene(quene);
break;
case 1:
for (int i = 0; i < num; i++)
quene = _addQuene(quene, 555 + i);
break;
case 2:
for (int i = 0; i < num; i++)
quene = _changeQuene(quene, 557, 666);
break;
}
}
sys.c
#pragma GCC optimize 0
#include "main.h"
void setup(void)
{
}
int i = 0;
void loop()
{
i++;
_quene();
}
sys.h
#ifndef _SYS_H
#define _SYS_H
void setup(void);
void loop();
struct testQuene
{
int data;
};
void _initQuene(struct testQuene *q);
struct testQuene *_addQuene(struct testQuene *q, int addNum);
struct testQuene *_deleteQuene(struct testQuene *q);
void _findQuene(struct testQuene *q, int findNum);
struct testQuene *_changeQuene(struct testQuene *q, int oldNum, int newNum);
int _isOverflow(int n);
void _loopPointer(int n);
void test(struct testQuene *quene, int n, int num);
#endif