队列(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/) 作业队列,消息队列和其他队列.几乎所有你能想到的都在这. 关于 那里有很多排队系统.他们每个人都不同,是为解决某些问题而创建的.这个页面 ...
随机推荐
- 设置mySql的编码方式为utf-8
检查命令: mysql> show variables like '%char%'; 期望结果: 使用mysql命令设置: 如果仍有编码不是utf8的,请检查配置文件,也可使用mysql命令设置 ...
- 使用easyui combobox初始化+在input中触发下拉框+获取值
效果图: 1.html <input id="alarmLeve" class="easyui-combobox" name="alarmLev ...
- 清空chrome浏览器缓存
缓存是一个很深奥的东西,虽然查了半天,还是没有搞清楚,希望以后可以遇到前端大神,可以给一个傻瓜化的通俗易懂的解释 已经上线的,后续有迭代的软件,迭代的版本不应该手动清除缓存了,因为太麻烦,对客户来说不 ...
- BZOJ 2780 Sevenk Love Oimaster (后缀自动机+树状数组+dfs序+离线)
题目大意: 给你$n$个大串和$m$个询问,每次给出一个字符串$s$询问在多少个大串中出现过 好神的一道题 对$n$个大串建出广义$SAM$,建出$parent$树 把字符串$s$放到$SAM$里跑, ...
- 论文阅读《ActiveStereoNet:End-to-End Self-Supervised Learning for Active Stereo Systems》
本文出自谷歌与普林斯顿大学研究人员之手并发表于计算机视觉顶会ECCV2018.本文首次提出了应用于主动双目立体视觉的深度学习解决方案,并引入了一种新的重构误差,采用自监督的方法来解决缺少ground ...
- WebService 布置简单的计算器
实验环境:myeclipse2015 具体的过程如下: 首先建立一个web service project 命名就为WebServiceProject,同时选择web service framewor ...
- js实现导航固定定位
js实现导航固定定位 <!DOCTY ...
- codevs 1803 志愿者招募
1803 志愿者招募 2008年NOI全国竞赛 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 申奥成功后,布布经过不懈努 ...
- ASP.NET-三种认证Authentication简介
ASP.NET 的 认证Authentication主要分为三种 1.Form Authentication 用于互联网的认证,自定性强,功能强大,可以用cookie和ssl 2.Window A ...
- Java web开发中主要用到的jar包
1.Java开发中主要用到的jar包介绍:(1)java JDK基础开发包:java包和javax包.书写方式:import java.lang.reflect.InvocationTargetExc ...