#include #include "ItemInterface.h" #include "ListInterface.h" #include "Bool.h" #include "highlevel.h" int nodecounter=0; int moviecounter=0; int reviewcounter=0; DLnkNodePtr SrchPtr=NULL; /********************************************************************** * * Function: Insert * Input: DLnkNodePtr List - the list to insert into * char elem[35] - the filename to insert * * Returns: Boolean TRUE on success * * Description: * This function inserts a new node into the list provided using a modified * binary search algorithm. The search will locate where in the list the * item should be, and inserts into that point. This is a combination * insertion and sorting process. Due to the number of comparisons, it * can not be more efficient than a separate insertion and sort. * * Variables: DLnkNodePtr PrevPtr - the previous node in the list * DLnkNodePtr NewPtr - the node being inserted * DLnkNodePtr CopyPtr - used when necessary to point to a node * MovieNode tmp - used to set the item of the new node * Boolean Name - the flag for detection * int beginning - the beginning of the current search * int end - the end of the current search * int middle - the middle element in the current search * int dup - the number of times to jump an item in the list * * Called by: Add * VerifyVRS * VerifyMOV * VerifyMRS * * Calls: AllocateNewNode * strcpy * SetItem * SetNextLink * SetPrevLink * EqualTo * GreaterThan * LessThan * GetItem * * Author: Tim McGaughey * Revisions: none * Version: 1.0 * * **********************************************************************/ Boolean Insert(DLnkNodePtr& List, char elem[35], Boolean Review, int MovieI) { DLnkNodePtr PrevPtr, NewPtr, CopyPtr; MovieNode tmp; AllocateNewNode(NewPtr); if (NewPtr==NULL) return FALSE; else { //find out if the node we are adding is for a movie or a review if (Review) { NewPtr->Item.movieindex=MovieI; } else { moviecounter++; NewPtr->Item.movieindex=moviecounter; } nodecounter++; strcpy(tmp.filename,elem); SetItem(NewPtr, tmp); SetNextLink(NewPtr, NULL); SetPrevLink(NewPtr, NULL); CopyPtr=NewPtr; PrevPtr=NULL; Boolean Name=FALSE; int beginning=1; int end; int middle; int dup; end = nodecounter; if (List==NULL) { List=NewPtr; return TRUE; } while ((Name==FALSE) && (beginning <= end)) { middle=((end+beginning)/2); dup=middle-1; while (GetPrevLink(List))//->PrevLink!=NULL) { List=GetPrevLink(List);//->PrevLink; } //change 1 - here is the first change of the list linking stuff.. while ((dup >= 1) && (GetNextLink(List)))//->Link!=NULL)) { //change 2 List=GetNextLink(List);//->Link; dup--; } if (EqualTo(CopyPtr, List)) { FreeNode(NewPtr); nodecounter--; Name=TRUE; } else { if (GreaterThan(CopyPtr, List)) { //change 3 from list->Link==NULL to not getnextlink if (!GetNextLink(List))//->Link == NULL) { SetNextLink(CopyPtr, NULL); //pointing from new to greater SetPrevLink(CopyPtr, List); //pointing from new to lesser node SetNextLink(List, CopyPtr); //pointing from new to lesser node Name=TRUE; List=CopyPtr; } else //if there is a next link { //change from list->link to getnextlink if (LessThan(CopyPtr, (GetNextLink(List))))//->Link)) //if less than next link { PrevPtr=List; //change from prevptr->link to getnextlink SetNextLink(CopyPtr, (GetNextLink(PrevPtr)));//->Link); //pointing from new to greater SetNextLink(PrevPtr, CopyPtr); //pointing from greater to new SetPrevLink(CopyPtr, PrevPtr); //pointing from new to lesser node //change from copyptr->link to getnextlink List=GetNextLink(CopyPtr);//->Link; SetPrevLink(List, CopyPtr); tmp=GetItem(CopyPtr); Name=TRUE; List=CopyPtr; } else // if greater than next link { beginning = middle+1; } } } else // if less than the current pointer { if (!GetPrevLink(List))//->PrevLink==NULL) // if no previous link { SetPrevLink(CopyPtr,NULL); SetPrevLink(List, CopyPtr); SetNextLink(CopyPtr, List); Name=TRUE; List=CopyPtr; } else //if there is a previous link { if (GreaterThan(CopyPtr, (GetPrevLink(List))))//->PrevLink)) //greater than previous? { PrevPtr=GetPrevLink(List);//->PrevLink; //This is so the prev links point SetPrevLink(List, CopyPtr); //pointing from greater node to new SetNextLink(PrevPtr, CopyPtr); //pointing from small node to new SetNextLink(CopyPtr, List); //pointing from new node to greater node SetPrevLink(CopyPtr, PrevPtr); //pointing from new node back to small node Name=TRUE; List=CopyPtr; } else //less than previous { end = middle-1; } } } } } } return TRUE; } /********************************************************************** * * Function: Remove * Input: DLnkNodePtr List - the list to insert into * char elem[35] - the filename to insert * * Returns: Boolean TRUE on success * * Description: * This function removes a node from the list provided using a modified * binary search algorithm. The search will locate where in the list the * item should be, and removes from that point. It will update the * surrounding nodes too. * * Variables: DLnkNodePtr PrevPtr - the previous node in the list * DLnkNodePtr NewPtr - the node being removed * DLnkNodePtr CopyPtr - used when necessary to point to a node * MovieNode tmp - used to set the item of the new node * Boolean Name - the flag for detection * int beginning - the beginning of the current search * int end - the end of the current search * int middle - the middle element in the current search * int dup - the number of times to jump an item in the list * * Called by: Delete * * Calls: AllocateNewNode * strcpy * SetItem * SetNextLink * SetPrevLink * EqualTo * SetList * GreaterThan * LessThan * FreeNode * * Author: Tim McGaughey * Revisions: none * Version: 1.0 * * **********************************************************************/ Boolean Remove(DLnkNodePtr& List, char elem[35]) { DLnkNodePtr PrevPtr, NewPtr, CopyPtr; MovieNode tmp; AllocateNewNode(NewPtr); if (List==NULL) { FreeNode(NewPtr); return FALSE; } else { strcpy(tmp.filename,elem); SetItem(NewPtr, tmp); SetNextLink(NewPtr, NULL); SetPrevLink(NewPtr, NULL); CopyPtr=NewPtr; PrevPtr=NULL; int beginning=1; int end; int middle; int dup; end = nodecounter+1; while (beginning <= end) { middle=((end+beginning)/2); dup=middle-1; while (GetPrevLink(List))//->PrevLink!=NULL) { List=GetPrevLink(List);//->PrevLink; } //change 1 List->link!=NULL to GetNextLink while ((dup >= 1) && (GetNextLink(List)))//->Link!=NULL)) { List=GetNextLink(List);//->Link; dup--; } if (EqualTo(CopyPtr, List)) { FreeNode(NewPtr); PrevPtr=GetPrevLink(List);//->PrevLink; CopyPtr=GetNextLink(List);//->Link; if (List->Item.filetype=='M') { moviecounter--; BluntRemove(List->Item.movieindex); PrevPtr=GetPrevLink(List);//->PrevLink; CopyPtr=GetNextLink(List);//->Link; } FreeNode(List); if (CopyPtr!=NULL) SetPrevLink (CopyPtr, PrevPtr); if (PrevPtr!=NULL) SetNextLink (PrevPtr, CopyPtr); if (CopyPtr!=NULL) { List=CopyPtr; } else { List=PrevPtr; } if (List!=NULL) { while (GetPrevLink(List))//->PrevLink!=NULL) { List=GetPrevLink(List);//->PrevLink; } } SetList(List); return TRUE; } else { if (GreaterThan(CopyPtr, List)) { beginning = middle+1; } else { end = middle - 1; } } } } FreeNode(NewPtr); return FALSE; } /********************************************************************** * * Function: BinarySearch * Input: DLnkNodePtr List - the list to insert into * char elem[35] - the filename to insert * * Returns: Boolean TRUE on success * * Description: * This function removes a node from the list provided using a modified * binary search algorithm. The search will locate where in the list the * item should be, and removes from that point. It will update the * surrounding nodes too. * * Variables: DLnkNodePtr PrevPtr - the previous node in the list * DLnkNodePtr NewPtr - the node being removed * DLnkNodePtr CopyPtr - used when necessary to point to a node * MovieNode tmp - used to set the item of the new node * Boolean Name - the flag for detection * int beginning - the beginning of the current search * int end - the end of the current search * int middle - the middle element in the current search * int dup - the number of times to jump an item in the list * * Called by: VerifyVRS * Modify * ModFileName * * Calls: AllocateNewNode * strcpy * SetItem * SetNextLink * SetPrevLink * EqualTo * SetList * GreaterThan * LessThan * FreeNode * * Author: Tim McGaughey * Revisions: none * Version: 1.0 * * **********************************************************************/ Boolean BinarySearch(DLnkNodePtr& List, char elem[35]) { DLnkNodePtr PrevPtr, NewPtr, CopyPtr; MovieNode tmp; AllocateNewNode(NewPtr); if (List==NULL) { FreeNode(NewPtr); return FALSE; } else { strcpy(tmp.filename,elem); SetItem(NewPtr, tmp); SetNextLink(NewPtr, NULL); SetPrevLink(NewPtr, NULL); CopyPtr=NewPtr; PrevPtr=NULL; int beginning=1; int end; int middle; int dup; end = nodecounter+1; while (beginning <= end) { middle=((end+beginning)/2); dup=middle-1; while (GetPrevLink(List))//->PrevLink!=NULL) { List=GetPrevLink(List);//->PrevLink; } while ((dup >= 1) && (GetNextLink(List)))//->Link!=NULL)) { List=GetNextLink(List);//->Link; dup--; } if (EqualTo(CopyPtr, List)) { FreeNode(NewPtr); SrchPtr=List; return TRUE; } else { if (GreaterThan(CopyPtr, List)) { beginning = middle+1; } else { end = middle - 1; } } } } FreeNode(NewPtr); return FALSE; } /********************************************************************** * * Function: BluntRemove * Input: int movieindex - the movie index to remove * * Returns: void * * Description: * Removes all nodes with a particular index number * * Variables: DLnkNodePtr List - Current List * DLnkNodePtr Next - The next pointer * DLnkNodePtr head - points to the head of the list * * Called by: Remove * * Calls: GetPrevLink * GetList * GetNextLink * Remove * * Author: Tim McGaughey * Revisions: none * Version: 1.0 * * **********************************************************************/ void BluntRemove(int movieindex) { DLnkNodePtr List=NULL; DLnkNodePtr Next=NULL; DLnkNodePtr NewList=NULL; DLnkNodePtr head=NULL; List=GetList(); while (GetPrevLink(List)) { List=GetPrevLink(List); } head=List; Next=GetNextLink(List); while (List) { if ((List->Item.movieindex==movieindex)&&(List->Item.filetype=='R')) { Remove(List, List->Item.filename); List=GetList(); Next=GetNextLink(List); } List=Next; Next=GetNextLink(List); } } /********************************************************************** * * Function: GetNextReviewCounter * Input: void * * Returns: int - next review counter * * Description: * Returns the next review counter * * Variables: none * * Called by: Remove * * Calls: ModFileName * * Author: Tim McGaughey * Revisions: none * Version: 1.0 * * **********************************************************************/ int GetNextReviewCounter() { return ++reviewcounter; } /********************************************************************** * * Function: GetMovieCounter * Input: void * * Returns: int - next movie counter * * Description: * Returns the next movie counter * * Variables: none * * Called by: MainHandler * List * * Calls: none * * Author: Tim McGaughey * Revisions: none * Version: 1.0 * * **********************************************************************/ int GetMovieCounter() { return moviecounter; } /********************************************************************** * * Function: SetMovieCounter * Input: counter * * Returns: void * * Description: * Sets the movie counter * * Variables: none * * Called by: ReadMRS * * Calls: none * * Author: Tim McGaughey * Revisions: none * Version: 1.0 * * **********************************************************************/ void SetMovieCounter(int counter) { moviecounter=counter; } /********************************************************************** * * Function: GetSrchPtr * Input: void * * Returns: void * * Description: * Returns the pointer found by a Binary Search * * Variables: none * * Called by: ReadMRS * * Calls: none * * Author: Tim McGaughey * Revisions: none * Version: 1.0 * * **********************************************************************/ DLnkNodePtr GetSrchPtr(void) { return SrchPtr; }