使用链表实现队列------《数据结构与算法分析-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 ...
随机推荐
- Longest Palindromic Substring - 一题多解
题意是寻找一个字符串的最大回文字串,最简单的是n3方的算法,由于字符串最大长度为1000,所以这个方法很危险而且不科学. 紧接着想到的是一个n方的算法:回文子串是从中间向两边产生的,那么对于每个字符考 ...
- 织梦dedecms|图片模型内容页标签
图片列表开始:{dede:productimagelist}图片列表结束:{/dede:productimagelist}图片显示: [field:imgsrc/]图集缩略图: {dede: ...
- QT无标题窗口在任务栏显示关闭(增加系统菜单)
在对话框中使用了如下代码: setWindowFlags(Qt::FramelessWindowHint); 在任务栏上右键点击程序,不会弹出菜单,解决办法,使用下面代码: setWindowFlag ...
- C与C++ 无参函数的区别
在<C++ 编程思想>:“关于无参函数声明,C与C++有很大的差别.在C语言中,声明int fun1(),意味着一个可以有任意数目和类型的函数:而在C++中,指的却是一个没有参数的函数”. ...
- Talented Chef(简单题,被我想的太复杂了,用复杂的方法当然会超时咯,由此可见,并非所有题都是想的越多越好)
Description As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the ...
- [Swust OJ 179]--火柴棍(找规律)
题目链接:http://acm.swust.edu.cn/problem/0179/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- Java Pattern Matcher 正则应用
转自:http://www.itzhai.com/java-notes-regex-matches-and-lookingat.html#read-more 1.基本语法 2.String内建的正则表 ...
- vmware时间不同步的问题
请以管理员身份运行(root)
- Filter学习
在这之前一直对filter感到陌生,有点细思极恐的感觉--终于下定决心来学习一下,欢迎拍砖-- Filter的主要作用是实现对HttpServletRequest的预处理,也可以对HttpServle ...
- Winsock在Windows下的编程教程(C语言)(图文并茂,超长教程)
https://www.0xaa55.com/forum.php?mod=viewthread&tid=378&extra=page%3D2