博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用数组实现队列----《数据结构与算法分析---C语言描述》
阅读量:5935 次
发布时间:2019-06-19

本文共 3396 字,大约阅读时间需要 11 分钟。

一、h文件:my_que.h

 

#ifndef  _MY_QUE_H_#define  _MY_QUE_H_struct QueRecord;typedef struct QueRecord* queue;typedef  int element_type;int IsEmpty(queue q);int IsFull(queue q);queue creat_que(int max_element);void make_empty(queue q);void enqueue(element_type x,queue q);element_type front_que(queue q);void dequeue(queue q);element_type front_deque(queue q);void dispose_que(queue q);#define mini_que 5struct QueRecord {    int capacity;    int size;    int front;    int rear;    element_type *array;};#endif

 

二、c文件:my_que.c

 

hangma@ubuntu:~/test/test/protest/que_test$ cat my_que.c#include 
#include
#include
#include "my_que.h"int IsEmpty(queue q){ return q->size == 0;}int IsFull(queue q){ return q->size == q->capacity;}queue creat_que(int max_element){ queue q; if(max_element < mini_que) {printf("the size of que is too small\n");exit(-2); } q = (queue)malloc(sizeof(struct QueRecord)); if(q == NULL) {printf("can't alloca memory\n"); exit(-1); } q->array = (element_type *)malloc(max_element * sizeof(element_type)); if(q->array == NULL) {printf("can't alloca the mem\n");exit(-1); } q->capacity = max_element; make_empty(q); return q;}void make_empty(queue q){ if(q != NULL) {q->size = 0; q->front = 1;q->rear = 0; }}int IsQueEnd(int value,queue q){ if( ++value == q->capacity) return 0; else return value;}void enqueue(element_type x,queue q){ if(q == NULL) {printf("the que is not exsit\n");exit(-2); } if(IsFull(q)) {printf("the que is full\n");exit(-2); } q->size++; q->rear = IsQueEnd(q->rear,q); q->array[q->rear] = x;}element_type front_que(queue q){ if(IsEmpty(q)) {printf("the que is empty\n");exit(-3); } return q->array[q->front];}void dequeue(queue q){ if(IsEmpty(q)) {printf("the que is empty\n");exit(-4); } q->size--; q->front = IsQueEnd(q->front,q);}element_type front_deque(queue q){ if(IsEmpty(q)) {printf("the que is empty");exit(-5); } q->size--; int front = q->front; q->front = IsQueEnd(q->front,q); return q->array[front];}void dispose_que(queue q){ if(q) {if(q->array) { free(q->array);}free(q); }}int main(int argc ,char *argv[]){ element_type val; int i = 0; queue q; q = creat_que(10); while( ++i <= 10 ) {printf("now ,please input the value:\n");scanf("%d",&val);printf("the val is %d\n",val);enqueue(val,q);printf("the q size is %d\n",q->size); } while(q->size) {val = front_deque(q);printf("the val is %d\n",val);sleep(1); } dispose_que(q); return 0;}

 

三、打印输出:

 

hangma@ubuntu:~/test/test/protest/que_test$ ./my_que now ,please input the value:1the val is 1the q size is 1now ,please input the value:2the val is 2the q size is 2now ,please input the value:3the val is 3the q size is 3now ,please input the value:4the val is 4the q size is 4now ,please input the value:5the val is 5the q size is 5now ,please input the value:6the val is 6the q size is 6now ,please input the value:7the val is 7the q size is 7now ,please input the value:8the val is 8the q size is 8now ,please input the value:9the val is 9the q size is 9now ,please input the value:10the val is 10the q size is 10the val is 1the val is 2the val is 3the val is 4the val is 5the val is 6the val is 7the val is 8the val is 9the val is 10

 

 

转载地址:http://mfjtx.baihongyu.com/

你可能感兴趣的文章
tomcat 8.0虚拟机配置文档
查看>>
轻松实现基于Heartbeat的高可用web服务集群
查看>>
pxc群集搭建
查看>>
JS中加载cssText延时
查看>>
常用的脚本编程知识点
查看>>
XILINX_zynq_详解(6)
查看>>
计算机网络术语总结4
查看>>
新手小白 python之路 Day3 (string 常用方法)
查看>>
HTML5 Geolocation API工作原理[转载]
查看>>
soapUI的简单使用(webservice接口功能测试)
查看>>
框架 Hibernate
查看>>
python-while循环
查看>>
【微信小程序】再次授权地理位置getLocation+openSetting使用
查看>>
手机端上传图片及java后台接收和ajaxForm提交
查看>>
HDU 5030 Rabbit's String
查看>>
【MSDN 目录】C#编程指南、C#教程、ASP.NET参考、ASP.NET 4、.NET Framework类库
查看>>
jquery 怎么触发select的change事件
查看>>
angularjs指令(二)
查看>>
(原創) 如何建立一个thread? (OS) (Linux) (C/C++) (C)
查看>>
<气场>读书笔记
查看>>