经过ubuntu的gcc验证

一、头文件 que_link.h

#ifndef _QUE_LINK_H_
#define _QUE_LINK_H_ struct que_record;
typedef struct que_record* que;
struct link_node;
typedef struct link_node* node;
typedef int elementType; int IsFull(que q);
int IsEmpty(que q);
que creatQue(int max_num);
void makeEmpty(que q);
void enque(elementType x,que q);
void deque(que q);
elementType front_que(que q);
elementType front_deque(que q);
void dispose_que(que q); struct que_record
{
node front;
node rear;
int size;
}; struct link_node
{
elementType data;
struct link_node* next;
}; #endif

二、c文件:que_link.c

#include <stdio.h>
#include <stdlib.h>
#include "que_link.h"

#define MAXSIZE 10

int IsFull(que q)
{
return q->size == MAXSIZE;
} int IsEmpty(que q)
{
return q->size == 0;
} que creatQue(int max_num)
{
que q;
q = (que)malloc(sizeof(struct que_record));
q->size = 0;
q->front = q->rear = (node)malloc(sizeof(struct link_node));
q->front->next = q->rear->next = NULL;
return q;
} void makeEmpty(que q)
{
if(NULL == q)
{
printf("the que is not exsit \n");
exit(-1);
} while(q->size)
deque(q);
} void deque(que q)
{ node ptr = NULL; ptr = q->front->next;
free(q->front);
q->front = ptr;
q->size--; if(q->size == 0)
{
//q->front->next = q->rear->next = NULL;
q->front = q->rear = NULL;
}
} void enque(elementType x, que q)
{
if(q)
{
if(IsFull(q))
{
printf("the que is full \n");
exit(-4);
} printf("the enque x is %d\n",x);
static int init_flag = 0;
if(!init_flag)
{
q->rear->data = x;
q->rear->next = NULL;
q->size++;
init_flag = 1;
}
else
{
node ptr=(node )malloc(sizeof(struct link_node));
ptr->data = x;
q->rear->next = ptr;
q->rear = q->rear->next;
q->rear->next = NULL;
q->size++;
}
}
} elementType front_que(que q)
{
if(q)
{
if(IsEmpty(q))
{
printf("the que is empty\n");
exit(-5);
} return q->front->data;
}
} elementType front_deque(que q)
{
if(q)
{
if(IsEmpty(q))
{
printf("the que is empty,so can't deque\n");
exit(-6);
} elementType x;
x = q->front->data;
deque(q);
return x;
}
} void dispose_que(que q)
{
if(q)
{
makeEmpty(q);
free(q);
}
} int main(int argc,char *argv[])
{
elementType val;
int i = 0;
que q;
q = creatQue(10);
while(i++ < 10 )
{
printf("now ,please input the value:\n");
scanf("%d",&val);
printf("the val is %d\n",val);
enque(val,q);
printf("the q size is %d\n",q->size);
if(val == 0)
break;
} 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$ gcc  que_link.c -o que_link
hangma@ubuntu:~/test/test/protest/que_test$ ./que_link
now ,please input the value:
1
the val is 1
the enque x is 1
the q size is 1
now ,please input the value:
2
the val is 2
the enque x is 2
the q size is 2
now ,please input the value:
3
the val is 3
the enque x is 3
the q size is 3
now ,please input the value:
4
the val is 4
the enque x is 4
the q size is 4
now ,please input the value:
5
the val is 5
the enque x is 5
the q size is 5
now ,please input the value:
6
the val is 6
the enque x is 6
the q size is 6
now ,please input the value:
7
the val is 7
the enque x is 7
the q size is 7
now ,please input the value:
8
the val is 8
the enque x is 8
the q size is 8
now ,please input the value:
9
the val is 9
the enque x is 9
the q size is 9
now ,please input the value:
10
the val is 10
the enque x 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语言描述 第三章的单链表

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

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

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

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

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

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

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

  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语言描述》

    一.h文件:my_que.h #ifndef _MY_QUE_H_ #define _MY_QUE_H_ struct QueRecord; typedef struct QueRecord* que ...

  9. 用链表实现栈----《数据结构与算法分析----C语言描述》

    一.头文件: #ifndef _STACK_LINK_H_ #define _STACK_LINK_H_ struct stack_record; typedef struct stack_recor ...

随机推荐

  1. winsock2获取网页

    #define WIN32_LEAN_AND_MEAN#include <winsock2.h>#include <stdlib.h>#include <stdio.h& ...

  2. 「C」 数组、字符串、指针

    一.数组 (一)数组 概念:用来存储一组数据的构造数据类型 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. (二)数组的定义 格式: 类型 数组名[元素个数 ...

  3. 杭电ACM 素数判定

    素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  4. APACHE的伪静态设置

    1.配置httpd.conf #LoadModule rewrite_module modules/mod_rewrite.so 开启 LoadModule rewrite_module module ...

  5. Python之路Day13

    day13主要内容:JavaScript.DOM.jQuery 武Sir blog:http://www.cnblogs.com/wupeiqi/articles/5369773.html JavaS ...

  6. CMD获取当前目录的绝对路径

    @echo offecho 当前盘符:%~d0echo 当前盘符和路径:%~dp0echo 当前批处理全路径:%~f0echo 当前盘符和路径的短文件名格式:%~sdp0echo 当前CMD默认目录: ...

  7. sql server单表导入、导出

    sql server单表导入.导出(通过CSV文件) 导出:直接打开查询分析器查询要导出表的信息(select *  from 表),得到的结果全选,右键另存为 xxx.csv文件  (得到该表的所有 ...

  8. Apache中RewriteCond规则参数介绍(转)

    CodeIgniter2.0已经出来有20多天了呢~也就是我一直用的php框架(CI).一直都在研究jquery,倒是把CI给忘到一边去了,呵呵~~今天公司事情不是很多,于是开始熟悉一下CI2.0的一 ...

  9. cmake 学习笔记(三)

    转自:http://blog.csdn.net/dbzhang800/article/details/6329314 接前面的 Cmake学习笔记(一) 与 Cmake学习笔记(二) 继续学习 cma ...

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

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