使用链表实现队列------《数据结构与算法分析-C语言描述》
经过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语言描述》的更多相关文章
- 数据结构与算法分析——C语言描述 第三章的单链表
数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...
- 《数据结构与算法分析——C语言描述》ADT实现(NO.00) : 链表(Linked-List)
开始学习数据结构,使用的教材是机械工业出版社的<数据结构与算法分析——C语言描述>,计划将书中的ADT用C语言实现一遍,记录于此.下面是第一个最简单的结构——链表. 链表(Linked-L ...
- C语言学习书籍推荐《数据结构与算法分析:C语言描述(原书第2版)》下载
维斯 (作者), 冯舜玺 (译者) <数据结构与算法分析:C语言描述(原书第2版)>内容简介:书中详细介绍了当前流行的论题和新的变化,讨论了算法设计技巧,并在研究算法的性能.效率以及对运行 ...
- 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)
#include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...
- 《数据结构与算法分析-Java语言描述》 分享下载
书籍信息 书名:<数据结构与算法分析-Java语言描述> 原作名:Data Structures and Algorithm Analysis in Java 作者: 韦斯 (Mark A ...
- 《数据结构与算法分析:C语言描述_原书第二版》CH3表、栈和队列_reading notes
表.栈和队列是最简单和最基本的三种数据结构.基本上,每一个有意义的程序都将明晰地至少使用一种这样的数据结构,比如栈在程序中总是要间接地用到,不管你在程序中是否做了声明. 本章学习重点: 理解抽象数据类 ...
- 《数据结构与算法分析——C语言描述》ADT实现(NO.02) : 队列(Queue)
第三个结构——队列(Queue) 队列与上次的栈相反,是一种先进先出(FIFO)的线性表.写入时只暴露尾部,读取时只暴露头部. 本次只实现了数组形式的队列.原因是链表形式的队列极为简单,只需要实现简单 ...
- 使用数组实现队列----《数据结构与算法分析---C语言描述》
一.h文件:my_que.h #ifndef _MY_QUE_H_ #define _MY_QUE_H_ struct QueRecord; typedef struct QueRecord* que ...
- 用链表实现栈----《数据结构与算法分析----C语言描述》
一.头文件: #ifndef _STACK_LINK_H_ #define _STACK_LINK_H_ struct stack_record; typedef struct stack_recor ...
随机推荐
- 关于MooTools你应该熟知的6个基本知识
MooTools是一个精简.模组化同时也面向对象的JavaScript框架,它设计给中等和进阶的JavaScript开发人员使用.使用 MooTools优美.详细而条理分明的API,可让你写出强大.富 ...
- HTML5 总结-视频-1
HTML5 视频 视频格式 当前,video 元素支持三种视频格式: 格式 IE Firefox Opera Chrome Safari Ogg No 3.5+ 10.5+ 5.0+ No MPEG ...
- BaseAdapter导致notifyDataSetChanged()无效的四个原因及处理方法
前一段时间在做一个项目的时候遇到了一个关于BaseAdapter的notifyDataSetChanged()方法无效问题,当时在网上搜了一个解决方法,今天又遇到了一个类似的问题,我在这里做个记录,防 ...
- C++惯用法:通过成员模板实现隐式转换(Coercion 强迫 by Member Template)
Intent To increase the flexibility of a class template's interface by allowing the class template to ...
- Tilemill + tilestream + mapbox.js 自制地图
感谢Mapbox,带来了一整套完整的地图方案. 你可以把你的地图放在Mapbox的网站上.也可以使用他们提供的开源软件自己架设地图服务. Mapbox的地图方案包括web,ios和android. 不 ...
- CSS中border-style的属性
属性可能的值 值 描述 none 定义无边框. hidden 与 "none" 相同.不过应用于表时除外,对于表,hidden 用于解决边框冲突. dotted 定义点状边框.在大 ...
- cocos2D(二)---- cocos2D文档的使用
在使用cocos2d进行游戏开发的过程中,难免要查阅cocos2d的API文档.搞清楚怎么使用某个类或者某个方法.幸运的是,cocos2d的作者已经在源码里面加入了文档凝视,我们仅仅须要使用文档生成工 ...
- ThinkPHP - 自动创建 + 自动验证 + 自动完成
自动创建:创建数据模型. $User->create(); 自动验证:验证提交的表单数据. protected $_validate = array( array('verify','requi ...
- [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)
题目链接:http://acm.swust.edu.cn/problem/0247/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- new Intent(String action,Uri uri)构造器说明
这是myDiary这个工程下MainActivity中的protected void onListItemClick(ListView l, View v, int position, long id ...