Linux的队列 std/queue.h

本文介绍了Linux头文件<sys/queue.h>提供的五种数据结构:List、Singly-linked list、Singly-linked tail queue、Tail queue和Circular queue。详细讲解了它们的结构特点、接口及使用示例。
摘要由CSDN通过智能技术生成

环境

Ubuntu16.04 x86_64 GNU/Linux , 4.15.0-43-generic

介绍

<sys/queue.h> 实现了5种数据结构 :

	singly-linked list  
	list 
	simple queue 
	tail queue 
	circular queue

下面对他们分别进行介绍

List

List是双向链表 : head节点不包含指向最后一个节点的指针,其结构如下图 :
LIST结构
Description :

	A list is headed by a single forward pointer (or an array of forward 
	pointers for a hash table header). The elements are doubly linked 
	so that an arbitrary element can be removed without a need to 
	traverse the list. New elements can be added to the list before
	or after an existing element or at the head of the list. A list
	may only be traversed in the forward direction.

Interface :

	LIST_INIT(head) :对head进行初始化
	LIST_INSERT_HEAD(head, elm, field) : 从list头部插入elm
	LIST_REMOVE(elm, field) : 将elm从list中删除
	LIST_INSERT_AFTER(listelm, elm, field) : 将elm插入到listelm后面
	LIST_INSERT_BEFORE(listelm, elm, field) : 将elm插入到liistelm之前
	LIST_FOREACH(var, head, field) : 循环遍历所有节点, var为中间变量 
	LIST_EMPTY(head) : 判断list是否为空
	LIST_FIRST(head) : 获取第一个节点
	LIST_NEXT(elm, field) : 获取elm的下一个节点

Example:

#include <sys/queue.h>
#include <stdlib.h>
#include <stdio.h>

struct node {
	LIST_ENTRY(node) next;
	int num;
};

LIST_HEAD(head, node);

struct head* lhead = NULL;


int main(int argc, char* argv[]){
	lhead = (struct head*)malloc(sizeof(struct head));
	LIST_INIT(lhead);
	
	struct node *var;
	struct node *num_1 = (struct node *)malloc(sizeof(struct node));
	num_1->num = 1;
	LIST_INSERT_HEAD(lhead, num_1, next);
	LIST_FOREACH(var, lhead, next){
		printf("%d->", var->num);
	}
	printf("null\n");

	struct node *num_2 = (struct node *)malloc(sizeof(struct node));
	num_2->num = 2;
	struct node *num_3 = (struct node *)malloc(sizeof(struct node));
	num_3->num = 3;
	LIST_INSERT_HEAD(lhead, num_2, next);
	LIST_INSERT_HEAD(lhead, num_3, next);
	LIST_FOREACH(var, lhead, next){
		printf("%d->", var->num);
	}
	printf("null\n");
	
	struct node *num_4 = (struct node *)malloc(sizeof(struct node));
	num_4->num = 4;
	struct node *num_5 = (struct node *)malloc(sizeof(struct node));
	num_5->num = 5;
	LIST_INSERT_BEFORE(num_3, num_4, next);
	LIST_INSERT_AFTER(num_3, num_5, next);
	LIST_FOREACH(var, lhead, next){
		printf("%d->", var->num);
	}
	printf("null\n");
	
	LIST_REMOVE(num_3, next);
	LIST_FOREACH(var, lhead, next){
		printf("%d->", var->num);
	}
	printf("null\n");
	var = LIST_NEXT(num_4, next);
	printf("4's next is %d\n", var->num);
	return 0;
}

Singly-linked list

singly-linked list 是单向链表,head不包含指向最后

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
std::vector和std::queue是C++标准库提供的两种常用容器。std::vector是一种动态数组,可以在末尾添加、删除和访问元素。std::queue是一种队列,遵循先进先出(FIFO)的原则,可以在队尾添加元素,在队头删除元素。 引用提供了一个std::vector的示例代码。在该示例中,可以看到如何使用std::vector的push_back()函数在末尾添加元素,使用front()函数获取队列的第一个元素,使用erase()函数删除队列的第一个元素,以及使用迭代器进行正向和反向遍历。 引用提供了使用std::vector时需要使用std命名限定的说明。使用using std::vector来引入std命名域,以便直接使用vector而不必再加上std::前缀。 引用提供了一个示例代码,演示了如何使用insert()函数在std::vector中的第一个位置插入一个数据。 std::queue是一种基于std::deque容器的适配器,使其行为符合队列的特性。std::queue提供了push()函数在队尾添加元素,pop()函数删除队头元素,front()函数获取队头元素,以及empty()函数判断队列是否为空。 总结起来,std::vector适用于需要随机访问和动态调整大小的场景,而std::queue适用于需要遵循FIFO原则的场景。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [std::vector](https://blog.csdn.net/mayue_web/article/details/86257000)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [std::vector的其它操作](https://blog.csdn.net/qq_15204179/article/details/129930182)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值