List.cpp
上传用户:wszmarenbt
上传日期:2013-04-26
资源大小:2552k
文件大小:2k
源码类别:

Windows编程

开发平台:

Visual C++

  1. //THE IMPLEMENTATION OF THE CLASS "LIST"
  2. //FILE LIST.CPP
  3. #include "List.h"
  4. void LIST::Build_Forward(int Data)
  5. {
  6. NODE *BUFFER;
  7. BUFFER=new NODE;
  8. BUFFER->DATA=Data;
  9. if (HEAD)
  10. {
  11. BUFFER->NEXT=HEAD;
  12. HEAD=BUFFER;
  13. }
  14. else
  15. {
  16. BUFFER->NEXT=0;
  17. HEAD=BUFFER;
  18. }
  19. }
  20. void LIST::Build_Backward(int Data)
  21. {
  22. NODE *PREVIOUS,*CURRENT,*NEWNODE;
  23. if (HEAD)
  24. {
  25. PREVIOUS=HEAD;
  26. CURRENT=HEAD->NEXT;
  27. while (CURRENT!=0)
  28. {
  29. PREVIOUS=CURRENT;
  30. CURRENT=CURRENT->NEXT;
  31. }
  32. NEWNODE=new NODE;
  33. NEWNODE->DATA=Data;
  34. NEWNODE->NEXT=0;
  35. PREVIOUS->NEXT=NEWNODE;
  36. }
  37. else
  38. {
  39. HEAD=new NODE;
  40. HEAD->DATA=Data;
  41. HEAD->NEXT=0;
  42. }
  43. }
  44. NODE* LIST::GET_HEAD()
  45. {
  46. return (HEAD);
  47. }
  48. int LIST::GET_NEXT(NODE * &TEMP)
  49. {
  50. if (HEAD==0)
  51. {
  52. printf("----Sorry!!!  It's EMPTY !!! ");
  53. return(NULL);
  54. }
  55. else
  56. {
  57. int BUFFER;
  58. BUFFER=TEMP->DATA;
  59. TEMP=TEMP->NEXT;
  60. return BUFFER;
  61. }
  62. }
  63. int LIST::GET_POSITION(int P)
  64. {
  65. NODE *CURRENT;
  66. int BUFFER=0;
  67. int COUNTER=1;
  68. if (HEAD)
  69. {
  70. CURRENT=HEAD;
  71. while ((CURRENT!=0)&&(COUNTER!=P)&&(P>=COUNTER))
  72. {
  73. CURRENT=CURRENT->NEXT;
  74. COUNTER++;
  75. }
  76. if(P<COUNTER)
  77. {
  78. printf("nSorry,can not find the position you gived.");
  79. printf("nThe position you gived is too small!");
  80. return(NULL);
  81. }
  82. else if(CURRENT==0)
  83. {
  84. printf("nSorry,can not find the position you gived.");
  85. printf("nThe position number is too BIG! ");
  86. return(NULL);
  87. }
  88. else //CURRENT!=0
  89. {
  90. BUFFER=CURRENT->DATA;
  91. return(BUFFER);
  92. }
  93. }
  94. else
  95. {
  96. printf("nSorry,can not find the position you gived.");
  97. printf("nThe list is EMPTY! ");
  98. return(NULL);
  99. }
  100. }
  101. void LIST::CLEAR()
  102. {
  103. NODE *TEMP_HEAD=HEAD;
  104. if (TEMP_HEAD==0) return;
  105. do
  106. {
  107. NODE *TEMP_NODE=TEMP_HEAD;
  108. TEMP_HEAD=TEMP_HEAD->NEXT;
  109. delete TEMP_NODE;
  110. }
  111. while (TEMP_HEAD!=0);
  112. }