循环队列的数组实现

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. Python面向对象编程-类的封装,继承、多态

    面向对象是一种程序设计思想,对象作为程序基本单元,包含了数据和操作数据的函数. 面向对象的三大特点--数据封装.多态和继承. #类的创建,class关键字,类名大写,object表示从哪个类继承而来, ...

  2. 使用C#开发数据库应用程序

    第一章 用Hello ACCP.NET快速热身(一) 1-1.进入C#世界 a.第一个C#程序 (1)新建项目[项目:project] (2)生成解决方案[生成:build,解决方案:solution ...

  3. C/C++ 运算符优先级(转载)

    最讨厌这个了.在这里记录下. 优先级 操作符 描述 例子 结合性 1 ()[]->.::++-- 调节优先级的括号操作符数组下标访问操作符通过指向对象的指针访问成员的操作符通过对象本身访问成员的 ...

  4. spark使用正则表达式读入多个文件

    String dir = "s3a://example/";String currentDir = dir + "{1[5-9],2[01]}/*.txt";J ...

  5. jquery ready&&load用法

    ready和load那一个先执行 DOM文档加载的步骤 (1) 解析HTML结构 (2) 加载外部脚本和样式表文件 (3) 解析并执行脚本代码 (4) 构造HTML DOM模型 //ready (5) ...

  6. getopts的使用方法

    getopts的使用 语法格式:getopts [option[:]] [DESCPRITION] VARIABLE option:表示为某个脚本可以使用的选项 ":":如果某个选 ...

  7. Hibernate基础知识

    Hibernate Hibernate的作用: 1.         Hibernate解决ORM(对象关系映射)的问题,大大减少了持久层的代码量 2.         hql方言,解决了可移植性问题 ...

  8. MyBatis逆向工程:根据table生成Model、Mapper、Mapper.xml

    逆向工程工具 下载地址:https://download.csdn.net/download/zhutouaizhuwxd/10779140 1.工程导入Eclipse  2.运行MainUI.jav ...

  9. VBA续嘘嘘——宏技巧集绵

    什么是VBA?它有什么作用? A.实现Excel中没有实现的功能. B.提高运行速度. C.编写自定义函数. D.实现自动化功能. E.通过插入窗体做小型管理软件. VBA在哪里存放的?怎么运行? A ...

  10. 不管你是否已经准备面试, 这45道Python面试题都对你非常有帮助!(mark!)

    1)什么是Python?使用Python有什么好处? Python是一种编程语言,包含对象,模块,线程,异常和自动内存管理.蟒蛇的好处在于它简单易用,可移植,可扩展,内置数据结构,并且它是一个开源的. ...