#ifndef _STATIC_QUEUE_H_
#define _STATIC_QUEUE_H_ // 静态queue模板,用数组实现的队列,在初始化的时候需要指定长度
template<class T>
class Static_Queue{
public:
Static_Queue(unsigned int size);
virtual ~Static_Queue();
bool push(T t);
bool pop(T &t);
unsigned int GetElementCnt();
void dump();
private:
unsigned int m_nSize;
T *m_arrT;
unsigned int m_nHead;
unsigned int m_nTail;
unsigned int m_nEmptyCnt;
}; template<class T>
Static_Queue<T>::Static_Queue(unsigned int size)
{
m_nSize = size;
m_arrT = new T[m_nSize];
memset(m_arrT,0,sizeof(T)*m_nSize);
m_nHead = 0;
m_nTail = 0;
m_nEmptyCnt = m_nSize;
} template<class T>
Static_Queue<T>::~Static_Queue()
{
delete[] m_arrT;
m_arrT = NULL;
} template<class T>
bool Static_Queue<T>::push(T t)
{
if( m_nEmptyCnt <= 0 )
{
return false;
}
m_arrT[m_nTail++] = t;
if(m_nTail >= m_nSize)
{
m_nTail = 0;
}
m_nEmptyCnt--;
return true;
} template<class T>
bool Static_Queue<T>::pop(T &t)
{
if( m_nEmptyCnt >= m_nSize )
{
return false;
}
t = m_arrT[m_nHead++];
if( m_nHead >= m_nSize )
{
m_nHead = 0;
}
m_nEmptyCnt++;
return true;
} template<class T>
unsigned int Static_Queue<T>::GetElementCnt()
{
return m_nSize - m_nEmptyCnt;
} template<class T>
void Static_Queue<T>::dump()
{
cout<<"head= "<<m_nHead<<" "<<"tail= "<<m_nTail<<endl;
cout<<"[";
for(int i = 0;i < m_nSize;i++ )
{
cout<<m_arrT[i]<<" ";
}
cout<<"]"<<endl;
} #endif

  测试代码:

#include <iostream>
#include "StaticQueue.h"
using namespace std; int main()
{
int i = 0;
int arr[] = {0,1,2,3,4,5,6,7,8,9};
Static_Queue<int> qu(8);
for(i = 0;i<10;i++)
{
if(qu.push(arr[i]) == false)
{
cout<<"push "<<arr[i]<<" fail"<<endl;
}
}
for(i = 0;i < 5;i++)
{
int t = 0;
if(qu.pop(t) == true)
{
cout<<"pop "<<t<<endl;
}
}
for(i = 0;i<10;i++)
{
if(qu.push(arr[i]) == false)
{
cout<<"push "<<arr[i]<<" fail"<<endl;
}
}
for(i = 0;i < 10;i++)
{
int t = 0;
if(qu.pop(t) == true)
{
cout<<"pop "<<t<<endl;
}
}
return 0;
}

  

C++用数组实现的静态队列的更多相关文章

  1. C++数组实现的循环队列

    #include<iostream> #include <string> /* 功能:数组实现的循环队列,C++实现,学习参考 */ using namespace std; ...

  2. JavaScript数组模拟栈和队列

    *栈和队列:js中没有真正的栈和队列的类型              一切都是用数组对象模拟的 栈:只能从一端进出的数组,另一端封闭       FILO   何时使用:今后只要仅希望数组只能从一端进 ...

  3. Javascript用数组实现栈和队列

    栈是遵循后进先出(LIFO)规则的一种有序集合,比如桌上的一叠书,我们只能从上面放或取. 队列是遵循先进先出(FIFO)规则的一种有序集合,比如排队,先排到的先离开. 数组也是一种有序的集合,它与上面 ...

  4. JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)

    前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...

  5. JAVA该队列中的数组,圆阵队列,链队列

    /** * 文件名:QueueText.java * 时间:2014年10月22下午9:05:13 * 笔者:维亚康姆维修 */ package chapter3; /** * 类名:ArrayQue ...

  6. Java的数组,栈,队列

    import java.util.Arrays; public class Array<E> { private E[] data; private int size; //构造函数,传入 ...

  7. 关于JS数组的栈和队列操作

    1.js支持重载吗? 虽然js 本身并没有函数重载,但是可以用arguments来模拟重载,函数名相同,参数不同,arguments的length属性,获取参数个数,索引属性获取参数值 2.什么是作用 ...

  8. [bzoj4698][Sdoi2008]Sandy的卡片_后缀数组_二分/单调队列_双指针

    Sandy的卡片 bzoj-4698 Sdoi-2008 题目大意:题目链接. 注释:略. 想法: 这个题跟一个Usaco的题特别像.我们把这些串差分 现在我们要求的就是公共子串且出现次数不少于$k$ ...

  9. 使用python实现数组、链表、队列、栈

    引言 什么是数据结构? 数据结构是指相互之间存在着一种或多种关系的数据元素的集合和该集合中数据元素之间的关系组成. 简单来说,数据结构就是设计数据以何种方式组织并存储在计算机中. 比如:列表,集合和字 ...

随机推荐

  1. Tr A(矩阵快速幂)

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  2. Hadoop虽然强大,但不是万能的(CSDN)

    Hadoop很强大,但企业在使用Hadoop或者大数据之前,首先要明确自己的目标,再确定是否选对了工具,毕竟Hadoop不是万能的!本文中列举了几种不适合使用Hadoop的场景. 随着 Hadoop  ...

  3. BZOJ3809: Gty的二逼妹子序列

    Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题.   对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数.   为了方 ...

  4. C# 使用 GetOleDbSchemaTable 检索架构信息(表、列、主键等)

    本文演示如何用 ADO.NET 中 OleDbConnection 对象的 GetOleDbSchemaTable 方法检索数据库架构信息.数据源中的架构信息包括数据库或可通过数据库中的数据源.表和视 ...

  5. python中迭代器和生成器

    l=[1,2,3,4] for n in l: print n 在看上面这段代码的时候,我们没有显式的控制列表的偏移量,就可以自动的遍历了整个列表对象.那么for 语句是怎么来遍历列表l的呢?要回答这 ...

  6. Odoo(OpenERP) 多个子类重载同一个父类方法的执行顺序及如何调用父类的父类方法

    首先说下起因,在修改英国会计模块(没错,就是那个安格鲁撒克逊记账模式!)中不符合中国国情的部分供能时,碰到了一个棘手的问题,简单的说就是B类继承它的父类A并重载了A的方法M,同时C类也继承了A类也重载 ...

  7. php页面之间传值

    echo("<script>window.open('2.php?head=".$head."');<script>");

  8. obout editor Absolute path for uploaded image

    本文转自:https://www.obout.com/editor_new/KnowledgeBase.aspx?id=706   Absolute path for uploaded image Q ...

  9. iOS coreData使用遇到的问题

    使用coreData,一定要有上下文环境.创建上下文时,和以前有些不同,之前 NSManagedObjeContext *context = [NSManagedObjectContext alloc ...

  10. 点击li标记中的<a>标记改变li背景图片怎样实现

    <div class="nav"><ul><li id="li1" class="dianji" onclic ...