标签:blog io ar os sp on 2014 log bs
myQuene.h
#pragma once typedef int TYPE; class myQueue { TYPE* m_pData; int m_nCount; int m_nHead,m_nTail; public: myQueue(int nCount = 4) { m_nCount = nCount; m_pData = new TYPE[nCount];//注意这里不是小括号,小扩号是调用构造函数 m_nHead = m_nTail = 0; } ~myQueue(void) { delete []m_pData; } bool isEmpty() { return m_nHead == m_nTail; } bool isFull()//这里是难点 { return (m_nTail+1)%m_nCount == m_nHead; //尾巴 加1 对总长度取 余数 如果与 头部 相等,则队列满,队列要预留出一个空间来判断是否满队列 } void push(const TYPE& t) { if (isFull()) { return; } m_pData[m_nTail++] = t; m_nTail %= m_nCount; //如果 尾巴 到了最后让他直接跑到 对头 } bool pop(TYPE& t) { if (isEmpty()) { return false; } t = m_pData[m_nHead++]; m_nTail %= m_nCount; return true; } };
main.h
// Queue.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "myQueue.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { myQueue q; int i = 0; while (i< 5) { q.push(i++); } TYPE t; q.pop(t); q.pop(t); q.pop(t); while (q.pop(t)) { cout<<t<<endl; } getchar(); return 0; }
标签:blog io ar os sp on 2014 log bs
原文地址:http://blog.csdn.net/djb100316878/article/details/41777009