/* File: main6.C

   Using the C++ list ADT
*/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "list6.h"

main() {
   List *DinnerL, *BabyL, *ShoppingL ;
   List *TempL1, *TempL2 ;
   position pos ;
   ListItem *iptr ;

   printf("Make new dinner list\n") ;
   DinnerL = new List ;
   assert(DinnerL != NULL) ;
   DinnerL->Print() ;

   printf("\nAdd spaghetti &  sauce to list\n") ;
   DinnerL->Append(ListItem("Spagetti")) ;
   DinnerL->Print() ;
   DinnerL->Append(ListItem("Sauce")) ;
   DinnerL->Print() ;

   printf("\nAdd bread\n") ;
   DinnerL->Append(ListItem("Bread")) ;
   DinnerL->Print() ;

   printf("\nAdd ground beef for meatballs\n") ;
   DinnerL->Append(ListItem("Beef")) ;
   DinnerL->Print() ;

   printf("\nDon't forget the garlic!\n") ;
   DinnerL->Prepend(ListItem("Garlic")) ;
   DinnerL->Print() ;

   printf("\nMake baby list\n") ;
   BabyL = new List ;
   assert(BabyL != NULL) ;
   BabyL->Prepend(ListItem("Pampers#3")) ;
   BabyL->Append(ListItem("Wipes")) ;
   BabyL->Append(ListItem("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(ListItem("Garlic")) ;
   ShoppingL->Delete(pos) ;
   ShoppingL->Print() ;

   printf("\nGone Shopping... sauce & parmesan are on sale... buy some?\n") ;
   if (ShoppingL->IsMember(ListItem("Sauce"))) {
      printf("   we need sauce\n") ;
   } else {
      printf("   we don't need sauce\n") ;
   }
   if (ShoppingL->IsMember(ListItem("Parmesan"))) {
      printf("   we need parmesan\n") ;
   } else {
      printf("   we don't need parmesan\n") ;
   }

   printf("\nSome stats\n") ;
   /* Previous version leaked memory */

   iptr = ShoppingL->FirstItem() ;
   printf("First Item: ") ;
   iptr->print() ;
   printf("\n") ;
   delete iptr ;

   iptr = ShoppingL->LastItem()  ;
   printf("Last Item: ") ;
   iptr->print() ;
   printf("\n") ;
   delete iptr ;

   printf("# of items: %d\n", ShoppingL->Count() ) ;


   pos  = ShoppingL->FirstPos() ;
   printf("[ ") ;
   while (1) {
      iptr = ShoppingL->ItemAt(pos) ;
      if (iptr == NULL) break ;
	  iptr->print() ;
	  delete iptr ;
	  printf(", ") ;
      pos = ShoppingL->NextPos(pos) ;
   }
   printf("]\n") ;

}
