头文件

lsg_queue.h
#pragma once

#include<stdbool.h>
/* 链式栈接口的定义头文件 */
#define true 1
#define false 0
//读写器配置
typedef struct {
unsigned char data[]; //数据
unsigned int lenght; // 有效数据
}Data; /* 队列的数据类型 */
typedef Data * datatype; /* 静态链的数据结构 */
typedef struct q_node {
datatype data;
struct q_node *next;
}q_node, *link_node; typedef struct l_queue {
/* 队头指针 */
q_node *front;
/* 队尾指针 */
q_node *rear;
}*link_queue; /* 静态顺序链的接口定义 */ /* 静态链的初始化 */
link_queue queue_init(); /* 判断队列是否为空,若为空
* 返回true
* 否则返回false
*/
int queue_empty(link_queue q); /* 插入元素e为队q的队尾新元素
* 插入成功返回true
* 队满返回false
*/
int queue_en(link_queue q, datatype e); /* 队头元素出队
* 用e返回出队元素,并返回true
* 若队空返回false
*/
int queue_de(link_queue q, datatype *e); /* 清空队 */
void queue_clear(link_queue q); /* 销毁队 */
void queue_destroy(link_queue q);
//判断epcid是否已存在队列中,true在,false不在
bool queue_Contains(link_queue q, unsigned char * data, int lenght);
/* 获得队头元素
* 队列非空,用e返回队头元素,并返回true
* 否则返回false
*/
int get_front(link_queue q, datatype *e); bool strncmp_Lsg(unsigned char * data, unsigned char * data2, int lenght);
/* 获得队长 */
int queue_len(link_queue q); /* 遍历队 */
void queue_traverse(link_queue q, void(*visit)(link_queue q)); void visit(link_queue q);

实现

lsg_queue.c
/* 接口的实现文件 */
#include<stdio.h>
#include<stdlib.h>
#include"lsg_queue.h"
#include<string.h>
//一个特殊的队列,出列,并未删除,使用一个线程来按照存在在队列固定时间后删除
link_queue queue_init()
{
/* 新建头结点 */
link_node new_node = (link_node)malloc(sizeof(q_node));
new_node->next = NULL;
/* 指针结点 */
link_queue q = (link_queue)malloc(sizeof(*q));
q->front = q->rear = new_node;
return q;
} int queue_empty(link_queue q)
{
return q->front == q->rear;
} int queue_en(link_queue q, datatype e)
{
/* 新建数据结点 */
link_node new_node = (link_node)malloc(sizeof(q_node));
/* 内存分配失败 */
if (!new_node)
return false;
new_node->data = e;
new_node->next = NULL;
q->rear->next = new_node;
q->rear = new_node;
return true;
}
/* 队头元素出队
* 用e返回出队元素,并返回true
* 若队空返回false
*/
int queue_de(link_queue q, datatype * e)
{
/* 队列为空 */
if (q->front == q->rear)
return false;
*e = q->front->next->data;
link_node temp = q->front->next;
q->front->next = temp->next;
/* 防止丢失尾指针 */
if (temp == q->rear)
q->rear = q->front;
free(temp);
temp = NULL;
return true;
}
//判断epcid是否已存在队列中,true在,false不在
bool queue_Contains(link_queue q, unsigned char * data, int lenght)
{
/* 头结点 */
link_node head = q->front;
/* 第一个结点 */
link_node temp = head->next;
while (temp)
{
//去掉后面的rssi值跟校验位 temp->data->lenght-2 == lenght
if (strncmp_Lsg(temp->data->data, data, lenght) && temp->data->lenght - == lenght)
{
return true;
}
link_node p = temp;
temp = p->next;
}
return false;
} bool strncmp_Lsg(unsigned char * data, unsigned char * data2, int lenght)
{
int i;
for (i = ; i < lenght; i++)
{
if (*data != *data2)
{
return false;
}
data++;
data2++;
} return true;
} void queue_clear(link_queue q)
{
/* 头结点 */
link_node head = q->front; q->front = q->rear = head;
/* 第一个结点 */
link_node temp = head->next;
while (temp)
{
link_node p = temp;
temp = p->next;
free(p->data);
free(p);
p = NULL;
}
head->next = NULL;
} void queue_destroy(link_queue q)
{
queue_clear(q);
//清头节点
free(q->front);
free(q);
q = NULL;
} int get_front(link_queue q, datatype * e)
{
/* 队为空 */
if (q->front == q->rear)
return false; *e = q->front->next->data;
return true;
} int queue_len(link_queue q)
{
/* 头结点 */
link_node p = q->front->next;
/* 计数器 */
int count = ;
while (p)
{
count += ;
p = p->next;
}
return count;
} void queue_traverse(link_queue q, void(*visit)(link_queue q))
{
visit(q);
} void visit(link_queue q)
{
int i;
/* 头结点 */
link_node p = q->front->next;
if (!p)
{
printf("队列为空");
}
while (p)
{
printf("开始输出:");
for (i = ; i < p->data->lenght; i++)
{
printf("%hhu ", p->data->data[i]);
}
printf("\n");
p = p->next; }
printf("\n");
}

