头文件——————————————————————————————
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include <stdlib.h>
#define Element int
struct node
{
Element data;
struct node *next;
};
typedef struct node *PtrToNode;
typedef struct _Queue
{
PtrToNode head;
PtrToNode rear;
} *Queue;
//头进尾出
int IsEmpty(Queue q);
Queue CreateQueue();
void DestroyQueue(Queue q);
void MakeEmpty(Queue q);
void Enqueue(Element x, Queue q);
Element Front(Queue q);
Element Rear(Queue q);
void Dequeue(Queue q);
#endif
源文件————————————————————————————
#include <iostream>
#include "./Queue.h" int IsEmpty(Queue q)
{
return q->head == q->rear && q->head == NULL;
}
Queue CreateQueue()
{
Queue q = (Queue)malloc(sizeof(struct _Queue));
if(NULL == q) return NULL;
q->head = q->rear = NULL;
return q;
}
void DestroyQueue(Queue q)
{
MakeEmpty(q);
free(q);
}
void MakeEmpty(Queue q)
{
while(!IsEmpty(q))
Dequeue(q);
}
void Enqueue(Element x, Queue q)//头进尾出
{
if(NULL == q) return ;
PtrToNode p = (PtrToNode)malloc(sizeof(struct node));
if(NULL == p) return ;
p->data = x;
if(IsEmpty(q))
q->rear = p;
p->next = q->head;
q->head = p;
}
Element Front(Queue q)
{
return q->head->data;
}
Element Rear(Queue q)
{
return q->rear->data;
}
void Dequeue(Queue q)//头进尾出
{
if(NULL == q || IsEmpty(q)) return ;
PtrToNode prev = q->head;
while(NULL != prev && prev != q->rear && prev->next != q->rear)
prev = prev->next;
if(NULL == prev) return ;
if(prev == q->rear)
{
q->head = q->rear = NULL;
free(prev);
}
PtrToNode tmp = prev->next;
prev->next = NULL;
q->rear = prev;
free(tmp);
/*if(q->head == q->rear)
{
PtrToNode tmp = q->head;
free(tmp);
q->head = q->rear = NULL;
}
PtrToNode prev = q->head;
while(NULL != prev && prev->next->data != q->rear->data)
prev = prev->next;
PtrToNode tmp = prev->next;
prev->next = NULL;
q->rear = prev;
free(tmp);
*/
}

  

队列queue的C实现的更多相关文章

  1. Python进阶【第二篇】多线程、消息队列queue

    1.Python多线程.多进程 目的提高并发 1.一个应用程序,可以有多进程和多线程 2.默认:单进程,单线程 3.单进程,多线程 IO操作,不占用CPU python的多线程:IO操作,多线程提供并 ...

  2. Java中的队列Queue,优先级队列PriorityQueue

    队列Queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口. Queue使用时要尽量避免Collecti ...

  3. jquery 的队列queue

    使用示列代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  4. Windows Azure Service Bus (2) 队列(Queue)入门

    <Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...

  5. Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue

    <Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...

  6. (C#)使用队列(Queue)解决简单的并发问题

    (C#)使用队列(Queue)解决简单的并发问题 2015-07-16 13:04 13265人阅读 评论(8) 收藏 举报  分类: Asp.Net(8)  版权声明:本文为博主原创文章,未经博主允 ...

  7. STL中的单向队列queue

    转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...

  8. java09 队列Queue与Deque

    队列Queue与Deque. Enumeration Hashtable与Hashtable子类Properties(资源配置文件) 引用类型(强.软.弱.虚)与WeakHashMap Identit ...

  9. 队列Queue和栈

    1.队列Queue是常用的数据结构,可以将队列看成特殊的线性表,队列限制了对线性表的访问方式,只能从线性表的一段添加(offer)元素, 从另一段取出(poll)元素,队列遵循先进先出的原则. 2.J ...

  10. 消息队列Queue大全

    消息队列Queue大全 (http://queues.io/) 作业队列,消息队列和其他队列.几乎所有你能想到的都在这. 关于 那里有很多排队系统.他们每个人都不同,是为解决某些问题而创建的.这个页面 ...

随机推荐

  1. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  2. .NET开发中经常用到的扩展方法

    整理一下自己经常用到的几个扩展方法,在实际项目中确实好用,节省了不少的工作量. 1  匿名对象转化 在WinForm中,如果涉及较长时间的操作,我们一般会用一个BackgroundWorker来做封装 ...

  3. 代码生成器Sql Server 和 Mysql 数据库脚本

    经常用到代码生成器,对于取数据脚本做个记录: #region SQL-SqlServer private string SqlTableList = @"SELECT so.name, Co ...

  4. paypal之nodejs 框架 Kraken-js 源码分析

    本文是基于 kraken-js 0.6.1 版本的 关于如何使用kraken-js 可以去看看官网的使用文档 点击这里 .kraken-js 是基于express之上的,目的在于让工程师更多的去关注代 ...

  5. hibernate 映射 多对一

    一对多和上文讲的多对一两种映射关系,其实就是站在相反的角度考虑同样的事情. 一对多和多对一映射原理是一样的,都在多的一端加入一个外键指向一的一端.也就是说,在关系数据库的表中,他们的表及表字段都是一样 ...

  6. 【转】关于TCP和UDP协议消息保护边界的介绍

    在 socket网络程序中,TCP和UDP分别是面向连接和非面向连接的.因此TCP的socket编程,收发两端(客户端和服务器端)都要有一一成对的 socket,因此,发送端为了将多个发往接收端的包, ...

  7. Django 源码小剖: URL 调度器(URL dispatcher)

    在刚开始接触 django 的时候, 我们尝试着从各种入门文档中创建一个自己的 django 项目, 需要在 mysite.urls.py 中配置 URL. 这是 django url 匹配处理机制的 ...

  8. Notes on how to use Webots, especially how to make a robot fly in the air

    How to create a new project Wizard - New project directory   Scene Tree Scene tree is a representati ...

  9. The model backing the <Database> context has changed since the database was created.

    Just found out the answer and thought of updating here. Just need to do the following. public class ...

  10. javascript不用new关键字创建对象示例

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...