队列的C++实现

队列的定义:队列是一种运算受限的线性表,它限定在表的一端进行插入操作,在表的另一端进行删除操作

其中:
允许插入的一端称作队尾
允许删除的一端称作队首

队列的顺序实现(C++)


初始化操作: InitQueue()
取队首元素操作: GetHead()
入列操作: EnQueue()
出列操作: DeQueue ()

template 
class Queue
{
public:
    virtual bool IsEmpty() const = 0;
    virtual bool IsFull() const = 0;
    virtual bool Front(T &x) const = 0;
    virtual bool EnQueue(T x) = 0;
    virtual bool DeQueue() = 0;
    virtual void Clear() = 0;
    virtual int Length() = 0;
};
template 
class SeqQueue :public Queue
{
public:
    SeqQueue(int mSize)
    {
        maxSize = mSize;
        q = new T[maxSize];
        front = rear = 0;
    }
    ~SeqQueue() { delete[]q; };
    bool IsEmpty() const { return front == rear; }
    bool IsFull() const { return (rear + 1) % maxSize == front; }
    bool Front(T &x) const //在x中返回队头元素
    {
        if (IsEmpty())  //空队列处理
        {
            cout 

队列的链式实现


typedef int ElemType;
typedef struct  qnode
{
    ElemType data;
    struct qnode * next;
}DataNode;
typedef struct
{
    DataNode *front;
    DataNode *rear;
}LinkQuNode;

void InitQueue(LinkQuNode *&q)
{
    q = new LinkQuNode;
    q->front = q->rear = NULL;
}
bool IsEmpty(LinkQuNode *q)
{
    return (q->rear == NULL);
}
void EnQueue(LinkQuNode *&q,ElemType Data)
{
    DataNode *temp = new DataNode;
    temp->data = Data;
    temp->next = NULL;
    if (IsEmpty(q))
    {
        q->rear = temp;
    }
    else
    {
        q->rear->next = temp;
        q->rear = temp;
    }
}
bool DelQueue(LinkQuNode *& q)
{
    if (IsEmpty(q))
    {
        cout 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值