数据结构之队列

以下代码基本均来自于慕课网:http://www.imooc.com/learn/519


1.定义

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。
队列的数据元素又称为队列元素。在队列中插入一个队列元素称为入队,从队列中删除一个队列元素成为出队。因为队列只允许在一段插入,在另一端删除,所以只有最早进入队列的元素才能最先从队列中删除,故队列又称为先进先出(FIFO—first in first out)线性表。

2.循环队列的数组实现

在本次实现中,使用了两个指针,m_head和m_tail,分别指向队列的队头和队尾。初始情况下,m_head和m_tail都指向位置0。新的元素从位置0开始插入。当向队列中插入第一个元素,m_head不变,m_tail指向位置1。插入第二个元素时,m_head不变,m_tail指向位置2。当移除一个元素时,m_head指向位置1,m_tail不变,还是指向位置2。所以,也就是,m_head始终指向队列中的第一个元素,而m_tail指向队列下一个新元素的位置。

queue.h

/*
环形队列
*/

class MyQueue
{
public:
    MyQueue(int queueCapacity);//initiate queue
    virtual ~MyQueue();//destroy queue
    void ClearQueue();//clear queue
    bool QueueEmpty() const;//detect whether queue is empty
    bool QueueFull() const;//detect whether queue is full
    int QueueLength() const;//length of the queue
    bool EnQueue(int element);//new element enter the queue
    bool DeQueue(int &element);//first element exit the queue
    void QueueTraverse();//print the queue elements

private:
    int *m_pQueue;//array pointer of the queue;
    int m_head;//head of the queue
    int m_tail;//tail of the queue
    int m_iQueueLen;//number of elements of the queue
    int m_iQueueCapacity;//capacity of the queue

};



queue.cpp

#include "queue.h"
#include<iostream>
using namespace std;

MyQueue::MyQueue(int queueCapacity)
{
    m_iQueueCapacity = queueCapacity;
    m_pQueue = new int[m_iQueueCapacity];
    ClearQueue();
}

MyQueue::~MyQueue()
{
    delete []m_pQueue;
    m_pQueue = NULL;
}

void MyQueue::ClearQueue()
{
    m_head = 0;
    m_tail = 0;
    m_iQueueLen = 0;
}

bool MyQueue::QueueEmpty() const
{
     if(m_iQueueLen == 0)
     {
        return true;
     }
     else
     {
        return false;
     }
}

bool MyQueue::QueueFull() const
{
     if(m_iQueueLen == m_iQueueCapacity)
     {
        return true;
     }
     else
     {
        return false;
     }
}

int MyQueue::QueueLength() const
{
      return m_iQueueLen;
}

bool MyQueue::EnQueue(int element)
{
      if(QueueFull())
      {
         return false;
      }
      else
      {
        m_pQueue[m_tail] = element;
        m_tail++;
        m_tail = m_tail % m_iQueueCapacity;
        m_iQueueLen ++;
        return true;
      }
}

bool MyQueue::DeQueue(int &element)
{
      if(QueueEmpty())
      {
         return false;
      }
      else
      {
        element = m_pQueue[m_head];
        m_head++;
        m_head = m_head % m_iQueueCapacity;
        m_iQueueLen --;
        return true;
      }
}

void MyQueue::QueueTraverse()
{
      cout << "------------------------" << endl;
      for(int i = m_head; i < m_iQueueLen + m_head; i++)
      {
        cout << m_pQueue[i % m_iQueueCapacity] << endl;
      }
      cout << "------------------------" << endl;
}


main.cpp

#include <iostream>
#include "queue.h"

using namespace std;

int main()
{
    MyQueue *q = new MyQueue(6);

    q->EnQueue(20);
    q->EnQueue(30);
    q->EnQueue(40);
    q->EnQueue(50);
    q->EnQueue(60);
    q->QueueTraverse();

    int element = 0;
    if(q->DeQueue(element))
    {
        cout << "removed:" << element << endl;
        q->QueueTraverse();
    }
    else{
        cout << "error";
    }

    if(q->DeQueue(element))
    {
        cout << "removed:" << element << endl;
        q->QueueTraverse();
    }
    else{
        cout << "error";
    }

    delete q;
    q = NULL;

    return 0;
}


程序的运行结果如下:




循环队列的链表实现,未完待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值