使用链表实现队列------《数据结构与算法分析-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 ...
随机推荐
- [C++参考]私有成员变量的理解
私有成员变量的概念,在脑海中的现象是,以private关键字声明,是类的实现部分,不对外公开,不能在对象外部访问对象的私有成员变量. 然而,在实现拷贝构造函数和赋值符函数时,在函数里利用对象直接访问了 ...
- [LeetCode]题解(python):044-Wildcard Matching
题目来源: https://leetcode.com/problems/wildcard-matching/ 题意分析: 定义两个新字符规则,'?'代表任意一个字符,’*‘代表任意长度的任意字符.输入 ...
- mybatis用logback日志不显示sql的解决办法
mybatis用logback日志不显示sql的解决方法 1.mybatis-config.xml的设定 关于logimpl的设定值还不支持logback,如果用SLF4J是不好用的. 这是官方文档的 ...
- commons-logging和slf4j都是日志的接口
过上面的图,可以简单的理清关系! commons-logging和slf4j都是日志的接口,供用户使用,而没有提供实现! log4j,logback等等才是日志的真正实现. 当我们调用接口时,接口的工 ...
- STL assign 和swap
首先看下在整个container上面的复制. c1=c2 可以等同于 c1.erase(c1.begin(),c1.end()) //delete all elems in c1 c1.insert( ...
- Windows Azure 社区新闻综述(#76 版)
欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure 的社区推动新闻.内容和对话.以下是本周的亮点. 文章.视频和博客文章 · 更新 Windows Azure 中的 SQ ...
- Linux UDEV和为MySQL InnoDB共享表空间配置裸设备
⑴ UDEV 基础 udev 可管理保存在/dev 目录下的文件.文件只有在接入相应设备后才会生成.设备被拔出后自动删除 它还允许用户添加规则.以便修改/dev中默认的名称和权 ...
- HDU 2087 剪花布条 KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2087 KMP匹配数—— AC代码: #include <iostream> #includ ...
- 用typedef声明类型
定义: 可以用typedef声明一个新的类型名来代替已有的类型名. 用法: typedef int INTEGER;//指定用标识符INTEGER代表int类型 typedef float REAL; ...
- ZSTU OJ 3999 零基础学算法---邻接表
题目:Click here 题意:我就喜欢中文题! 分析:这个题虽然是中文题,但是还是有一点费解的.其实就是给你一棵树,是用图的形式给你的,只知道a,b之间有一条边,并不知道谁是父,谁是子.思路就是先 ...