00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00051
00052 #if !defined CDLIST_H
00053 #define CDLIST_H
00054
00055 #include <stdio.h>
00056 #include "datatype.h"
00057
00058 template <class CItem, int iMaxItemNum>
00059 class CDList
00060 {
00061
00062 public:
00063 CDList(void);
00064 ~CDList();
00065
00066
00067 protected:
00068 int iHeadPointer;
00069 int iTailPointer;
00070 int iFreePointer;
00071 CItem *pItemList;
00072 int *piPrevious;
00073 int *piNext;
00074 int iCurPointer;
00075
00076
00077 public:
00078 int iStatus;
00079 void AddHead(const CItem &NewItem);
00080 void AddTail(const CItem &NewItem);
00081 CItem RemoveHead();
00082 CItem RemoveTail();
00083 CItem ShowHead() {return pItemList[iHeadPointer];};
00084 CItem ShowTail() {return pItemList[iTailPointer];};
00085 bool IsEmpty() { return (iHeadPointer==iFreePointer)? true:false;};
00086 int GetItemNum();
00087 void RemoveAll();
00088
00089 void ToHead() {iCurPointer=iHeadPointer;};
00090 void ToTail() {iCurPointer=iTailPointer;};
00091 inline bool ToNext();
00092 inline bool ToPrevious();
00093 CItem ShowItem() {return pItemList[iCurPointer];};
00094 };
00095
00096
00097 template <class CItem, int iMaxItemNum>
00098 CDList<CItem, iMaxItemNum>::CDList(void)
00099 {
00100 pItemList=new CItem [iMaxItemNum];
00101 if (pItemList==NULL)
00102 {
00103 iStatus=MEM_ERROR;
00104 return;
00105 }
00106 piPrevious=new int [iMaxItemNum];
00107 if (piPrevious==NULL)
00108 {
00109 iStatus=MEM_ERROR;
00110 delete [] pItemList;
00111 return;
00112 }
00113 piNext=new int [iMaxItemNum];
00114 if (piNext==NULL)
00115 {
00116 iStatus=MEM_ERROR;
00117 delete [] piPrevious;
00118 delete [] pItemList;
00119 return;
00120 }
00121 iHeadPointer=iTailPointer=iFreePointer=0;
00122 for (int i=0; i<iMaxItemNum-1; i++)
00123 piNext[i]=i+1;
00124 piNext[iMaxItemNum-1]=0;
00125 iStatus=MDC_SUCCESS;
00126 }
00127
00128
00129 template <class CItem, int iMaxItemNum>
00130 CDList<CItem, iMaxItemNum>::~CDList()
00131 {
00132 if (pItemList!=NULL)
00133 delete [] pItemList;
00134 if (piPrevious!=NULL)
00135 delete [] piPrevious;
00136 if (piNext!=NULL)
00137 delete [] piNext;
00138 }
00139
00140
00141
00142 template <class CItem, int iMaxItemNum>
00143 void CDList<CItem, iMaxItemNum>::AddHead(const CItem &NewItem)
00144 {
00145 int iPosition;
00146
00147
00148 iPosition=iFreePointer;
00149 iFreePointer=piNext[iFreePointer];
00150
00151 pItemList[iPosition]=NewItem;
00152 piPrevious[iPosition]=iPosition;
00153 piNext[iPosition]=iHeadPointer;
00154
00155 piPrevious[iHeadPointer]=iPosition;
00156 iHeadPointer=iPosition;
00157 }
00158
00159 template <class CItem, int iMaxItemNum>
00160 void CDList<CItem, iMaxItemNum>::AddTail(const CItem &NewItem)
00161 {
00162 int iPosition;
00163
00164 iPosition=iFreePointer;
00165 iFreePointer=piNext[iFreePointer];
00166 pItemList[iPosition]=NewItem;
00167 piPrevious[iPosition]=iTailPointer;
00168 piNext[iPosition]=iPosition;
00169 piNext[iTailPointer]=iPosition;
00170 iTailPointer=iPosition;
00171 }
00172
00173 template <class CItem, int iMaxItemNum>
00174 CItem CDList<CItem, iMaxItemNum>::RemoveHead()
00175 {
00176 int iPosition;
00177
00178 iPosition=iHeadPointer;
00179 iHeadPointer=piNext[iPosition];
00180 piPrevious[iHeadPointer]=iHeadPointer;
00181 piNext[iPosition]=iFreePointer;
00182 iFreePointer=iPosition;
00183 return pItemList[iPosition];
00184 }
00185
00186 template <class CItem, int iMaxItemNum>
00187 CItem CDList<CItem, iMaxItemNum>::RemoveTail()
00188 {
00189 int iPosition;
00190
00191 iPosition=iTailPointer;
00192 iTailPointer=piPrevious[iPosition];
00193 piNext[iTailPointer]=iTailPointer;
00194 piNext[iPosition]=iFreePointer;
00195 iFreePointer=iPosition;
00196 return pItemList[iPosition];
00197 }
00198
00199 template <class CItem, int iMaxItemNum>
00200 int CDList<CItem, iMaxItemNum>::GetItemNum()
00201 {
00202 int iPosition;
00203 int iCount;
00204
00205 if (IsEmpty())
00206 return 0;
00207 iPosition=iHeadPointer;
00208 for (iCount=1; iCount<=iMaxItemNum+1; iCount++)
00209 {
00210 if (piNext[iPosition]==iPosition)
00211 break;
00212 iPosition=piNext[iPosition];
00213 }
00214 return iCount;
00215 }
00216
00217 template <class CItem, int iMaxItemNum>
00218 void CDList<CItem, iMaxItemNum>::RemoveAll()
00219 {
00220 int iCount;
00221
00222 iHeadPointer=iTailPointer=iFreePointer=0;
00223 for (iCount=0; iCount<iMaxItemNum-1; iCount++)
00224 piNext[iCount]=iCount+1;
00225 piNext[iMaxItemNum-1]=0;
00226 }
00227
00228 template <class CItem, int iMaxItemNum>
00229 inline bool CDList<CItem, iMaxItemNum>::ToNext()
00230 {
00231 if (piNext[iCurPointer]==iCurPointer)
00232 return false;
00233 iCurPointer=piNext[iCurPointer];
00234 return true;
00235 };
00236
00237 template <class CItem, int iMaxItemNum>
00238 inline bool CDList<CItem, iMaxItemNum>::ToPrevious()
00239 {
00240 if (piPrevious[iCurPointer]==iCurPointer)
00241 return false;
00242 iCurPointer=piPrevious[iCurPointer];
00243 return true;
00244 };
00245
00246
00247 #endif // CDLIST_H