算法导论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

posted @ 2015-02-11 17:04  孙永杰  阅读(217)  评论(0编辑  收藏  举报