链式队列基本操作
2015-08-04 09:45
239 查看
#include<stdio.h> #include<stdlib.h> typedef int dataType; typedef struct node { dataType data; struct node *next; }linklist; typedef struct { linklist *front; linklist *real; }linkqueue; void setnull(linkqueue *q)//置空 { linklist *p=malloc(sizeof (linkqueue)); q->front=q->real=p; q->front->next=NULL; } int empty(linkqueue *q)//判断是否为空 { return (q->front ==q->real); } void enqueue(linkqueue *q,dataType x)//入队 { printf("入队:%d\n",x); linklist *p=malloc(sizeof(linkqueue)); if(p==NULL) { printf("error\n"); exit(1); } p->data=x; p->next=NULL; if(empty(q)) { q->front->next=p; q->real=p; } else { q->real->next=p; q->real=p; } return ; } dataType dequeue(linkqueue *q)//出队 { if(empty(q)) { printf("queue is empty!\n"); return 0; } else { linklist *p; p=q->front; q->front=q->front->next; free(p); printf("\n出队:%d\n",q->front->data); return (q->front->data); } } void clearqueue(linkqueue *q)//清除所有元素 { printf("\n清除队列中*************\n\n"); linklist *p; p=q->front; int count=1; while(p!=NULL) { q->front=q->front->next; free(p); p=q->front; } q->real=NULL; printf("清除完成\n"); } void printqueue(linkqueue *q)//打印所有元素 { linklist *s; s=q->front->next; while(s!=NULL) { printf("%d\t",s->data); s=s->next; } printf("\n\n"); } dataType real(linkqueue *q)//打印队尾元素 { if(empty(q)) { printf("queue is empty!\n"); } else return (q->real->data); } dataType front(linkqueue *q)//打印队首元素 { if(empty(q)) { printf("queue is empty!\n"); } else return (q->front->next->data); } void main() { linkqueue p; setnull(&p);// 设为空 printf("\n是否为空?\n%d\n\n",empty(&p));//判断是否为空 enqueue(&p,2);//入队 enqueue(&p,3);//入队 enqueue(&p,4);//入队 printf("\n是否为空?\n%d\n\n",empty(&p));//判断是否为空 printf("\n打印队列:\n"); printqueue(&p); printf("the front data:\n"); printf("%d\n",front(&p));//打印front printf("the real data:\n"); printf("%d\n",real(&p));//打印real dequeue(&p);//出队 printf("the front data:\n"); printf("%d\n",front(&p));//打印front printf("\n打印队列:\n"); printqueue(&p); clearqueue(&p); printf("\n是否为空?\n%d\n",empty(&p));//判断是否为空 }
运行结果:
是否为空? 1 入队:2 入队:3 入队:4 是否为空? 0 打印队列: 2 3 4 the front data: 2 the real data: 4 出队:2 the front data: 3 打印队列: 3 4 清除队列中************* 清除完成 是否为空? 1
相关文章推荐
- mysql 多表联查
- 分享下我成为大讲台自适应学习系统内测员的经验
- 第16章 调色板管理器_16.4 一个DIB位图库的实现(2)
- iOS AudioQueue机制的延迟问题探究
- 前端常用工具
- Android拓展系列(12)--使用Gradle发布aar项目到JCenter仓库
- win7无线手机可以连接但没有数据通过
- The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.
- Eclipse中web-inf和meta-inf文件夹的信息
- webview使用
- 网易新闻应用源码完整版客户端+服务端源码
- CSS3中的clip-path使用攻略
- 动态规划--最长公共子序列
- 常用的Javascript数据验证插件
- 开发所用的一些工具,navicat破解,myeclipse破解,
- hdu 1671 phone list(字典树)
- hdu 1181 变形课 (dfs)简单搜索
- codeforces 543A A. Writing Code(完全背包优化dp )
- 大众点评的大数据实践-CSDN.NET
- (转)推荐系统—从入门到精通(论文选摘)