链式队列基本操作
来源:互联网 发布:三国无双7萝莉捏脸数据 编辑:程序博客网 时间:2024/04/30 02:01
#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");}elsereturn (q->real->data);}dataType front(linkqueue *q)//打印队首元素{if(empty(q)){printf("queue is empty!\n");}elsereturn (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));//打印frontprintf("the real data:\n");printf("%d\n",real(&p));//打印realdequeue(&p);//出队printf("the front data:\n");printf("%d\n",front(&p));//打印frontprintf("\n打印队列:\n");printqueue(&p);clearqueue(&p);printf("\n是否为空?\n%d\n",empty(&p));//判断是否为空}
运行结果:
是否为空?1入队:2入队:3入队:4是否为空?0打印队列:234the front data:2the real data:4出队:2the front data:3打印队列:34清除队列中*************清除完成是否为空?1
0 0
- 链式队列基本操作
- 链式队列基本操作
- 链式队列基本操作总结
- 链式队列的基本操作
- 队列之链式队列基本操作
- 链式队列基本操作之code_legend
- 队列基本操作(链式结构)
- 【数据结构】链式队列的基本操作
- 链式队列的基本操作实现c++
- 链式队列的基本操作(配图)
- 链式队列的基本操作与实现
- 【队列】链式结构实现队列的基本操作
- 循环队列和链式队列的基本操作1
- 基本数据结构:链式队列
- 基本数据结构:链式队列
- 队列的基本操作(采用链式存储结构)
- 链式队列一些操作
- 链式队列操作
- Executor 的工具类 Executors
- 南邮 OJ 1048 图的宽度优先遍历序列
- Oracle 常用命令总结
- Python 学习 字符串
- codeforces 543A A. Writing Code(完全背包优化dp )
- 链式队列基本操作
- 开发所用的一些工具,navicat破解,myeclipse破解,
- Eclipse中web-inf和meta-inf文件夹的信息
- VI/VIM以指定编码方案打开文件
- webview使用
- HDU - 5136(计数dp)
- The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.
- position的使用方法
- win7无线手机可以连接但没有数据通过