一、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 1898: [Zjoi2004]Swamp 沼泽鳄鱼( dp + 矩阵快速幂 )

    ----------------------------------------------------------------------- #include<cstdio> #incl ...

  2. LNMP下基于端口的虚拟主机配置

    1.在/usr/local/nginx/conf/nginx.conf文件的的最后一个"}"前加上 include vhost/*.conf; 2.在/usr/local/ngin ...

  3. poj 1150 The Last Non-zero Digit

    /** 大意: 求A(n,m)的结果中从左到右第一个非零数 思路: 0是由2*5的得到的,所以将n!中的2,5约掉可得(2的数目比5多,最后再考虑进去即可) 那n!中2 的个数怎么求呢? int ge ...

  4. Sort list by merge sort

    使用归并排序对链表进行排序 O(nlgn) 的时间效率 /** * Definition for singly-linked list. * struct ListNode { * int val; ...

  5. (IOS)国际本地化设置

    首先New File,在Resource中选择Strings File: 然后命名该strings文件,必须命名为 Localizable : 再者选中该strings文件,在Localization ...

  6. S2S:分享出的营销机遇

    (速途网专栏 作者:高学争)经常在网上购物的你,有没有遇到过这样的问题:你知道自己想买什么,但是在淘宝上一搜,出来了数以万计的同类型,他们有着同样的价位(甚至可能是同样的图片),但是由不同的商家提供, ...

  7. C++堆和栈的比较(7个区别)

    基础知识: 堆 栈是一种简单的数据结构,是一种只允许在其一端进行插入或删除的线性表.允许插入或删除操作的一端称为栈顶,另一端称为栈底,对堆栈的插入和删除操作被称 为入栈和出栈.有一组CPU指令可以实现 ...

  8. IM-即时通讯技术概述

    IM-即时通讯技术概述 简述 即时通讯技术(IM)支持用户在线实时交谈.如果要发送一条信息,用户需要打开一个小窗口,以便让用户及其朋友在其中输入信息并让交谈双方都看到交谈的内容.大多数常用的即时通讯发 ...

  9. 在非MFC程序中使用调试宏 ASSERT(),VERIFY()和 TRACE()

    游戏制作已经开始采用C++了,却鲜有人选择使用MFC.但笔者觉得的 ASSERT(),VERIFY()和 TRACE()这几个宏很好用.所以就想自己写一个版本来适应Windows平台下不同的工程类型. ...

  10. poj 1664 put apples(dfs)

    题目链接:http://poj.org/problem?id=1664 思路分析:数据较小,考虑深度优先搜索搜索解空间. 代码如下: #include <iostream> using n ...