1 #include 2 3 4 using namespace std; 5 6 typedef struct _NODE_ 7 { 8 int a; 9 _NODE_* pNext; 10 11 }Node,*pNode; 12 13 14 class CList 15 { 16 17 private: 18 pNode m_pHead; 19 pNode m_pTail; 20 int iNodeCount; 21 22 public: 23 CList() 24 { 25 m_pHead = m_pTail = NULL; 26 iNodeCount = 0; 27 } 28 ~CList() 29 { 30 31 } 32 void InitList(); 33 pNode CreateNode(int a); 34 bool LinkNode(pNode pNodeTemp); 35 bool InsertNodeByIndex(pNode pNodeTemp,int iIndex); 36 bool DeleteNode(int iIndex); 37 void DestroyList(); 38 39 void TravelList() 40 { 41 if(m_pHead == NULL && iNodeCount == 0) 42 { 43 cout<<"List is Empty."< a< pNext; 52 } 53 cout< a = a; 78 pNodeTemp->pNext = NULL; 79 80 return pNodeTemp; 81 } 82 return false; 83 } 84 85 bool CList::LinkNode(pNode pNodeTemp) 86 { 87 if(m_pHead == NULL) 88 { 89 m_pHead = m_pTail = pNodeTemp; 90 } 91 else 92 { 93 m_pTail->pNext = pNodeTemp; 94 m_pTail = pNodeTemp; 95 } 96 97 iNodeCount++; 98 99 return true;100 }101 102 bool CList::InsertNodeByIndex(pNode pNodeTemp,int iIndex)103 {104 if(iIndex >=1 && iIndex <= iNodeCount)105 {106 pNode pNodeInsert = m_pHead;107 108 for(int i=1;i pNext;111 }112 113 pNodeTemp->pNext = pNodeInsert->pNext;114 pNodeInsert->pNext = pNodeTemp;115 116 iNodeCount++;117 return true;118 }119 120 return false;121 }122 bool CList::DeleteNode(int iIndex)123 {124 pNode pNodeDel = m_pHead;125 for(int i=1;i pNext;128 }129 130 pNodeDel->pNext = pNodeDel->pNext->pNext;131 132 iNodeCount--;133 if(iNodeCount == 0)134 {135 m_pHead = m_pTail = NULL;136 }137 138 return true;139 }140 141 void CList::DestroyList()142 {143 pNode pNodeDel = m_pHead;144 145 while(pNodeDel != NULL)146 {147 m_pHead = m_pHead->pNext;148 149 delete pNodeDel;150 151 pNodeDel = m_pHead;152 153 iNodeCount--;154 }155 156 m_pHead = m_pTail = NULL;157 158 iNodeCount = 0;159 }160 161 162 163 164 165 166 int main()167 {168 169 CList CListObj;170 171 pNode pNodeTemp = { 0};172 173 int i = 0;174 int a = 0;175 int iIndex = 0;176 cout<<"Test."< >a;181 pNode pNodeTemp = CListObj.CreateNode(a);182 CListObj.LinkNode(pNodeTemp);183 }184 CListObj.TravelList();185 186 cout<<"Input a Index to Delete."< >iIndex;188 189 CListObj.DeleteNode(iIndex);190 191 CListObj.TravelList();192 193 cout<<"Input Data and Index to Inesert."< >a;196 cout<<"Index:"< >iIndex;198 pNodeTemp = CListObj.CreateNode(a);199 200 CListObj.InsertNodeByIndex(pNodeTemp,iIndex);201 202 203 CListObj.TravelList();204 205 206 cout<