测试

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <winnt.h>
#include"lsg_queue.h"
#define BAUD_RATE 9600
#define p printf int main(int argc, char *argv[])
{
char buf[]; int lenght = , i = ;
unsigned char datatemp[] = { 0xA0, 0x06, 0x01, 0x74 };
datatype e = NULL;
link_queue q = queue_init(); Data * data = (struct Data*)malloc(sizeof(Data));/*2.结构体指针需要初始化*/
Data * data2 = (struct Data*)malloc(sizeof(Data));/*2.结构体指针需要初始化*/
data->lenght = ;
data2->lenght = ;
for (i = ; i < data->lenght; i++)
{
data->data[i] = datatemp[i];
data2->data[i] = datatemp[i];
}
printf("加入队列"); queue_en(q, data); queue_en(q, data);
printf("length=%d\n", queue_len(q)); if (strncmp_Lsg(&datatemp[], &datatemp[], ))
{
p("\n测试成功已存在");
} datatemp[] = ;
if (queue_Contains(q, datatemp, data->lenght))
{
p("已存在");
}
queue_de(q, &e);
for (i = ; i < e->lenght; i++)
{
p("%hhu ", e->data[i]);
}
queue_traverse(q, visit);
queue_destroy(q);
printf("退出");
getchar();
system("pause"); return ;
}
声明:原创博客请在转载时保留原文链接或者在文章开头加上本人博客地址,如发现错误,欢迎批评指正。凡是转载于本人的文章,不能设置打赏功能,如有特殊需求请与本人联系!

最新文章

  1. 【转载】写一个js库需要怎样的知识储备和技术程度?
  2. cf723d Lakes in Berland
  3. MySQL配置文件my.cnf 例子最详细翻译
  4. 美萍超市销售管理系统标准版access数据库密码mp611
  5. JavaScript之作用域与闭包详解
  6. Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
  7. 重复造轮子感悟 – XLinq性能提升心得
  8. Abstract-抽象类
  9. OPENSSL库的使用-DES篇
  10. SQL重复记录查询的几种方法
  11. 2019微软Power BI 每月功能更新系列——Power BI 4月版本功能完整解读
  12. python 缺少module
  13. tomcat的 tomcat-user.xml
  14. 第三方jar上传到Maven私服(Nexus)
  15. 安尼泰科T1行车记录仪说明书
  16. C# MVC 用户登录状态判断
  17. 50个常用的sql语句
  18. 【Tomcat】Servlet 工作原理解析
  19. 数学软件Matlab的使用感受
  20. Python Algorithms – chapter3 计数初步

热门文章

  1. 教你一步一步用 Node.js 制作慕课网视频爬虫
  2. nginx支持返回相对路径
  3. bzoj1088 [SCOI2005]扫雷
  4. js 禁止剪切、复制、粘贴的文本框代码
  5. 《Python》 property、classmethod、staticmethod、isinstance、issubclass
  6. JavaScript世界的一等公民 - 函数
  7. 给hackrf加上1602LCD以及UART(附带固件编译方法)
  8. Android 搭建ssh服务
  9. A+B for Input-Output Practice (VII)
  10. EasyTouch基本用法