Leaked source code of windows server 2003
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.

138 lines
3.4 KiB

  1. //------------------------------------------------------------------------------
  2. template<class T>
  3. KList<T>::KList(){
  4. count=0;
  5. head=NULL;
  6. tail=NULL;
  7. return;
  8. };
  9. //------------------------------------------------------------------------------
  10. template<class T>
  11. KList<T>::~KList(){
  12. empty();
  13. return;
  14. };
  15. //------------------------------------------------------------------------------
  16. template<class T>
  17. void KList<T>::empty(){
  18. Node* n=head;
  19. while(n!=NULL){
  20. Node* nextN=n->next;
  21. delete n;
  22. n=nextN;
  23. };
  24. head=NULL;
  25. tail=NULL;
  26. count=0;
  27. };
  28. //------------------------------------------------------------------------------
  29. template<class T>
  30. bool KList<T>::addTail(const T& t){
  31. Node* n=new Node;
  32. if(n==NULL){
  33. _DbgPrintF(DEBUGLVL_VERBOSE,("KList: out of memory]"));
  34. return false;
  35. };
  36. n->next=n->last=NULL;
  37. n->Obj=t;
  38. if(tail!=NULL){
  39. ASSERT(tail->next==NULL);
  40. n->last=tail;
  41. tail->next=n;
  42. tail=n;
  43. } else {
  44. head=n;
  45. tail=n;
  46. };
  47. count++;
  48. return true;;
  49. };
  50. //------------------------------------------------------------------------------
  51. template<class T>
  52. bool KList<T>::addHead(const T& t){
  53. Node* n=new Node;
  54. if(n==NULL){
  55. _DbgPrintF(DEBUGLVL_VERBOSE,("KList: out of memory]"));
  56. return false;
  57. };
  58. n->next=n->last=NULL;
  59. n->Obj=t;
  60. if(head!=NULL){
  61. assert(head->last==NULL);
  62. n->next=head;
  63. head->last=n;
  64. head=n;
  65. } else {
  66. head=n;
  67. tail=n;
  68. };
  69. count++;
  70. return true;
  71. };
  72. //------------------------------------------------------------------------------
  73. template<class T>
  74. T& KList<T>::getHead() const {
  75. ASSERT(count!=0);
  76. return head->Obj;
  77. };
  78. //------------------------------------------------------------------------------
  79. template<class T>
  80. T& KList<T>::getTail() const {
  81. ASSERT(count!=0);
  82. return tail->Obj;
  83. };
  84. //------------------------------------------------------------------------------
  85. template<class T>
  86. POS KList<T>::getHeadPosition() const{
  87. return (POS) head;
  88. };
  89. //------------------------------------------------------------------------------
  90. template<class T>
  91. T& KList<T>::getAt(POS& P){
  92. ASSERT(count!=0);
  93. return ((Node*)P)->Obj;
  94. };
  95. //------------------------------------------------------------------------------
  96. template<class T>
  97. T& KList<T>::getNext(POS& P){
  98. ASSERT(count!=0);
  99. T& ret=((Node*)P)->Obj;
  100. P = (POS)((Node*)P)->next;
  101. return ret;
  102. };
  103. //------------------------------------------------------------------------------
  104. template<class T>
  105. void KList<T>::removeHead(){
  106. ASSERT(count>=1);
  107. Node* oldHead=head;
  108. head=head->next;
  109. head->last=NULL;
  110. delete oldHead;
  111. count--;
  112. };
  113. //------------------------------------------------------------------------------
  114. template<class T>
  115. void KList<T>::removeTail(){
  116. ASSERT(count>=1);
  117. Node* oldTail=tail;
  118. tail=tail->last;
  119. tail->next=NULL;
  120. delete oldTail;
  121. count--;
  122. };
  123. //------------------------------------------------------------------------------
  124. template<class T>
  125. void KList<T>::removeAt(POS& P){
  126. ASSERT(count>=1);
  127. Node* theNode= (Node*)P;
  128. Node* lastNode=theNode->last;
  129. Node* nextNode=theNode->next;
  130. delete theNode;
  131. if(head==theNode){head=nextNode;if(nextNode!=NULL)nextNode->last=NULL;}
  132. if(tail==theNode){tail=lastNode;if(lastNode!=NULL)lastNode->next=NULL;};
  133. if(lastNode!=NULL)lastNode->next=nextNode;
  134. if(nextNode!=NULL)nextNode->last=lastNode;
  135. count--;
  136. return;
  137. };
  138. //------------------------------------------------------------------------------