队列 c实现
循环队列的数组实现
queue.h
#ifndef _QUEUE_H_
#define _QUEUE_H_ #define SIZE 10 typedef int data_t; typedef struct head{
data_t data[SIZE];
int front;
int rear;
}queue_t; queue_t *queue_creat(); int queue_is_empty(queue_t *head);
int queue_is_full(queue_t *head);
void queue_clear(queue_t *head); int queue_en(queue_t *head,data_t data);
data_t queue_de(queue_t *head); void queue_show(queue_t *head);
void queue_destory(queue_t **head); #endif //_QUEUE_H_
queue.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h> #include "queue.h" queue_t *queue_create()
{
queue_t *head = (queue_t *)malloc(sizeof(queue_t));
bzero(head, sizeof(queue_t)); head->front = ;
head->rear = ; return head;
} int queue_is_empty(queue_t *head)
{
return head->front == head->rear;
} int queue_is_full(queue_t *head)
{
return head->rear - head->front == SIZE;
} void queue_clear(queue_t *head)
{
head->rear = head->front;
} int queue_en(queue_t *head, data_t data)
{
if (queue_is_full(head)) {
printf("queue is full!\n");
return -;
} head->data[head->rear % SIZE] = data;
head->rear++; return ;
} data_t queue_de(queue_t *head)
{
if (queue_is_empty(head)) {
printf("queue is empty!\n");
return -;
} data_t data = head->data[head->front % SIZE];
head->front++; return data;
} void queue_show(queue_t *head)
{
int i;
for (i = head->front; i < head->rear; i++) {
printf("%d, ", head->data[i % SIZE]);
}
printf("\n");
} void queue_destory(queue_t **head)
{
free(*head);
*head = NULL;
}
main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h> #include "queue.h" int main()
{
queue_t *head = queue_create(); int n = ;
while (n--) {
queue_en(head, n+);
}
queue_show(head); int i;
for (i = ; i < ; i++) {
printf("%d, ", queue_de(head));
}
printf("\n");
queue_show(head); for (i = ; i < ; i++) {
queue_en(head, i);
}
queue_show(head); queue_destory(&head); return ;
}
队列的链表实现
queue.h
#ifndef _QUEUE_
#define _QUEUE_ typedef int data_t; typedef struct node{
data_t data;
struct node *next;
}NODE; typedef struct{
struct node *front;
struct node *rear;
}queue_t; queue_t *queue_create(); int queue_is_empty(queue_t *head);
int queue_is_full(queue_t *head);
void queue_clear(queue_t *head); int queue_en(queue_t *head, data_t data);
data_t queue_de(queue_t *head); void queue_show(queue_t *head);
void queue_destory(queue_t **head); #endif
queue.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h> #include "queue.h" queue_t *queue_create()
{
queue_t *head = (queue_t *)malloc(sizeof(queue_t));
bzero(head, sizeof(queue_t)); head->front = (NODE *)malloc(sizeof(NODE));
bzero(head->front, sizeof(NODE)); head->front->data = -;
head->front->next = NULL; head->rear = head->front; return head;
} int queue_is_empty(queue_t *head)
{
//return head->front->next == NULL;
return head->front == head->rear;
} int queue_is_full(queue_t *head)
{
return ;
} void queue_clear(queue_t *head)
{
NODE *p = head->front->next;
NODE *q = p->next; head->front->next = NULL; while (p != NULL) {
q = p->next; free(p); p = q;
} head->rear = head->front;
} int queue_en(queue_t *head, data_t data)
{
NODE *p = (NODE *)malloc(sizeof(NODE));
bzero(p, sizeof(NODE));
p->data = data;
p->next = NULL; head->rear->next = p; head->rear = p; return ;
} data_t queue_de(queue_t *head)
{
if (queue_is_empty(head)) {
printf("queue is empty!\n");
return -;
} NODE *p = head->front;
NODE *q = p->next;
data_t data = q->data; p->next = q->next;
free(q);
q = NULL; if (p->next == NULL) {
head->rear = head->front;
} return data;
} void queue_show(queue_t *head)
{
NODE *p = head->front->next; while (NULL != p) {
printf("%d, ", p->data);
p = p->next;
}
printf("\n");
} void queue_destory(queue_t **head)
{
queue_clear(*head); free(*head);
*head = NULL;
}
main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h> #include "queue.h" int main()
{
queue_t *head = queue_create(); int n = ;
while (n--) {
queue_en(head, n+);
}
queue_show(head); int i;
for (i = ; i < ; i++) {
printf("%d, ", queue_de(head));
}
printf("\n");
queue_show(head); for (i = ; i < ; i++) {
queue_en(head, i);
}
queue_show(head); queue_destory(&head); return ;
}
队列 c实现的更多相关文章
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- 消息队列 Kafka 的基本知识及 .NET Core 客户端
前言 最新项目中要用到消息队列来做消息的传输,之所以选着 Kafka 是因为要配合其他 java 项目中,所以就对 Kafka 了解了一下,也算是做个笔记吧. 本篇不谈论 Kafka 和其他的一些消息 ...
- Beanstalkd一个高性能分布式内存队列系统
高性能离不开异步,异步离不开队列,内部是Producer-Consumer模型的原理. 设计中的核心概念: job:一个需要异步处理的任务,是beanstalkd中得基本单元,需要放在一个tube中: ...
- .net 分布式架构之业务消息队列
开源QQ群: .net 开源基础服务 238543768 开源地址: http://git.oschina.net/chejiangyi/Dyd.BusinessMQ ## 业务消息队列 ##业务消 ...
- 【原创经验分享】WCF之消息队列
最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...
- 缓存、队列(Memcached、redis、RabbitMQ)
本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...
- Java消息队列--ActiveMq 实战
1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...
- Java消息队列--JMS概述
1.什么是JMS JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...
- 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)
Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
随机推荐
- HDU-6395-矩阵快速幂
Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- python中几种循环体
条件判断与循环 1.条件判断 if <条件判断1>: <执行1> elif <条件判断2>: <执行2> elif <条件判断3>: < ...
- openstack安装指南和在centos7上的安装指南
openstack安装指南官网:http://docs.openstack.org/project-install-guide/newton/ openstack在centos7上的安装指南官网:ht ...
- Java线程池理解及用法
前言 多线程的异步执行方式,虽然能够最大限度发挥多核计算机的计算能力,但是如果不加控制,反而会对系统造成负担.线程本身也要占用内存空间,大量的线程会占用内存资源并且可能会导致Out of Memory ...
- 在vue项目中使用sass
如果想开发移动端项目,那么适配的时候sass必不可缺,但是 npm 安装sass时候总是报错失败! 研究半天发现可以解决的方法,亲测有效 1.先换成淘宝镜像再安装 npm install -g cn ...
- 从概率图模型pgm到rbm
有向图模型:directed acyclic graph DAG 贝叶斯网络 对称的,无向图, UGM: undirected graphic model UGM, 更有名的名称是MRF,mar ...
- pycharm(Tip of Day)
You can easily override the methos of the base class by press Ctrl + 0 ( code | override methods) Yo ...
- 逆袭之旅DAY13.东软实训.Oracle.简单的查询语句.限制.排序
2018-07-09 21:34:00 一.简单查询: .查询数据表的所有列: SELECT * FROM 表名; SELECT 列名,列名.... FROM 表名; .起别名: SELECT 列名 ...
- 初学Linux系统最应该做对的4件事情[长文]
“闲来无事,逛逛贴吧”已经是本人无事消磨时间的最佳选择了.五花八门的问题,各式各样的回答,总能给自己带来无限的欢乐.当然也有些问题值得自己去思考或者回答.之前就有人在贴吧里问到“Linux好难啊!该怎 ...
- day23 模块02
核能来袭--模块 2 1.nametuple() 2.os模块 3.sys模块(重点) 4.序列化 (四个函数) 5.pickle(重点) 6.json(重点中的重点) 1.nametuple() 命 ...