队列(Queue)-c实现
相对而言,队列是比较简单的。
代码还有些warning,我改不动,要找gz帮忙。
#include <stdio.h> typedef struct node
{
int data;
struct node* next;
}Node; typedef struct queue
{
Node* head;
Node* tail;
}Queue; void InitQueue(Queue*);
void EnQueue(Queue*, int);
int DeQueue(Queue* queue);
void PrintQueue(Queue* queue);
int IsNull(Queue* queue);
void DelQueue(Queue* queue); void DelQueue(Queue* queue)
{
while(queue->head != queue->tail)
{
DeQueue(queue);
} } void PrintQueue(Queue* queue)
{
if(IsNull(queue))
{
printf("empty queue.\n");
return ;
}
Node* curNode= queue->head->next;
while(curNode)
{
if(curNode->next !=NULL)
{
printf("%d==>", curNode->data);
}else
{
printf("%d ", curNode->data); }
curNode = curNode->next;
}
printf("\n"); } void InitQueue(Queue* queue)
{
queue->tail = (Node*)malloc(sizeof(Node));//warning
queue->tail->data = -;
queue->head = queue->tail;
queue->tail->next = NULL;
}
//入队列
void EnQueue(Queue* queue, int data)
{
Node * newNode = (Node*)malloc(sizeof(Node));//warning
newNode->data = data;
newNode->next = NULL;
queue->tail->next = newNode;//2b 这里漏了。。。导致链表连不上去
queue->tail = newNode;
} int DeQueue(Queue* queue)
{
int popValue = queue->head->data;
Node *popNode = queue->head;
queue->head = queue->head->next;
free(popNode);//warning
return popValue;
}
//1 means Null
int IsNull(Queue* queue)
{
return (queue->head == queue->tail);
} int main(void)
{
printf("Hello World!\n");
Queue queue;
InitQueue(&queue);
//printf("IsNull = %d\n", IsNull(&queue));
printf("enque 4 times and the elems: 1, 2, 3, 4\n");
EnQueue(&queue,);
//printf("IsNull = %d\n", IsNull(&queue));
EnQueue(&queue,);
EnQueue(&queue,);
EnQueue(&queue,);
PrintQueue(&queue);
printf("deque 1 times.\n");
DeQueue(&queue);
PrintQueue(&queue); printf("IsNull = %d\n", IsNull(&queue));
DelQueue(&queue);
printf("IsNull = %d\n", IsNull(&queue)); return ;
}
运行结果:
Hello World!
enque 4 times and the elems: 1, 2, 3, 4
1==>2==>3==>4
deque 1 times.
2==>3==>4
IsNull = 0
IsNull = 1 ////1 means Null
下个先写写排序吧,上次写了一部分,当然算法思想是参考其他人,我只是一个重造的菜狗。菜狗,菜狗,菜狗。。。,明天回家,很开心。晚上吃的太多,有点难受,@2016年7月29日00:44:26,睡不着。。。
队列(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/) 作业队列,消息队列和其他队列.几乎所有你能想到的都在这. 关于 那里有很多排队系统.他们每个人都不同,是为解决某些问题而创建的.这个页面 ...
随机推荐
- JS自定义功能函数实现动态添加网址参数修改网址参数值
无论是前端开发还是后台设计,很多时候开发人员都需要获取当前或目标网址的相关信息.这个已有现成的内置对象属性可以直接调用了(下面是获取当前页面的参考代码) 复制代码 代码如下: <script t ...
- Node Sass does not yet support your current environment: Windows 64-bit然如何解决,cnpm此问题解决方法
这里直接说了node sass不支持当前环境,所以可以直接删掉原来不支持本机的node sass,再重新安装就行了 删除: npm uninstall --save node-sass 安装: npm ...
- BZOJ 2118 墨墨的等式 (同余最短路)
题目大意:已知B的范围,求a1x1+a2x2+...+anxn==B存在非负正整数解的B的数量,N<=12,ai<=1e5,B<=1e12 同余最短路裸题 思想大概是这样的,我们选定 ...
- nfs+inotify
服务器先安装nfs服务,因为nfs服务端没有固定端口给客户端访问,所以需要借助rpc服务的111端口给客户端连接,即客户端访问rpc会调用nfs服务 yum -y install rpcbind nf ...
- JSp获取到当前用户的全部session
<%@page import="java.util.Enumeration"%> <% for (Enumeration<?> e = session ...
- tp volist需要便利两个数组时的处理办法
你需要便利两个数组,并且需要使用key 和value的试的时候,volist是否先得有些捉鸡? 我们可以便利其中一个数组,而另一个利用数组的指针来操作 next($arr) 将数组指针下移 key($ ...
- csdn第五届在线编程大赛-全然平方
题目详情 给定整数区间[A,B]问当中有多少个全然平方数. 输入格式: 多组数据,包括两个正整数A,B 1<=A<=B<=2000000000. 输出格式: 每组数据输出一行包括一个 ...
- HDU 4828 (卡特兰数+逆元)
HDU 4828 Grids 思路:能够转化为卡特兰数,先把前n个人标为0,后n个人标为1.然后去全排列,全排列的数列,假设每一个1的前面相应的0大于等于1,那么就是满足的序列.假设把0看成入栈,1看 ...
- doT.js的使用
引言 doT.js可以更好的在html端使用json数据. {{ }} for evaluation 模板标记符 {{= }} for interpolation 输出显示,默认变量名叫it {{! ...
- NOIP 2012 T2 国王游戏 (贪心+高精)
思路: 呃呃网上那么多题解写得都不错-.. 就是高精 巨坑... 这里展出的是任氏高精(纯自己yy滴) //By SiriusRen #include <cstdio> #include ...