一、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 5 struct 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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:
1
the val is 1
the q size is 1
now ,please input the value:
2
the val is 2
the q size is 2
now ,please input the value:
3
the val is 3
the q size is 3
now ,please input the value:
4
the val is 4
the q size is 4
now ,please input the value:
5
the val is 5
the q size is 5
now ,please input the value:
6
the val is 6
the q size is 6
now ,please input the value:
7
the val is 7
the q size is 7
now ,please input the value:
8
the val is 8
the q size is 8
now ,please input the value:
9
the val is 9
the q size is 9
now ,please input the value:
10
the val is 10
the q size is 10
the val is 1
the val is 2
the val is 3
the val is 4
the val is 5
the val is 6
the val is 7
the val is 8
the val is 9
the val is 10

使用数组实现队列----《数据结构与算法分析---C语言描述》的更多相关文章

  1. 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)

    #include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...

  2. C语言学习书籍推荐《数据结构与算法分析:C语言描述(原书第2版)》下载

    维斯 (作者), 冯舜玺 (译者) <数据结构与算法分析:C语言描述(原书第2版)>内容简介:书中详细介绍了当前流行的论题和新的变化,讨论了算法设计技巧,并在研究算法的性能.效率以及对运行 ...

  3. 数据结构与算法分析——C语言描述 第三章的单链表

    数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...

  4. 《数据结构与算法分析——C语言描述》ADT实现(NO.00) : 链表(Linked-List)

    开始学习数据结构,使用的教材是机械工业出版社的<数据结构与算法分析——C语言描述>,计划将书中的ADT用C语言实现一遍,记录于此.下面是第一个最简单的结构——链表. 链表(Linked-L ...

  5. 《数据结构与算法分析-Java语言描述》 分享下载

    书籍信息 书名:<数据结构与算法分析-Java语言描述> 原作名:Data Structures and Algorithm Analysis in Java 作者: 韦斯 (Mark A ...

  6. 《数据结构与算法分析:C语言描述_原书第二版》CH3表、栈和队列_reading notes

    表.栈和队列是最简单和最基本的三种数据结构.基本上,每一个有意义的程序都将明晰地至少使用一种这样的数据结构,比如栈在程序中总是要间接地用到,不管你在程序中是否做了声明. 本章学习重点: 理解抽象数据类 ...

  7. 《数据结构与算法分析——C语言描述》ADT实现(NO.02) : 队列(Queue)

    第三个结构——队列(Queue) 队列与上次的栈相反,是一种先进先出(FIFO)的线性表.写入时只暴露尾部,读取时只暴露头部. 本次只实现了数组形式的队列.原因是链表形式的队列极为简单,只需要实现简单 ...

  8. 使用链表实现队列------《数据结构与算法分析-C语言描述》

    经过ubuntu的gcc验证 一.头文件 que_link.h #ifndef _QUE_LINK_H_ #define _QUE_LINK_H_ struct que_record; typedef ...

  9. 读书笔记:《数据结构与算法分析Java语言描述》

    目录 第 3 章 表.栈和队列 3.2 表 ADT 3.2.1 表的简单数组实现 3.2.2 简单链表 3.3 Java Collections API 中的表 3.3.1 Collection 接口 ...

随机推荐

  1. BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )

    这道题和蔡大神出的今年STOI初中组的第二题几乎一模一样... 先跑一遍最短路 , 再把所有边反向 , 再跑一遍 , 所有点两次相加的最大值即为answer --------------------- ...

  2. struts jsp传值到action,乱码的解决方案

    使用了Struts框架,前台写好了编码为utf-8 <%@ page language="java" contentType="text/html; charset ...

  3. Regex阅读笔记(一)之入门

    在检查一行文本时,^代表一行的开始,$代表结束. 字符数组:[],在里面列举任意多个字符,可以匹配其中任意一个字符,字符组元字符'-'表示一个范围. ^$表示一个空行(没有任何字符,包括空白字符) [ ...

  4. USB接口的SmartCard Class协议标准:ICCD and CCID

    ICCD是 Intergrated Circuit(s) card Device 的缩写.CCID是 Integrated Circuit(s) cards interface devices的缩写I ...

  5. kinect for windows - DepthBasics-D2D详解之二

    通过上篇文章,我们了解了在视频图像从kinect开发包传输到应用程序之前的一系列初始化工作,那么这篇文章主要来叙述,如何将一帧图像数据获取到,并显示出来的. 更新窗口是在Run函数消息处理中,当Kin ...

  6. 1214 线段覆盖wiki oi

    题目描述 Description 给定x轴上的N(0<N<100)条线段,每个线段由它的二个端点a_I和b_I确定,I=1,2,……N.这些坐标都是区间(-999,999)的整数.有些线段 ...

  7. android 调出显示标题栏(title bar)

    无法同时继承fragmentactivity和actionbaractivity 解决方法 import android.support.v7.app.ActionBarActivity; 将exte ...

  8. icon-font图标介绍

    前言 像素完美(Pixel Perfection).分辨率无关(Resolution Independent)和多平台体验一致性是设计师们的追求. 可访问性(Accessability).加载性能和重 ...

  9. Codeforces Round #198 (Div. 2) B. Maximal Area Quadrilateral

    B. Maximal Area Quadrilateral time limit per test 1 second memory limit per test 256 megabytes input ...

  10. 模拟Hibernate框架的小demo

    该程序为尚学堂马士兵老师讲解,模拟了hibernate的原理,主要应用了字符串拼接,反射知识. step1,新建数据库 use jd; create table _student( _id int(1 ...