【队列】基于循环数组的实现

Description

 请完成以下队列类的实现:(请注意数组实现应该为循环数组

enum ErrorCode

{

         success,

         underflow,

         overflow

};

const int maxQueue = 100;

template <class QueueEntry>

class MyQueue

{

public:

         MyQueue();

         bool empty() const;  // 判断队列是否为空

         ErrorCode append(const QueueEntry &item);  // 入队操作

         ErrorCode serve();// 出队操作

         ErrorCode retrieve(QueueEntry &item) const;   // 获取队头元素

         bool full() const; // 判断队列是否已满

         int size() const; // 获取队列已有元素个数

         void clear();  // 清除队列所有元素

         ErrorCode retrieve_and_serve(QueueEntry &item);  // 获取队头元素并出队

private:

         int front;                             // 队头下标

         int rear;                              // 队尾下标

         QueueEntry entry[100];       // 队列容器

};

Problem Source: 第二周 5班

 

解题思路:

实现循环数组

Circular arrays(循环数组)

将数组设想为一个循环的,而非线性的; 用两个下标front和rear记录队头和队尾位置; 添加元素时,rear右移,将元素置于rear位置。当rear等于max时(last index), rear 置 0. 元素出队时,删除位于front位置的元素,然后front右移. 当front 等于 max时, 置 front 为 0。

Boundary conditions

问题:无法区分满队列与空队列。

解决方法:

1. 在数组中空一个位置;

2. 使用一个布尔量表示队列是否满。当rear刚好到达front之前时,置 此标志为true.

3. 使用一个计数器( counter)以记录队列中的元素个数。

 

此处代码采用第一种解决办法:

1.front始终指向队列第一个元素,即总是放着东西的;

   rear始终指向队列的下一个位置,即总是空着的;

2.假设数组开到100,那么队列满时,实际长度为99,判断为满:(rear + 1) % 100 == front

                                                                    判断为空:front==rear

3.当排到数组尾部,要跳到头部来继续进行循环。

实现代码:

enum ErrorCode {
    success,
    underflow,
    overflow
};

const int maxQueue = 100;

template <class QueueEntry>
class MyQueue
{
public:
         MyQueue();
         bool empty() const;
         ErrorCode append(const QueueEntry &item);
         ErrorCode serve();
         ErrorCode retrieve(QueueEntry &item) const;
         bool full() const;
         int size() const;
         void clear();
         ErrorCode retrieve_and_serve(QueueEntry &item);
private:
         int front;  
         int rear;   
         QueueEntry entry[100]; 
};

template <class QueueEntry>
MyQueue<QueueEntry>::MyQueue() {      //初始化 
      front = rear = 0;
}

template <class QueueEntry>
bool MyQueue<QueueEntry>:: empty() const {   //判断队列是否为空 
     if (front == rear) return true;
     else return false; 
}

template <class QueueEntry>
ErrorCode MyQueue<QueueEntry>::append(const QueueEntry &item) {//入队 
    if (full()) {
        return overflow;
    } else {
        entry[rear] = item;
        rear++;
        if (rear  == 100) rear = 0;//从数组头部继续进行循环 
        return success;
    }
}

template <class QueueEntry>
ErrorCode MyQueue<QueueEntry>::serve() {  //出队 
    if (empty()) {
        return underflow;
    } else {
        front++;
        if (front == 100) front = 0;
        return success;
    }
}

template <class QueueEntry>
ErrorCode MyQueue<QueueEntry>::retrieve(QueueEntry &item) const {  //取队头 
     if (empty()) {
         return underflow;
     } else {
         item = entry[front];
         return success;
     }
}

template <class QueueEntry>
bool MyQueue<QueueEntry>::full() const {  //判断队列是否已满 
     if ((rear + 1) % 100 == front) return true;
     else return false;
}

template <class QueueEntry>
int MyQueue<QueueEntry>::size() const {  //判断队的长度 
    if (front <= rear) {
        return (rear - front);        
    } else {
      return (100 - front + rear);
    }
}

template <class QueueEntry>
void MyQueue<QueueEntry>::clear() {  //清除队列 
    rear = front = 0;
}

template <class QueueEntry>
ErrorCode MyQueue<QueueEntry>::retrieve_and_serve(QueueEntry &item) {//取队头并出队 
    ErrorCode flag;
    flag = retrieve(item);
    serve();
    return flag;
}                                 

(本博文或多或少参考过其他网上资料,但时间已久忘记当初的参考了,在此对他们表示感谢!)

转载于:https://www.cnblogs.com/zengyh-1900/p/4064287.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
字符循环队列是一种基于数组实现队列,它可以有效地支持插入和删除操作,并且可以在固定的存储空间内高效地实现数据的循环利用。下面是字符循环队列的表示和实现方法: 1. 队列的表示 在字符循环队列的表示中,我们需要定义一个数组和两个指针,分别表示队列的头和尾。由于队列循环的,因此在队列满时,尾指针需要回到数组的起始位置。在代码实现中,我们可以使用取模运算实现循环队列的“循环”。 ```c++ #define MAXSIZE 100 // 队列的最大长度 typedef struct { char data[MAXSIZE]; // 存储队列元素的数组 int front; // 队头指针 int rear; // 队尾指针 } Queue; ``` 2. 队列的初始化 队列的初始化操作包括创建一个空队列,将队头和队尾指针初始化为0。 ```c++ void InitQueue(Queue *Q){ Q->front = Q->rear = 0; // 队头和队尾指针初始化为0 } ``` 3. 入队操作 入队操作包括在队尾插入一个元素,并将队尾指针向后移动一位。如果队列已满,则无法插入新元素。 ```c++ bool EnQueue(Queue *Q, char x){ if((Q->rear + 1) % MAXSIZE == Q->front) // 队列已满 return false; Q->data[Q->rear] = x; // 插入新元素 Q->rear = (Q->rear + 1) % MAXSIZE; // 队尾指针向后移动一位 return true; } ``` 4. 出队操作 出队操作包括删除队头元素,并将队头指针向后移动一位。如果队列为空,则无法删除元素。 ```c++ bool DeQueue(Queue *Q, char *x){ if(Q->front == Q->rear) // 队列为空 return false; *x = Q->data[Q->front]; // 删除队头元素 Q->front = (Q->front + 1) % MAXSIZE; // 队头指针向后移动一位 return true; } ``` 5. 获取队列长度 获取队列长度操作需要计算队列中元素的个数,即队尾指针减去队头指针。 ```c++ int GetQueueLength(Queue *Q){ return (Q->rear - Q->front + MAXSIZE) % MAXSIZE; } ``` 这些就是字符循环队列的表示和实现方法。在实际应用中,我们可以根据需要进行相应的修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值