zoukankan      html  css  js  c++  java
  • c 结构体的队列

    头文件

    lsg_queue.h
    #pragma once
    
    #include<stdbool.h>
    /* 链式栈接口的定义头文件 */
    #define true 1
    #define false 0
    //读写器配置
    typedef struct {
        unsigned char data[600];   //数据
        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 - 2 == 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 = 0; 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 = 0;
        while (p)
        {
            count += 1;
            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 = 0; i < p->data->lenght; i++)
            {
                printf("%hhu ", p->data->data[i]);
            }
            printf("
    ");
            p = p->next;
    
        }
        printf("
    ");
    }

    测试

    #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[50];
    
        int lenght = 0, i = 0;
        unsigned char datatemp[6] = { 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 = 6;
        data2->lenght = 6;
        for (i = 0; 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
    ", queue_len(q));
    
        
        if (strncmp_Lsg(&datatemp[1], &datatemp[1], 6))
        {
            p("
    测试成功已存在");
        }
    
        datatemp[0] = 0;
        if (queue_Contains(q, datatemp, data->lenght)) 
        {
            p("已存在");
        }
        queue_de(q, &e);
        for (i = 0; i < e->lenght; i++)
        {
            p("%hhu ", e->data[i]);
        }
        queue_traverse(q, visit);
        queue_destroy(q);
        printf("退出");
        getchar();
        system("pause");
    
        return 0;
    }
    声明:原创博客请在转载时保留原文链接或者在文章开头加上本人博客地址,如发现错误,欢迎批评指正。凡是转载于本人的文章,不能设置打赏功能,如有特殊需求请与本人联系!
  • 相关阅读:
    Windows下sqlmap的使用_01
    连接查询中on and和on where的区别
    java中的BigDecimal和String的相互转换
    Linux使用rarcrack暴力破解RAR,ZIP,7Z压缩包
    oralce 日期 date 相关操作
    scp
    linux网络配置
    Linux防火墙--iptables--白名单配置
    ORACLE 查询所有表、外键、主键等信息
    openssl版本升级
  • 原文地址:https://www.cnblogs.com/lsgsanxiao/p/9174062.html
Copyright © 2011-2022 走看看