队列 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)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
随机推荐
- CF-413E-线段树
http://codeforces.com/problemset/problem/413/E 给出一个2*N的格子图,每个格子要么是障碍要么是空地,M次询问(A,B)之间的最短距离. 采用分治的思想, ...
- PAT 1009 Product of Polynomials
1009 Product of Polynomials (25 分) This time, you are supposed to find A×B where A and B are two p ...
- Oracle 数据库中查看表空间的2种方法
在Oracle数据库中查看表空间使用状况是我们在实际应用中经常涉及到的,以下的内容就就是对Oracle 数据库中查看表空间使用状况时所要用到的SQL的描述,希望你能从中获得自己想要的东西. Oracl ...
- Hadoop 2.7.3 完全分布式维护-部署篇
测试环境如下 IP host JDK linux hadop role 172.16.101.55 sht-sgmhadoopnn-01 1.8.0_111 CentOS release ...
- 月日加四位尾数编号生成 VB方式
<%Private Sub Form_Click()MsgBox "生成两位后缀"ClsFor i = 1 To 99barcod= Format(Right(Year(Da ...
- codepen素材 coffeeScript -> JavaScript
将coffeeScript代码复制到下面的网址进行转换: http://js2.coffee/
- PHP中的Trait方法
<?php /* * 自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. * Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制. * Trait ...
- laravel控制器方法中,用函数作为变量进行传递时的处理方法
本人在做上传图片时,里面执行的方法时一致的,只是个别地方不同,这种情况下,就需要把公用的部分提取出来,把不同的地方放到回调函数种去. StudentController中的方法: public fun ...
- 使用perfect进行服务端开发
最近闲来无事,研究了下基于perfect的swift后端开发.根据大神的博客进行了简单的配置,加深下印象也算是和各位分享一下. 参考博客:http://www.cnblogs.com/ludashi ...
- jdk8--stream并行流
stream的并行流要理解一个框架如下: 单线程,多线程和并行流对比 package com.atguigu.java8; import java.util.concurrent.ForkJoinPo ...