C语言实现循环队列
2016-02-06 13:24
513 查看
C语言实现的循环队列,用数组形式实现的,下面是实现的代码,初始化,插入,删除,和打印操作。
#include<stdlib.h>
#include"seq_queue.h"
#include <stdio.h>
#include<math.h>
static bool is_full(seq_queue *q)
{
return (q->tail+1)%MAX_SIZE == q->head;
}
bool is_empty(seq_queue *q)
{
return q->head == q->tail;
}
bool init_queue(seq_queue *q)
{
if(q==NULL)
{
return false;
}
q->head=0;
q->tail=0;
return true;
}
bool push_queue(seq_queue *q,ELEM_TYPE e)
{
if(is_full(q))
{
printf("队列已满!\n");
return false;
}
q->data[q->tail]=e;
q->tail=(q->tail+1)%MAX_SIZE;
return true;
}
bool pop_queue(seq_queue *q,ELEM_TYPE *e)
{
if(is_empty(q))
{
printf("队列为空!\n");
return false;
}
*e=q->data[q->head];
q->head=(q->head+1)%MAX_SIZE;
return true;
}
bool show_queue(seq_queue *q)
{
if(q==NULL || is_empty(q))
{
return false;
}
int i=0;
if(q->tail>q->head)
{
for(i=q->head;i<q->tail;i++)
{
printf("%d ",q->data[i]);
}
}
else if(q->tail<q->head)
{
for(i=q->head;i<MAX_SIZE;i++)
{
printf("%d ",q->data[i]);
}
for(i=0;i<q->tail;i++)
{
printf("%d ",q->data[i]);
}
}
return true;
}
#include<stdlib.h>
#include"seq_queue.h"
#include <stdio.h>
#include<math.h>
static bool is_full(seq_queue *q)
{
return (q->tail+1)%MAX_SIZE == q->head;
}
bool is_empty(seq_queue *q)
{
return q->head == q->tail;
}
bool init_queue(seq_queue *q)
{
if(q==NULL)
{
return false;
}
q->head=0;
q->tail=0;
return true;
}
bool push_queue(seq_queue *q,ELEM_TYPE e)
{
if(is_full(q))
{
printf("队列已满!\n");
return false;
}
q->data[q->tail]=e;
q->tail=(q->tail+1)%MAX_SIZE;
return true;
}
bool pop_queue(seq_queue *q,ELEM_TYPE *e)
{
if(is_empty(q))
{
printf("队列为空!\n");
return false;
}
*e=q->data[q->head];
q->head=(q->head+1)%MAX_SIZE;
return true;
}
bool show_queue(seq_queue *q)
{
if(q==NULL || is_empty(q))
{
return false;
}
int i=0;
if(q->tail>q->head)
{
for(i=q->head;i<q->tail;i++)
{
printf("%d ",q->data[i]);
}
}
else if(q->tail<q->head)
{
for(i=q->head;i<MAX_SIZE;i++)
{
printf("%d ",q->data[i]);
}
for(i=0;i<q->tail;i++)
{
printf("%d ",q->data[i]);
}
}
return true;
}
相关文章推荐
- 解决VS2010子目录中的.cpp文件引用上一级目录的stdafx.h找不到定义的问题
- C语言可变参函数的实现原理浅析
- 快速排序---C语言实现
- c++拷贝函数
- [c++基础]valarray模板详解
- 栈的简单实现——使用C++容器库(STL Stack)
- C++面向对象编程:对象的内存分配与静态成员
- 1012. The Best Rank (25)
- C++动态调用DLL
- 理解C语言——从小菜到大神的晋级之路(8)——数组、指针和字符串
- C++ 经常使用类 string类
- C语言之数值计算--级数算法
- C语言之数值计算--级数算法
- C语言之数值计算--级数算法
- c++ 对象的互斥访问
- C 指针有害健康
- 一起talk C栗子吧(第一百二十一回:C语言实例--线程知识体系图)
- 【LeetCode】9. Palindrome Number
- c++n连环
- 第六届蓝桥杯C/C++B组第八题 移动距离