/*data_filter.h*/
/*数据包过滤判断-数组队列方式*/
#ifndef _DATA_FILTER_H#define _DATA_FILTER_H
#define GM_UINT32 int
#define MAXSIZE 5 //队列中元素最大个数
typedef struct node
{
GM_UINT32 front;
GM_UINT32 rear;
GM_UINT32 count;
GM_UINT32 elements[MAXSIZE];
} QUEUE;
void creat( QUEUE *queue ); /*队列的初始化函数*/
int is_full( QUEUE queue ); /*判读队列是否已满*/
void in_queue( QUEUE *queue, int value ); /*队列的插入函数*/
int out_queue( QUEUE *queue ); /*出队函数*/
void printqueue( QUEUE queue );
int findnum(QUEUE *queue,int num);
/*数据包过滤判断函数,返回值为1,表示已经过滤,返回值为0,表示没有被过滤*/
int datapose(QUEUE *queue,int data);
#endif /*_DATA_FILTER_H*/
/*data_filter.c*/
//循环队列(数组实现)实现对冗余数据包的过滤
#include <stdlib.h>
#include <stdio.h>
#include "data_filter.h"
void creat( QUEUE *queue ) /*队列的初始化函数*/
{
queue -> front = 0;
queue -> rear = 0;
queue -> count = 0;
//queue -> MaxSize = MAXSIZE - 1;
}
int is_full( QUEUE queue ) /*判读队列是否已满*/
{
if ( queue.count == MAXSIZE )
{
return 1;
}
else
{
return 0;
}
}
void in_queue( QUEUE *queue, int value ) /*队列的插入函数*/
{
if ( queue -> count == MAXSIZE ) //可以利用前面的is_full函数进行判断
{
printf("sorry! the queue is full! no space is available!\n");
}
else
{
if ( queue -> count == 0 )
{
//主要是处理队列为空时的入队列
queue -> elements[queue -> rear] = value;
queue -> count += 1;
}
else
{
queue -> rear = (queue -> rear + 1) % MAXSIZE;
queue -> elements[queue ->rear] = value;
queue -> count += 1;
}
}
}
int out_queue( QUEUE *queue ) /*出队函数*/
{
if ( queue -> count == 0 ) //可以利用前面的is_empty函数进行判断
{
printf("sorry! the queue is empty! no number is available! \n");
exit(EXIT_FAILURE);
}
else
{
queue -> front = (queue -> front + 1) % MAXSIZE;
queue -> count -= 1;
return queue -> elements[queue -> front];
}
}
//print queue 队列内部元素打印输出函数
void printqueue( QUEUE queue )
{
int i = 1/*, count*/;
if ( queue.count == 0 )
{
printf("the queue is empty! no number can be printed! \n");
}
else
{
//count = queue.count;
printf("the number of queue is %d \n", queue.count);
for ( i; i <= queue.count; i++ )
{
printf("the %d number is %d \n", i, queue.elements[(queue.front + i - 1) % MAXSIZE]);
//printf("the %d number is %d /n", i, queue.elements[queue.front + i - 1]);
}
}
}
/*********************************新增加函数**************************************/
int findnum(QUEUE *queue,int num)
{
int result = 0;
int i = 1/*, count*/;
if ( queue->count == 0 )
{
printf("the queue is empty! no number can be printed! \n");
result = 0;
}
else
{
for ( i; i <= queue->count; i++ )
{
//printf("the %d number is %d \n", i, queue->elements[(queue->front + i - 1) % MAXSIZE]);
if(num == queue->elements[(queue->front + i - 1) % MAXSIZE])
{
result = 1;//找到了
printf("the num:%d is found in the queue \n",num);
break;
}
}
}
return result;
}
/*数据包过滤判断函数,返回值为1,表示已经过滤,返回值为0,表示没有被过滤*/
int datapose(QUEUE *queue,int data)
{
int result = 0;
if(0 == findnum(queue,data))//没有该数据,
{
if(is_full(*queue)==0) //且队列中不满
{
in_queue(queue,data); //进队
printf("数据包%d入队\n",data);
}
else //队列已满,先出队,再进队
{
out_queue(queue); /*出队,删除队头*/
in_queue(queue,data); //再进队
printf("数据包%d入队\n",data);
}
result = 0;
}
else //队列中有该数据
{
printf("数据包%d已过滤\n",data);
result = 1;
}
return result;
}
/*主函数main,测试函数*/
#include<stdio.h>
#include<stdlib.h>
#include"data_filter.h"
#define DataNum 20 //数据包个数
/*test functios*/
void main(void)
{
int a[DataNum],i;
QUEUE queue;
creat(&queue);
for(i = 0 ;i<DataNum;i++)
{
printf("请输入数据包%d\n",i+1);
scanf("%d",&a[i]);
}
printf("请输出数据包\n");
for(i = 0 ;i<DataNum;i++)
{
printf("%5d",a[i]);
}
printf("\n");
for(i = 0;i<DataNum;i++)
{
datapose(&queue,a[i]);/*数据包过滤子函数调用*/
}
printqueue(queue); //打印最终的队列
printf("请输出数据包\n");
}