【线性表】 LineTable
1 #include<iostream> 2 #include<windows.h> 3 4 5 using namespace std; 6 7 8 #define MAX 3 9 10 11 typedef struct _DATA_ 12 { 13 int iAge; 14 char szName[20]; 15 16 17 }Data,*pData; 18 19 20 typedef struct _LINETABLE_ 21 { 22 23 pData pDataTemp; 24 25 int iMax; 26 int iSize; 27 }LineTable, *pLineTable; 28 29 30 int Compare(LineTable LineTableTemp, Data DataTemp); 31 32 class CLineTable 33 { 34 35 public: 36 CLineTable() 37 { 38 39 } 40 ~CLineTable() 41 { 42 43 } 44 45 46 bool IsExist(LineTable& LineTableTemp); 47 void InputData(LineTable& LineTableTemp,Data DataTemp) 48 { 49 50 LineTableTemp.pDataTemp[LineTableTemp.iSize] = DataTemp; 51 52 LineTableTemp.iSize++; 53 } 54 bool InitLineTable(LineTable& LineTableTemp); 55 void DestroyLineTable(LineTable& LineTableTemp); 56 bool ClearLineTable(LineTable& LineTableTemp); 57 bool IsEmpty(LineTable& LineTableTemp); 58 bool GetLength(LineTable& LineTableTemp, DWORD& dwLength); 59 bool GetElement(LineTable& LineTableTemp, pData pDataTemp, DWORD dwIndex); 60 bool LocateElement(LineTable& LineTableTemp, Data DataTemp, 61 int& iIndex, int Compare(LineTable,Data)); 62 bool PreElement(LineTable& LineTableTemp, Data DataTemp, 63 Data& DataPre, int Compare(LineTable LineTableTemp, Data DataTemp)); 64 65 bool NextElement(LineTable& LineTableTemp, Data DataTemp, 66 Data& DataNext, int Compare(LineTable, Data)); 67 68 bool TravelLineTable(LineTable& LineTableTemp) 69 { 70 if(IsExist(LineTableTemp)) 71 { 72 int i = 0; 73 for(i=0;i<LineTableTemp.iSize;i++) 74 { 75 cout<<LineTableTemp.pDataTemp[i].szName<<" " 76 <<LineTableTemp.pDataTemp[i].iAge<<endl; 77 } 78 return true; 79 } 80 81 return false; 82 } 83 84 bool InsertData(LineTable& LineTableTemp, 85 Data DataTemp, Data DataNew); 86 87 bool DeleteData(LineTable& LineTableTemp, 88 Data DataTemp, Data& DataDel); 89 }; 90 91 92 93 94 bool CLineTable::IsExist(LineTable& LineTableTemp) 95 { 96 if(LineTableTemp.pDataTemp != NULL) 97 { 98 return true; 99 } 100 101 return false; 102 } 103 104 105 106 bool CLineTable::InitLineTable(LineTable& LineTableTemp) 107 { 108 109 LineTableTemp.iMax = MAX; 110 111 LineTableTemp.pDataTemp = new Data[LineTableTemp.iMax]; 112 113 if(LineTableTemp.pDataTemp == NULL) 114 { 115 return false; 116 } 117 118 memset(LineTableTemp.pDataTemp,0, 119 sizeof(Data)*LineTableTemp.iMax); 120 121 LineTableTemp.iSize = 0; 122 123 124 return true; 125 } 126 127 128 void CLineTable::DestroyLineTable(LineTable& LineTableTemp) 129 { 130 131 if(LineTableTemp.pDataTemp != NULL) 132 { 133 delete LineTableTemp.pDataTemp; 134 } 135 LineTableTemp.iMax = 0; 136 LineTableTemp.iSize = 0; 137 LineTableTemp.pDataTemp = NULL; //这个一定要赋为空 138 } 139 140 141 142 bool CLineTable::ClearLineTable(LineTable& LineTableTemp) 143 { 144 if(IsExist(LineTableTemp)) 145 { 146 memset(LineTableTemp.pDataTemp,0, 147 sizeof(Data)*LineTableTemp.iSize); 148 149 150 LineTableTemp.iSize = 0; 151 152 return true; 153 } 154 155 156 157 return false; 158 } 159 160 161 bool CLineTable::IsEmpty(LineTable& LineTableTemp) 162 { 163 if(IsExist(LineTableTemp)) 164 { 165 if(LineTableTemp.iSize == 0) 166 { 167 return true; 168 } 169 } 170 171 return false; 172 } 173 174 175 bool CLineTable::GetLength(LineTable& LineTableTemp, DWORD& dwLength) 176 { 177 if(IsExist(LineTableTemp)) 178 { 179 dwLength = LineTableTemp.iSize; 180 181 return true; 182 } 183 184 return false; 185 } 186 187 188 189 190 191 192 bool CLineTable::GetElement(LineTable& LineTableTemp, pData pDataTemp, DWORD dwIndex) 193 { 194 if(IsExist(LineTableTemp) && 1<=dwIndex 195 && dwIndex<=LineTableTemp.iSize) 196 { 197 *pDataTemp = LineTableTemp.pDataTemp[--dwIndex]; 198 199 return true; 200 } 201 202 return false; 203 } 204 205 bool CLineTable::LocateElement(LineTable& LineTableTemp, 206 Data DataTemp, 207 int& iIndex, 208 int Compare(LineTable,Data)) 209 { 210 211 if(IsExist(LineTableTemp)) 212 { 213 iIndex = Compare(LineTableTemp, DataTemp); 214 215 if(iIndex == -1) 216 { 217 return false; 218 } 219 220 return true; 221 } 222 223 return false; 224 } 225 226 227 bool CLineTable::PreElement(LineTable& LineTableTemp, Data DataTemp, 228 Data& DataPre, int Compare(LineTable, Data)) 229 { 230 int iIndex = 0; 231 232 if(IsExist(LineTableTemp)) 233 { 234 if(LocateElement(LineTableTemp,DataTemp,iIndex,Compare)) 235 { 236 iIndex--; 237 if(iIndex>0) 238 { 239 DataPre = LineTableTemp.pDataTemp[iIndex-1]; 240 241 return true; 242 } 243 } 244 245 cout<<"No Precursor."<<endl; 246 return false; 247 } 248 return false; 249 } 250 251 252 bool CLineTable::NextElement(LineTable& LineTableTemp, Data DataTemp, 253 Data& DataNext, int Compare(LineTable, Data)) 254 { 255 int iIndex = 0; 256 257 if(IsExist(LineTableTemp)) 258 { 259 if(LocateElement(LineTableTemp,DataTemp,iIndex,Compare)) 260 { 261 iIndex--; 262 if(iIndex<LineTableTemp.iSize-1) 263 { 264 DataNext = LineTableTemp.pDataTemp[iIndex+1]; 265 266 return true; 267 } 268 } 269 270 cout<<"No Successor."<<endl; 271 return false; 272 } 273 return false; 274 275 276 } 277 278 279 bool CLineTable::InsertData(LineTable& LineTableTemp, 280 Data DataTemp, Data DataNew) 281 { 282 int i = 0; 283 int iIndex = 0; 284 285 if(LineTableTemp.iSize>=LineTableTemp.iMax) 286 { 287 LineTableTemp.pDataTemp = 288 (pData)realloc(LineTableTemp.pDataTemp, 289 sizeof(Data)*(LineTableTemp.iMax+MAX)); 290 291 292 cout<<endl<<"Allocate"<<endl; 293 294 if(LineTableTemp.pDataTemp == NULL) 295 { 296 return false; 297 } 298 else 299 { 300 LineTableTemp.iMax+=MAX; 301 } 302 303 } 304 305 if(LocateElement(LineTableTemp, DataTemp, iIndex, Compare)) 306 { 307 iIndex--; 308 for(i=LineTableTemp.iSize-1;i>=iIndex;i--) 309 { 310 LineTableTemp.pDataTemp[i+1] = 311 LineTableTemp.pDataTemp[i]; 312 } 313 314 315 LineTableTemp.pDataTemp[iIndex] = DataNew; 316 317 LineTableTemp.iSize++; 318 319 return true; 320 } 321 } 322 323 324 325 bool CLineTable::DeleteData(LineTable& LineTableTemp, 326 Data DataTemp, Data& DataDel) 327 { 328 int i = 0; 329 int iIndex = 0; 330 if(LocateElement(LineTableTemp, DataTemp, iIndex, Compare)) 331 { 332 iIndex--; 333 DataDel = LineTableTemp.pDataTemp[iIndex]; 334 335 for(i=iIndex;i<LineTableTemp.iSize;i++) 336 { 337 LineTableTemp.pDataTemp[i] = 338 LineTableTemp.pDataTemp[i+1]; 339 } 340 341 LineTableTemp.iSize--; 342 343 return true; 344 } 345 } 346 347 348 int main() 349 { 350 351 CLineTable CLineTableObject; 352 353 LineTable LineTableTemp = {0}; 354 355 DWORD dwLength = 0; 356 357 CLineTableObject.InitLineTable(LineTableTemp); 358 359 360 Data DataTemp = {0}; 361 362 int iIndex = 0; 363 Data DataNew = {0}; 364 Data DataDel = {0}; 365 int i = 0; 366 for(i=0;i<3;i++) 367 { 368 cout<<"Input Name and Age:"<<endl; 369 370 cin>>DataTemp.szName; 371 cin>>DataTemp.iAge; 372 373 CLineTableObject.InputData(LineTableTemp,DataTemp); 374 } 375 if (!CLineTableObject.IsEmpty(LineTableTemp)) 376 { 377 cout<<endl<<"LineTable have Data."<<endl; 378 } 379 380 381 382 if (CLineTableObject.GetLength(LineTableTemp,dwLength)) 383 { 384 cout<<"LineTable's Size: "<<dwLength<<endl<<endl; 385 } 386 387 388 cout<<"Input Index:"<<endl; 389 390 391 cin>>iIndex; 392 393 394 if(CLineTableObject.GetElement(LineTableTemp,&DataNew, 395 iIndex)) 396 { 397 398 cout<<DataNew.szName<<" "<<DataNew.iAge<<endl; 399 } 400 else 401 { 402 cout<<"Position Error."<<endl; 403 } 404 405 406 cout<<endl<<"Input Data To Find:"<<endl; 407 408 cin>>DataTemp.szName; 409 cin>>DataTemp.iAge; 410 411 if (CLineTableObject.LocateElement(LineTableTemp,DataTemp, 412 iIndex ,Compare)) 413 { 414 415 if (iIndex==-1) 416 { 417 cout<<"No Person"<<endl; 418 } 419 cout<<"Index:"<<iIndex<<endl; 420 } 421 else 422 { 423 cout<<"No Data."<<endl; 424 } 425 426 cout<<endl<<"Input Data To Get Pre:"<<endl; 427 428 cin>>DataTemp.szName; 429 cin>>DataTemp.iAge; 430 431 432 if (CLineTableObject.PreElement(LineTableTemp, 433 DataTemp,DataNew,Compare)) 434 { 435 436 cout<<DataNew.szName<<" "<<DataNew.iAge<<endl; 437 438 } 439 440 441 cout<<endl<<"Input Data To Get Next:"<<endl; 442 443 cin>>DataTemp.szName; 444 cin>>DataTemp.iAge; 445 446 447 if (CLineTableObject.NextElement(LineTableTemp, 448 DataTemp,DataNew,Compare)) 449 { 450 451 cout<<DataNew.szName<<" "<<DataNew.iAge<<endl; 452 453 } 454 455 cout<<"Input Person Position to Insert:"<<endl; 456 457 cin>>DataTemp.szName; 458 cin>>DataTemp.iAge; 459 460 cout<<"Input New Data"<<endl; 461 462 cin>>DataNew.szName; 463 cin>>DataNew.iAge; 464 465 if(CLineTableObject.InsertData(LineTableTemp, 466 DataTemp, DataNew)) 467 { 468 CLineTableObject.TravelLineTable(LineTableTemp); 469 470 } 471 472 cout<<"Input Person Position to Delete:"<<endl; 473 474 cin>>DataTemp.szName; 475 cin>>DataTemp.iAge; 476 477 if(CLineTableObject.DeleteData(LineTableTemp, 478 DataTemp, DataDel)) 479 { 480 cout<<endl<<DataDel.szName 481 <<" "<<DataDel.iAge<<" is Deleted."<<endl<<endl; 482 483 CLineTableObject.TravelLineTable(LineTableTemp); 484 485 486 } 487 return 0; 488 } 489 490 491 492 493 int Compare(LineTable LineTableTemp, Data DataTemp) 494 { 495 int i = 0; 496 for(i=0;i<LineTableTemp.iSize;i++) 497 { 498 if(strcmp(LineTableTemp.pDataTemp[i].szName,DataTemp.szName) == 0 499 && LineTableTemp.pDataTemp[i].iAge == DataTemp.iAge) 500 { 501 502 return i+1; 503 } 504 505 } 506 507 return -1; 508 }