队列 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)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
随机推荐
- 一、I/O操作(File文件对象)
一.File类 Java里,文件和文件夹都是用File代表 1.使用绝对路径或者相对路径创建File对象 使用绝对路径或者相对路径创建File对象 package File; import java. ...
- [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- Python3+pdfminer+jieba+wordcloud+matplotlib生成词云(以深圳十三五规划纲要为例)
一.各库功能说明 pdfminer----用于读取pdf文件的内容,python3安装pdfminer3k jieba----用于中文分词 wordcloud----用于生成词云 matplotlib ...
- python logs
# -*- coding: utf-8 -*-import loggingimport sysimport osimport xlrdfrom UtilAzxu import Properties# ...
- 转 cousera computational neuroscience week5 学习笔记(part 1)
(2013-08-14 14:58:41) 转载▼ 标签: 学习笔记 it 很久没有写博文了,之所以重新写还是因为看了coursera的computational neuroscience之后,发现这 ...
- Cracking The Coding Interview5.2
//Given a (decimal - e.g. 3.72) number that is passed in as a string, print the binary representatio ...
- ubuntu 删除开机系统引导,设置快速开机和安静开机
1.隐藏开机选择界面 1.sudo gedit /etc/default/grub GRUB_HIDDEN_TIMEOUT=0 GRUB_HIDDEN_TIMEOUT_QUIET=true //隐藏开 ...
- Linux系统分区方案(CentOs 6)
装Linux如何分区: 方案1:(监控服务器,负载均衡器) 1./boot 引导分区,存放引导文件和Linux内核. 启动文件:用于判断你需要启动哪个操作系统或启动哪个内核. ...
- SQL-28 查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
题目描述 film表 字段 说明 film_id 电影id title 电影名称 description 电影描述信息 CREATE TABLE IF NOT EXISTS film ( film_i ...
- SpringMVC @RequestParam和@RequestBody的区别
问题:@Requestbody 用的时候遇到400和415错误,因为请求格式不对. @RequestBody @RequestBody能把简单json结构参数转换成实体类,如下代码: @Request ...