队列的知识

1.二叉树的广度(层次)遍历算法优先用队列,深度优先(DFS算法)用栈。

2.用序列筛选法建立堆:

筛选法建堆必须从第n/2个元素开始,例如{12,13,11,18,60,15,7,19,25,100},从10/2 就是  60开始

3.常用开源软件:(其中Redis,kafka经常被用作队列)

MongoDB:分布式存储文件

Redis:Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库

Memcached: Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。

kafka:Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写。Kafka是一种高吞吐量的分布式发布订阅消息系统

 

#include <stdio.h>
#include <stdlib.h>
 
typedef int elemType;
/**************************/
/*           */
/**************************/
typedef struct nodet 
{
	elemType data;
	struct nodet * next;
} node_t;            // 节点的结构
 
typedef struct queuet
{
	node_t * head;
	node_t * tail;
} queue_t;          // 队列的结构
 
/*1. 初始化链队列*/
// 其  初始化的 操作就是初始化队列的队头和队尾的两个标志位,
// 所以就有删除或是插入的时候,会判断有没有 队列为空的时候。
void initQueue(queue_t * queue_eg)
{
	queue_eg->head = NULL; //队头标志位
	queue_eg->tail = NULL; //队尾标志位
}
 
/*2.向链队的队尾插入一个元素x*/
void enQueue(queue_t *hq, elemType x)
{
	node_t * new_p;
	new_p = (node_t *)malloc(sizeof(queue_t));
	if (new_p == NULL )
	{
		printf("分配空间出错!");
		exit(1);
	}
	new_p->data = x;
	new_p->next = NULL;
	if (hq->head == NULL)
	{
		hq->head = new_p;
		hq->tail = new_p;
	} else {
		//hq->tail->data = x;
		hq->tail->next = new_p;
		hq->tail = new_p;
	}
	return;
}
 
/*3. 从列队中队首删除一个元素*/
elemType outQueue(queue_t * hq)
{
	node_t * p;
	elemType temp;
	if (hq->head == NULL)
	{
		printf("队列为空,不能删除!");
		exit(1);
	}
	temp = hq->head->data;
	p = hq->head;
	hq->head = hq->head->next;
	if(hq->head == NULL)
	{
		hq->tail = NULL;
	}
	free(p);
	return temp;
}
 
/*4. 读取队首元素 */
elemType peekQueue(queue_t * hq)
{
	if (hq->head == NULL)
	{
		printf("队列为空。");
		exit(1);
	} 
	return hq->head->data;
}
 
/*5. 检查队列是否为空,若是空返回1,若不为空返回0 。*/
int is_emptyQueue(queue_t * hq)
{
	if (hq->head == NULL)
	{
		return 1;
	} else {
		return 0;
	}
}
 
/*6. 清除链队中的所有元素*/
 
void clearQueue(queue_t * hq)
{
	node_t * p = hq->head;
	while(p != NULL)
	{
		hq->head = hq->head->next;
		free(p);
		p = hq->head;
	}
	hq->tail = NULL;
	return;
}
 
/*main()函数*/
int main(int argc, char* argv[])
{
	queue_t q;
	int a[8] = {1, 2, 3, 4, 5, 6, 7, 8};
	int i;
	initQueue(&q);
	for(i=0; i<8; i++)
	{
		enQueue(&q, a[i]);
	}
	//printf("%d",outQueue(&q));
 
	enQueue(&q, 68);
	//printf("%d", peekQueue(&q));
	
	while (!is_emptyQueue(&q))
	{
		printf("%d.\n", outQueue(&q));
	}
 
	printf(" \n");
	clearQueue(&q);
	system("pause");
	system("pause");
	system("pause");
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值