/* File: main5.C Using the C++ list ADT */ #include #include #include #include "list5.h" main() { List *DinnerL, *BabyL, *ShoppingL ; List *TempL1, *TempL2 ; char *tempstr ; position pos ; data x ; printf("Make new dinner list\n") ; DinnerL = new List ; assert(DinnerL != NULL) ; DinnerL->Print() ; printf("\nAdd spaghetti & sauce to list\n") ; DinnerL->Append("Spagetti") ; DinnerL->Print() ; DinnerL->Append("Sauce") ; DinnerL->Print() ; printf("\nAdd bread\n") ; DinnerL->Append("Bread") ; DinnerL->Print() ; printf("\nAdd ground beef for meatballs\n") ; DinnerL->Append("Beef") ; DinnerL->Print() ; printf("\nDon't forget the garlic!\n") ; DinnerL->Prepend("Garlic") ; DinnerL->Print() ; printf("\nMake baby list\n") ; BabyL = new List ; assert(BabyL != NULL) ; BabyL->Prepend("Pampers#3") ; BabyL->Append("Wipes") ; BabyL->Append("Peaches") ; BabyL->Print() ; printf("\nCopy and join lists\n") ; TempL1 = BabyL->Duplicate() ; TempL1->Print() ; TempL2 = DinnerL->Duplicate() ; TempL2->Print() ; TempL1->Concatenate(TempL2) ; ShoppingL = TempL1 ; printf("ShoppingL: ") ; ShoppingL->Print() ; printf("BabyL: ") ; BabyL->Print() ; printf("DinnerL: ") ; DinnerL->Print() ; delete DinnerL ; delete BabyL ; printf("\nRemove garlic\n") ; pos = ShoppingL->Locate("Garlic") ; ShoppingL->Delete(pos) ; ShoppingL->Print() ; printf("\nGone Shopping... sauce & parmesan are on sale... buy some?\n") ; if (ShoppingL->IsMember("Sauce")) { printf(" we need sauce\n") ; } else { printf(" we don't need sauce\n") ; } if (ShoppingL->IsMember("Parmesan")) { printf(" we need parmesan\n") ; } else { printf(" we don't need parmesan\n") ; } printf("\nSome stats\n") ; /* Previous version leaked memory */ tempstr = ShoppingL->FirstItem() ; printf("First Item: %s\n", tempstr) ; free(tempstr) ; tempstr = ShoppingL->LastItem() ; printf("Last Item: %s\n", tempstr) ; free(tempstr) ; printf("# of items: %d\n", ShoppingL->Count() ) ; pos = ShoppingL->FirstPos() ; printf("[ ") ; while (1) { x = ShoppingL->ItemAt(pos) ; if (x == NULL) break ; printf("\"%s\" ", x ) ; pos = ShoppingL->NextPos(pos) ; } printf("]\n") ; }