Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
1.3 KiB

  1. /*************************************************
  2. * queue.cpp *
  3. * *
  4. * Copyright (C) 1995-1999 Microsoft Inc. *
  5. * *
  6. *************************************************/
  7. #include "stdafx.h"
  8. #include "queue.h"
  9. CQueue::CQueue(int nSize)
  10. {
  11. m_pQueue = new int[nSize+1];
  12. m_nSize = nSize+1;
  13. m_nFront = 0;
  14. m_nRear = 0;
  15. }
  16. CQueue::~CQueue()
  17. {
  18. delete[] m_pQueue;
  19. }
  20. int CQueue::Peek()
  21. {
  22. if (m_nFront == m_nRear)
  23. return -1;
  24. else
  25. return m_pQueue[m_nRear];
  26. }
  27. int CQueue::Get()
  28. {
  29. if (m_nFront == m_nRear)
  30. return -1;
  31. else
  32. {
  33. int nTmp = m_pQueue[m_nRear];
  34. m_nRear = Inc(m_nRear);
  35. return nTmp;
  36. }
  37. }
  38. BOOL CQueue::Add(int data)
  39. {
  40. if (Inc(m_nFront) == m_nRear)
  41. return FALSE;
  42. else
  43. {
  44. m_pQueue[m_nFront] = data;
  45. m_nFront = Inc(m_nFront);
  46. return TRUE;
  47. }
  48. }
  49. int CQueue::Inc(int x)
  50. {
  51. if ((x+1) < m_nSize)
  52. x++;
  53. else
  54. x = 0;
  55. return x;
  56. }
  57. int CQueue::Dec(int x)
  58. {
  59. if ((x-1) >= 0)
  60. x--;
  61. else
  62. x = m_nSize - 1;
  63. return x;
  64. }
  65. BOOL CQueue::IsEmpty()
  66. {
  67. return m_nRear == m_nFront;
  68. }
  69. void CQueue::Dump()
  70. {
  71. int pf = m_nFront;
  72. int pr = m_nRear;
  73. if (pf == pr) return;
  74. for (; pf != pr; )
  75. {
  76. TRACE("[%d]%d,",pr,m_pQueue[pr]);
  77. pr = Inc(pr);
  78. }
  79. }