Node int linkedlistnext int tmp current data current

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: erator() void LinkedList::SetIterator { current = head; } serves that purpose. W e probably should set current to 0 iin the We current to n constructor to indicate a non-set iterator, as it will constructor set iterator as be when the end is reached. be Applied Programming How about Applied Programming The next function returns the data at the current The node of the list and moves current on to the next node. node. int LinkedList::Next() { int tmp = current->data; current = current->next; current current return tmp; return tmp } It will fail if current iis 0, so there should be a It current s so check. check. Applied Programming Here is how an iterator is used: Here iterator bool LinkedList::More() { return (current != 0); } LinkedList list; int data; // There is a problem. W hat if the user adds or deletes a node while What iterating? iterating? Such changes should be locked out. Or current should be reset to ?. Or current should // Applied Programming set up the list list.SetIterator(); while(list.More()) { data = list.Next(); data list.Next process data } Applied Programming W e can actually use iterators to perform We iterators to additions and deletions. additions void InsertBeforeCurrent(const int&); void InsertBeforeCurrent(const int DeleteCurrent(); W e would need the extra member data variable We previous for these extra actions. previous Iteration could then continue. Recall: const int MAXTITLE = 40; const int const int MAXCATEGORY = 10; const int struct Video { char Title[MAXTITLE+1]; long BarCode; long BarCode short RunTime; short RunTime char Category[MAXCATEGORY+1]; short NumCopies; short NumCopies }; LinkedList VideoDB; Applied Programming Applied Programming W hy can't Video be a class? Why Video We now provide int ListDataCmp(const Video& V1, const Video& V2); to indicate the alphabetical order of the Title. to Title. Everything else is as described in our general Everything LinkedList. LinkedList In fact we can go one step further. Applied Programming A further possibility. W e cou...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online