vlib队列 - C语言通用队列模块

本文介绍了一种C语言实现的通用队列数据结构,支持多种数据类型,并采用环形存储机制。文中提供了队列的基本操作示例,包括构造、入队、出队、随机访问等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vlib - queue队列

queue源代码仓库链接

基本概念

队列是一种先进先出(First In First Out,FIFO)的特殊数据结构,一般情况下它只有一个出口一个入口,从队尾进入从队头出,入队push,出队pop。
本文介绍的队列是C语言的通用队列,支持各种数据类型,采用连续地址的环形存储机制。

例子

int main()
{
	queue_t q = queue(int, 8); // 定义并构造一个最大容量为8的int型队列
	int i = 0;

	// 将 0,1,2,3,4 添加到队列中
	for (i = 0; i < 5; i++)
	{
		queue_push(q, &i);
	}
	i = 100; queue_insert(q, 2, &i, QUEUE_ORGIN_FRONT); // 在队头索引为2的位置插入100

	// 遍历队列的元素
	for (i = 0; i < queue_size(q); i++)
	{
		printf("q[%d] = %d\r\n", i, queue_at(q, int, i));
	}

	// 弹出队列的前两个元素
	if (queue_pop(q, &i)); printf("pop: %d\r\n", i);
	if (queue_pop(q, &i)); printf("pop: %d\r\n", i);

	// 使用完队列后进行删除
	_queue(q);

	system("pause");
	return 0;
}

结果:

q[0] = 0
q[1] = 1
q[2] = 100
q[3] = 2
q[4] = 3
q[5] = 4
pop: 0
pop: 1

特点

  • queue定义为queue_t类型
  • queue构造需确定类型和大小,支持各种类型和最大的容量
  • queue可以很方便的进行常用的入队出队操作
  • queue具备随机访问的操作,此方式可用直接获取或者修改队列中的元素
  • queue还提供插入、移除等操作方法,以及特殊的队列模式

常用方法

#define queue(type, capacity) // 构造
#define _queue(queue) // 删除
#define queue_at(queue, type, i) // 随机访问
int queue_size(queue_t queue); // 获取大小
int queue_push(queue_t queue, void* data); // 入队
int queue_pop(queue_t queue, void* data); // 出队
void queue_clear(queue_t queue); // 清空
int queue_insert(queue_t queue, int index, void* data, int orgin); // 插队
int queue_erase(queue_t queue, int begin, int end, int orgin); // 移除
int queue_alter_capacity(queue_t queue, int capacity); // 修改队列的最大容量
void queue_mode(queue_t queue, int mode, int set); // 设置或者取消某些特殊的队列模式
int queue_init(queue_t queue, void* array, int data_size, int capacity); // 根据已定义数组来初始化队列
void queue_deinit(queue_t queue); // 与初始化对应,去初始化

方法的使用具体看仓库介绍和源代码

最后

此模块为笔者本人编写,如果喜欢希望点赞给星支持,有漏洞或者修改建议欢迎留言交流。

#include #include #include //队列最大长度 #define MAX_QUEUE 1024 //偷懒,就用静态队列了 static int mQueue[MAX_QUEUE]; //队列插入 void InsertData(int **Front, int **Rear) { if (*Rear + 1 == *Front && (*Rear + 1 - MAX_QUEUE != *Front)) { //当队列数据已满,返回 puts("Queue Size Overflow!\n"); return; } else if (*Rear - mQueue > MAX_QUEUE) { //实现的是类似循环队列,但由于是静态线性队列(数组) //而不是用链表来实现的,所以到静态队列(数组)尾部,尾指针自动指向(数组)头部 *Rear = mQueue; } puts("Input Data:"); scanf("%d", *Rear); //输入数据后,尾指针后移 *Rear += 1; } //从头指针删除一个队列中的数据 void DeleteData(int **Front, int **Rear) { if (*Front == *Rear) { //头指针尾指针重合,队列空,不能删除,返回 puts("Queue Empty!\n"); return; } else if (*Front - mQueue > MAX_QUEUE) { //参考 Rear *Front = mQueue; } //从头指针删除一个数据 *Front += 1; } //显示队列数据 void ShowData(int **Front, int **Rear) { int *temp; for (temp=*Front; temp!=*Rear; temp++) { printf("%d --> ", *temp); } puts("\n"); } void usage(void) { puts("1. Insert Data"); puts("2. Delete Data"); puts("3. Show Data"); } int main(int argc, char **argv) { //头指针,尾指针 //队列的一个特性 First in first out FIFO int *pFront, *pRear; int op_code; //初始化队列,头指针和尾指针此时指向的地址相同 pFront = pRear = mQueue; while (1) { usage(); scanf("%d", &op_code); switch (op_code) { case 1: printf("%p\n", pFront); printf("%d\n", *pFront); InsertData(&pFront, &pRear); break; case 2: DeleteData(&pFront, &pRear); break; case 3: ShowData(&pFront, &pRear); break; default: break; } } return 0; }
以下是使用C语言实现的vpp处理IP欺骗(IP Spoofing)攻击的代码示例: ```c #include <vnet/ip/ip.h> #include <vnet/ip/ip4.h> /* 定义一个函数,用于判断数据包的源IP地址是否在本地子网内 */ static int is_source_ip_local(ip4_address_t src_address) { /* 这里假设本地子网的IP地址范围为192.168.0.0/16 */ const ip4_address_t local_subnet_mask = { .as_u32 = clib_host_to_net_u32(0xFFFF0000), }; const ip4_address_t local_subnet_address = { .as_u32 = clib_host_to_net_u32(0xC0A80000), // 192.168.0.0 }; /* 判断源IP地址是否在本地子网内 */ if ((src_address.as_u32 & local_subnet_mask.as_u32) == local_subnet_address.as_u32) { return 1; } else { return 0; } } /* 定义一个函数,用于处理IP欺骗攻击 */ void handle_ip_spoofing(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffer, uword n_packets) { u32 next_index = 0; u32 i; /* 遍历数据包 */ for (i = 0; i < n_packets; i++) { vlib_buffer_t *buf = vlib_get_buffer(vm, buffer[i]); ip4_header_t *ip = vlib_buffer_get_current(buf); /* 判断数据包是否为IP数据包 */ if (PREDICT_TRUE(ip->ip_version_and_header_length == 0x45)) { /* 判断数据包的源IP地址是否在本地子网内 */ if (is_source_ip_local(ip->src_address)) { /* 如果源IP地址在本地子网内,则将数据包转发出去 */ next_index = IP_LOOKUP_NEXT_ARP; } else { /* 如果源IP地址不在本地子网内,则丢弃数据包 */ next_index = IP_LOOKUP_NEXT_DROP; } } else { /* 如果不是IP数据包,则将数据包转发出去 */ next_index = IP_LOOKUP_NEXT_ARP; } /* 设置下一跳节点的索引 */ vnet_buffer(b)->ip.adj_index[VLIB_TX] = 0; vlib_validate_buffer_enqueue_x1(vm, node, next_index, buffer[i], 0); } /* 释放队列中的数据包 */ vlib_buffer_free(vm, buffer, n_packets); } ``` 需要注意的是,这里的示例代码仅供参考,具体实现可能因为应用场景的不同而有所差异。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值