一、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. linux: telnet

    问题: telnet: connect to address 192.168.1.103: Connection refused 总结:{ 1. 需要开启telnet服务, /etc/xinetd.d ...

  2. linux kernel中timer的使用

    linux kernel中timer的使用 http://blog.csdn.net/njuitjf/article/details/16888821 在kernel中如果想周期性的干些什么事情,或者 ...

  3. Hadoop运行中遇到的错误调试

    在运行Hadoop的过程中遇到的最多的问题就是DataNode不能正常的启动,各种问题都有可能,说一下我遇到的两种情况: (1)第一种情况是Master的防火墙没有关闭.这样在启动Hadoop的时候, ...

  4. Android 常用开源代码整理

    1.AndroidAnnotations一个强大的android开源注解框架, 基本上可以注入任何类型, 比一般的所谓的注入框架要快, 因为他是通过生成一个子类来实现的绑定.具体查看文档. 2.and ...

  5. Windows下搭建objective C开发环境

    摘自:http://blog.csdn.net/zhanghefu/article/details/18320827 最近打算针对iPhone.iPod touch和iPad开发一些应用,所以,需要开 ...

  6. ASP.NET PipeLine #Reprinted#

    从ASP.NET 1.0 起,相比于ASP中的COM, PipeLine 就是一项重大的改进. ASP.NET 时代的管道模型究竟是怎么样的? 我们能接触到的四个最重要的概念就是HttpApplica ...

  7. 使用Jquery后去div个数

    <div id="tree1" class="tree-folder-content"> <div class="tree-fold ...

  8. JQuery 实现返回顶部效果

    首先要里了解一下几个知识 $(window).scrollTop() ---滚动条距顶部距离 fadeIn() 方法使用淡入效果来显示被选元素,假如该元素是隐藏的. fadeOut() 方法使用淡出效 ...

  9. springMVC框架下JQuery传递并解析Json数据

    springMVC框架下JQuery传递并解析Json数据

  10. ckeditor3.4.2是否升级为4.2.1的问题

    ckeditor官网访问地址: http://ckeditor.com/demo 目前公司项目中用到富文本编辑器基本都是cheditor3.4.2, 在不修改其源码的情况下,不兼容于IE10,具体见& ...