队列queue的C实现
头文件——————————————————————————————
#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实现的更多相关文章
- Python进阶【第二篇】多线程、消息队列queue
1.Python多线程.多进程 目的提高并发 1.一个应用程序,可以有多进程和多线程 2.默认:单进程,单线程 3.单进程,多线程 IO操作,不占用CPU python的多线程:IO操作,多线程提供并 ...
- Java中的队列Queue,优先级队列PriorityQueue
队列Queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口. Queue使用时要尽量避免Collecti ...
- jquery 的队列queue
使用示列代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- Windows Azure Service Bus (2) 队列(Queue)入门
<Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...
- Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue
<Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...
- (C#)使用队列(Queue)解决简单的并发问题
(C#)使用队列(Queue)解决简单的并发问题 2015-07-16 13:04 13265人阅读 评论(8) 收藏 举报 分类: Asp.Net(8) 版权声明:本文为博主原创文章,未经博主允 ...
- STL中的单向队列queue
转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...
- java09 队列Queue与Deque
队列Queue与Deque. Enumeration Hashtable与Hashtable子类Properties(资源配置文件) 引用类型(强.软.弱.虚)与WeakHashMap Identit ...
- 队列Queue和栈
1.队列Queue是常用的数据结构,可以将队列看成特殊的线性表,队列限制了对线性表的访问方式,只能从线性表的一段添加(offer)元素, 从另一段取出(poll)元素,队列遵循先进先出的原则. 2.J ...
- 消息队列Queue大全
消息队列Queue大全 (http://queues.io/) 作业队列,消息队列和其他队列.几乎所有你能想到的都在这. 关于 那里有很多排队系统.他们每个人都不同,是为解决某些问题而创建的.这个页面 ...
随机推荐
- 雅虎Yahoo 前段优化 14条军规
Yahoo 14条 雅虎十四条 腾讯前端设计的Leader推荐我背熟的.请大家都能好好学习,不要像我一样一扫而过,好好的记下来!不仅仅是晓得一些CSS xhtml就好了,深刻认识到很多的东西需要学习的 ...
- 详解eNSP下的单臂路由模拟实验配置
不同VLAN之间的通信可以通过两种方式:单臂路由和三层交换机.其中,单臂路由是通过路由子接口,交换机的某个端口以trunk的方式与路由器的某个端口相连,同时路由器的链接端口配置子接口,配置子接口承载的 ...
- 如何提交docker镜像到DockerHub
Write a Dockerfile In detail: FROM(指定基础image) 构建指令,必须指定且需要在Dockerfile其他指令的前面.后续的指令都依赖于该指令指定的image.FR ...
- "Cannot find one of more components. Please reinstall the application"--安装VS2013之后不能正常打开的处理办法
今天,安装完VS2013之后,不能正常启动.总提示一个让人摸不到头脑的错误: "Cannot find one of more components. Please reinstall th ...
- 用inno Setup做应用程序安装包的示例脚本(.iss文件)(
用innoSetup做应用程序安装包的示例脚本(.iss文件),具体要看innoSetup附带的文档,好象是pascal语言写的脚本. 示例1(应用程序.exe,客户端安装): ;{089D6802- ...
- 进程间通信(IPC)介绍
进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息. IPC的方式通常有管道(包括无名管道和命名管道).消息队列.信号量.共享存储.Socket ...
- 你可能不知道的python
1.如何循环获得下标,使用 enumerate ints = ['a','b','c','d','e','f'] for idx, val in enumerate(ints): print idx, ...
- GitHub上排名前100的Android开源库介绍(来自github)
本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍,至于排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果,然后过滤了 ...
- Tips for thrift
Introduction I have designed and developed game servers successfully with thrift (http://thrift.apac ...
- python string.py 源码分析 三:maketrans
l = map(chr, xrange(256)) #将ascii转为字符串 _idmap = str('').join(l) del l # Construct a translation stri ...