循环队列的数组实现

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实现的更多相关文章

  1. 消息队列——RabbitMQ学习笔记

    消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...

  2. 消息队列 Kafka 的基本知识及 .NET Core 客户端

    前言 最新项目中要用到消息队列来做消息的传输,之所以选着 Kafka 是因为要配合其他 java 项目中,所以就对 Kafka 了解了一下,也算是做个笔记吧. 本篇不谈论 Kafka 和其他的一些消息 ...

  3. Beanstalkd一个高性能分布式内存队列系统

    高性能离不开异步,异步离不开队列,内部是Producer-Consumer模型的原理. 设计中的核心概念: job:一个需要异步处理的任务,是beanstalkd中得基本单元,需要放在一个tube中: ...

  4. .net 分布式架构之业务消息队列

    开源QQ群: .net 开源基础服务  238543768 开源地址: http://git.oschina.net/chejiangyi/Dyd.BusinessMQ ## 业务消息队列 ##业务消 ...

  5. 【原创经验分享】WCF之消息队列

    最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...

  6. 缓存、队列(Memcached、redis、RabbitMQ)

    本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...

  7. Java消息队列--ActiveMq 实战

    1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...

  8. Java消息队列--JMS概述

    1.什么是JMS JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...

  9. 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)

    Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...

  10. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

随机推荐

  1. sqlite3 查询表

    cx = sqlite3.connect("c:/数据库地址") # 打开数据库cu = cx.cursor()# query the tablerows = cu.execute ...

  2. ATOM常用插件推荐

    转载:http://blog.csdn.net/qq_30100043/article/details/53558381 ATOM常用插件推荐 simplified-chinese-menu ATOM ...

  3. PHP 进阶之路 - 深入理解 FastCGI 协议以及在 PHP 中的实现

    在讨论 FastCGI 之前,不得不说传统的 CGI 的工作原理,同时应该大概了解 CGI 1.1 协议 传统 CGI 工作原理分析 客户端访问某个 URL 地址之后,通过 GET/POST/PUT ...

  4. 三、存储过程(Stored Procedure)与游标(Cursor)

    一.存储过程 一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数,来执行它. 在大型数据库中,存储过程和触发器具有重要的作用.无论是存储过程还是触发器,都 ...

  5. 01二维矩阵中最大全为1的正方形maxSquare——经典DP问题(二维)

    在一个二维01矩阵中找到全为1的最大正方形 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 以矩阵中每一个点作为正方形右下角点来处理,而以该点为右下角点的最大边长最多比 ...

  6. SpringBoot与docker

    1.简介 Docker是一个开源的应用容器引擎: Docker支持将软件编译成一个镜像:然后在镜像中各种软件做好配置,将镜像发布出去,其它使用者开源直接使用这个镜像: 运行中的这个镜像称为容器,容器启 ...

  7. AI工具(星形工具)(光晕工具)(移动复制)(柜子绘制)5.12

    星形工具;基本操作与矩形一样,拖动星形工具绘制,点击键盘上箭头增加星形的角数.下箭头减少星形的角数. 选择星形工具在屏幕单击,出现星形对话框,可以设置半径1半径2,角点数.图中的星形就可以用星形工具绘 ...

  8. datatime 模块

    import datetime # 这个是一个包 里面包含 对时间的处理 对日期的处理datetime.date # 日期相关datetime.time # 时间相关 # 获取当前详细时间print( ...

  9. String常用方法

    1. String StringBuffer StringBuilder的区别: 001.在执行速度方法 StringBuilder > StringBuffer > String 002 ...

  10. 四:(之一和之二) docker架构和底层技术分析(C/S架构)

    1.架构和底层技术 Docker Host提供了RESTUL api,使docker client可以通过这些命令调用dockerd. Registry是一个公用的存储镜像的容器,类似于github. ...