主要是空队列和满队列的定义。空队列、满队列的定义没有死板的规定,这取决于开发者的定义。
代码里的规则:
空队列:头指针等于尾指针。
满队列:尾指针加1后等于头队列。
queue.h
#pragma once#include <Windows.h>#define QUEUE_VOLUME_LIMIT 0X1000#pragma pack(1)typedef struct {int head;int tail;LPVOID e[QUEUE_VOLUME_LIMIT];
}CircleQueue;#pragma pack()class QueueClass {public:QueueClass();~QueueClass();CircleQueue * m_queue = 0;int Enqueue(LPVOID v);int Dequeue(LPVOID* v);int Size();
};
queue.cpp
#include "queue.h"QueueClass::QueueClass() {if (m_queue == 0) {m_queue = new CircleQueue();m_queue->head = 0;m_queue->tail = 0;}
}QueueClass::~QueueClass() {if (m_queue) {delete m_queue;}
}int QueueClass::Dequeue(LPVOID * v) {if (m_queue->tail == m_queue->head){return 0;}*v = m_queue->e[m_queue->head];m_queue->head ++;if (m_queue->head >= QUEUE_VOLUME_LIMIT) {m_queue->head = 0;}return TRUE;
}int QueueClass::Enqueue(LPVOID v) {if (m_queue->tail + 1 == QUEUE_VOLUME_LIMIT) {if ( m_queue->head == 0) {return 0;}else {m_queue->e[m_queue->tail] = v;m_queue->tail = 0;}}else {if (m_queue->tail + 1 == m_queue->head) {return 0;}else {m_queue->e[m_queue->tail] = v;m_queue->tail++;}}return TRUE;
}int QueueClass::Size() {if (m_queue->tail > m_queue->head) {return m_queue->tail - m_queue->head;}else {return QUEUE_VOLUME_LIMIT - (m_queue->head - m_queue->tail);}
}