`
chinrui
  • 浏览: 96752 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

循环队列的实现

阅读更多
循环队列(C++)

/* ----------------------------自定义循环队列---------------------------------*/
/*  function:
 * add value into the Queue
 * delete value from the Queue
 * count the number of in the Queue
 * judge the Queue is empty or not
 * judge the Queue is full or not
 */



#include <iostream>
#include <stdlib.h>

using namespace std;

//the max size is six so the capcity is five
#define MAX_SIZE 6

typedef int Elemtype;
typedef struct {
    Elemtype data[MAX_SIZE];
    int tailIndex;
    int headIndex;
} RoundQueue;

//declare the functions in this program
void initQueue(RoundQueue &);
void addValue(RoundQueue & , Elemtype);
Elemtype delValue(RoundQueue &);
void display(RoundQueue &);
bool isEmpty(RoundQueue &);
bool isFull(RoundQueue &);
int count(RoundQueue &);

int main()
{
    RoundQueue rq;
    initQueue(rq);

    for(int i = 0; i < MAX_SIZE - 1; i ++) {
        addValue(rq , i + 1);
    }
    cout << "---------before delete---------" << endl;
    display(rq);

    /*Elemtype eDel = delValue(rq);
    cout << eDel << endl;*/

    for(int i = 0; i < MAX_SIZE - 2; i++) {
        delValue(rq);
    }
    cout << "---------after delete---------" << endl;
    display(rq);

    for(int i = 0; i < MAX_SIZE - 2; i++) {
        addValue(rq , i + 1);
    }
    cout << "---------add value into the Queue again---------" << endl;
    display(rq);

    int iNumber = count(rq);
    cout << "---------the number of values in the Queue ---------" << endl;
    cout << iNumber << endl;
    return 0;
}

//initial an empty round Queue
void initQueue(RoundQueue &rq) {

    rq.headIndex = 0;
    rq.tailIndex = 0;
}

//add a value into the round Queue
void addValue(RoundQueue &rq , Elemtype value) {

    if(isFull(rq)) {
        return;
    }

    rq.tailIndex = (rq.tailIndex + 1) % MAX_SIZE;
    rq.data[rq.tailIndex] = value;
}

//delete a value from the round Queue
Elemtype delValue(RoundQueue &rq) {

    //judge the Queue is empty or not
    if(isEmpty(rq)) {
        return -1;
    }

    //delete the value and move the head index
    rq.headIndex = (rq.headIndex + 1) % MAX_SIZE;
    Elemtype eReturn = rq.data[rq.headIndex];
    return eReturn;
}

//display the values in the Queue
void display(RoundQueue &rq) {

    //judge the Queue is empty or not
    if(isEmpty(rq)) {
        return;
    }

    int index = (rq.headIndex + 1) % MAX_SIZE;

    //display values in the Queue
    while(true) {
        cout << rq.data[index] << " ";

        if(index == rq.tailIndex) {
            break;
        }

        index = (index + 1) % MAX_SIZE;
    }
    cout << endl;
}

//judge the Queue is empty or not
bool isEmpty(RoundQueue &rq) {

    if(rq.headIndex == rq.tailIndex) {
        return true;
    }

    return false;
}

//judge the Queue is full or not
bool isFull(RoundQueue &rq) {

    if((rq.tailIndex + 1) % MAX_SIZE == rq.headIndex) {
        return true;
    }

    return false;
}

//count the number of values in the queue
int count(RoundQueue &rq) {

    return (rq.tailIndex - rq.headIndex + MAX_SIZE) % MAX_SIZE;
}
分享到:
评论

相关推荐

    循环队列实现杨辉三角

    该C程序使用循环队列实现了N行杨辉三角的输出,实现简单。 使用VC进行编译即可。

    51单片机的FIFO(先入先出)循环队列实现

    51单片机的FIFO(先入先出)循环队列实现 51单片机是基于Intel 8051微控制器架构的一类单片机,在嵌入式系统开发中应用广泛。FIFO是一种数据结构,全称为First In First Out,即先进先出,类似于现实生活中的排队...

    循环队列实现斐波那契数列

    用循环队列实现的n阶斐波那契数列的求解。输出最后n-1个不大于max的斐波那契数列。

    k阶斐波那契序列 循环队列实现C源码

    以下是一个简单的循环队列实现: ```cpp #include struct Queue { int* arr; int front, rear; int capacity; Queue(int size) : arr(new int[size]), front(0), rear(0), capacity(size) {} ~Queue() { ...

    利用循环队列实现AOV 网的拓扑排序

    在本文中,我们将讨论如何使用循环队列实现AOV网的拓扑排序。循环队列是一种特殊的队列结构,它具有高效的存储和检索能力,可以有效地解决拓扑排序问题。 首先,我们需要对AOV网进行初始化,包括顶点的输入和存储,...

    循环队列的C++实现

    循环队列是一种线性数据结构,它在物理结构上实现了一个闭合的循环,因此队首和队尾可以在数组的任意位置。这种设计使得在队列满或空时,仍能有效地进行入队和出队操作,避免了普通队列在达到边界条件时的特殊处理。...

    循环队列实现杨辉三角的输出

    用循环队列实现杨辉三角的输出。通过该程序可以让你对循环队列有一定的理解。

    数据结构课程设计说明书贪吃蛇的循环队列实现.pdf

    数据结构课程设计说明书中的主题是使用循环队列实现贪吃蛇游戏。循环队列是一种特殊类型的线性数据结构,它在物理存储上是线性的,但在逻辑上表现为一个环形结构,具有首尾相连的特点。在贪吃蛇游戏中,循环队列被...

    循环队列实现源码(C、C++、Java)

    本文将深入探讨循环队列的概念、工作原理,并提供C、C++和Java三种语言的实现源码。 **循环队列概念** 循环队列是队列的一种优化版本,它利用数组的循环特性来模拟队列的操作。在循环队列中,队头元素和队尾元素的...

    用循环队列实现斐波那契数列的输出

    为了实现斐波那契数列,我们可以利用循环队列的特性,将最近的两个斐波那契数作为队列中的元素,并通过出队和入队操作计算新的斐波那契数。 首先,我们需要创建一个循环队列的结构体,包括存储斐波那契数的数组、队...

    Android之循环队列操作

    以下是一个简单的循环队列实现: ```java public class CircularQueue&lt;T&gt; { private T[] elements; private int front; // 队头位置 private int rear; // 队尾位置 private int size; public CircularQueue...

    用循环队列实现银行排队系统(VIP客户优先出队)

    利用循环队列来实现银行排队系统,对进入队列的客户分为VIP和普通客户,其中VIP优先出队。能实现的功能如下1.新客户排队等待服务 2.客户离开排队服务 3.查询当前客户前面还有几人 4.查询截止目前总共办理多少客户 注...

    循环队列的基本操作及应用----数据结构试验

    * 任务队列:在操作系统中,任务队列可以用循环队列实现,以便管理和调度系统中的任务。 * 网络缓冲区:在网络通信中,循环队列可以用来实现网络缓冲区,以便缓存和处理网络数据。 * 打印队列:在打印系统中,循环...

    循环队列实现求k阶斐波那契数列

    用循环队列编写求k阶斐波那契序列中前n+1项(f0,,f1,f2,…,fn)的算法,满足fn,而fn +1&gt;max,max为某个约定的常数,所用循环队列的容量为k,且算法结束时,留在队列中的元素为所求k阶斐波那契序列中的最后k项

    CircularQueue循环队列实现

    在这个实现中,我们主要探讨如何通过C++或者类似的编程语言来构建一个高效的循环队列,并将其封装成一个易于使用的库。 首先,我们需要理解循环队列的基本概念。在传统的线性队列中,如果队列的尾部达到了数组的...

    循环队列

    循环队列

    java队列实现(顺序队列、链式队列、循环队列)

    在Java中,队列的实现主要有三种:顺序队列、链式队列和循环队列。下面我们将详细探讨这三种队列的实现方式。 1. **顺序队列**: 顺序队列通常是基于数组实现的。在Java中,我们可以使用ArrayList或LinkedList来...

    java循环队列的分析和实例介绍.pdf

    以下是一个基于上述原理的循环队列实现的伪代码示例: ```pseudocode type QueueType = record Elements: array[1..MaxLength] of ElementType; front, rear: TPosition; end; function Front(Q: QueueType): ...

    简单的C语言循环队列

    C语言中的循环队列实现** 在C语言中,我们可以定义一个结构体来表示循环队列,包含数组、队头指针、队尾指针等成员。初始化时,队头和队尾指针均设为0,数组为空。入队操作时,将新元素添加到队尾指针指向的位置,...

Global site tag (gtag.js) - Google Analytics