算法导论10.1队列

N~WKVA~K77_5OYL7DHEO~7A

 

注意n个空间的队列,有最多n - 1个元素

RRJLZY634M2{@TKUVKJ(8BM

 

8E{WYRS87E9(DF[D@S5]BJG

 
/*
 * IA_10.1queue.h
 *
 *  Created on: Feb 13, 2015
 *      Author: sunyj
 */

#ifndef IA_10_1QUEUE_H_
#define IA_10_1QUEUE_H_

#include <iostream>
#include <cstdint>

// ENQUEUE(Q, x)
// Q[Q.tail] = x
// if Q.tail = Q.length
//     Q.tail = 1
// else Q.tail = Q.tail + 1

// DEQUEUE(Q)
// x = Q[Q.haed]
// if Q.head == Q.length
//     Q.head = 1
// Q.head = Q.head + 1
// return x

template <class T> class queue {
public:
    queue(int64_t const n) : head(0), tail(0), length(n)
    {
        data = new T[n]();
    }
    bool full()
    {
        if (head == tail + 1 || (0 == head && length == tail + 1))
        {
            return true;
        }
        return false;
    }
    bool empty() { return head == tail; }
    void print()
    {
        if (empty())
        {
            return ;
        }
        if (head < tail)
        {
            for (int64_t i = head; i < tail; i++)
            {
                std::cout << data[i] << " ";
            }
        }
        else
        {
            for (int64_t i = head; i < length; i++)
            {
                std::cout << data[i] << " ";
            }
            for (int64_t i = 0; i < tail; i++)
            {
                std::cout << data[i] << " ";
            }
        }
        std::cout << std::endl;
        return;
    }
    int64_t enqueue(T const x)
    {
        if (full())
        {
            std::cout << "queue is full, enqueue failed" << std::endl;
            return -1;
        }
        data[tail] = x;
        if (length == tail + 1)
        {
            tail = 0;
        }
        else
        {
            ++tail;
        }
        return 0;
    }
    int64_t dequeue(T& x)
    {
    	if (empty())
    	{
    		return -1;
    	}
        x = data[head];
        if (length - 1 == head)
        {
            head = 0;
        }
        else
        {
            ++head;
        }
        return 0;
    }
    void clear()
    {
        while (!empty())
        {
        	T x;
            dequeue(x);
        }
    }
private:
    int     head;   // points to the first element of the queue, the element has already been in queue
    int     tail;   // points to the next coming element
    T*      data;
    int64_t const length; // the queue can hold at most length-1 element
};

#endif /* IA_10_1QUEUE_H_ */

 

/*
 * IA_10.1queue.cpp
 *
 *  Created on: Feb 11, 2015
 *      Author: sunyj
 */
#include "IA_10.1queue.h"

int main()
{
    queue<int> q(4);
    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3); // after enqueue 3, the queue is full
    q.enqueue(4); // enqueue failed, because the queue is full
    q.print();
    int x;
    if (0 == q.dequeue(x))
    {
        std::cout << x << std::endl;
    }
    if (0 == q.dequeue(x))
    {
        std::cout << x << std::endl;
    }
    q.print();
    q.enqueue(4);
    q.enqueue(5);
    q.enqueue(6); // enqueue failed, because the queue is full
    q.print();    // elements 3, 4, 5
    q.clear();
    q.print();
    return 0;
}
 
 
 

 

7f48157d1119f39f73137f82d808e0447fe0ac6eaf14af9dafaf10cc1f9bb13008bb90dccaa8e1702a1460b1814ba9969a504fc2d5628535ee519d2992ef76c6a7ef6300

9a504fc2d5628535f161962092ef76c6a7ef6329

6a3ae8781655f453cb34444f52dffd7a

转载于:https://www.cnblogs.com/sunyongjie1984/p/4286603.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值