使用链表实现队列------《数据结构与算法分析-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 ...
随机推荐
- mysqld守护进程
1.安装方式:安装文件:可执行的二进制文件: 源代码编译. 2.版本选择:常见版本区别:GA(一般应用,尽量使用最新版本)/RC(候选发布版本)/测试版本实版本选择主要是够用.适用.好用!不一定是最新 ...
- 用Response对象的write方法和<%%>及<%=%>输出同样效果的乘法表格
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Response1.aspx ...
- js左侧三级菜单导航代码
效果演示: 实例代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- kinect for windows - 初认识
kinect是微软开发的一种计算机输入设备,原来只是用于xbox,kinect负责捕捉用户的动作,让xbox游戏做出相应的反应.很快大家对此非常有兴趣,因此有些geek和组织为kinect开发了驱动和 ...
- ASP.NET 内置对象涉略
一.ASP.NET中内置的常用对象的介绍 本文列举了ASP.NET 的八个内置对象,其中前五个是比较常用的. 1.Response Response 对象用于从服务器向用户发送输出的结果. Write ...
- 复习知识点:GCD多线程
GCD的基础 #pragma mark - 使用GCD 创建一个 串行 队列 // 第一种:系统提供的创建串行队列的方法 // 在真正的开发中如果需要创建串行队列,比较习惯用这种 // dispatc ...
- 【Android 多媒体开发】 MediaPlayer 状态机 接口 方法 解析
作者 : 韩曙亮 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/38487967 一. MediaPlayer 状态机 介绍 ...
- Asp.Netserver控件开发的Grid实现(三)列编辑器
以下是GridColumnsEditor的实现代码: GridColumnsEditor.cs using System; using System.Collections.Generic; usin ...
- c 中有关打印*,字符的题目集
#include<stdio.h> //1.打印* void priStar() { printf("输入一个整数\n"); int num; scanf(" ...
- Oracle同义词 synonyms
Oracle中的同义词: 总结:简单的一句话,Oracle中不同用户的表一般都只能够自己的所属的用户可以用,如果不想通过授权的方式授权给其他用户使用,那么创建表的时候在表名的前面加上 synonyms ...