Create a dialog box-based MFC ApplicationProgramThe interface is as follows (default is used for all attributes ):
First, add a simple struct definition (before the class definition in the dialog box ):
Typedef struct _ tagnode {cstring name; int age;} node;
Add a member variable to the dialog box class:
PRIVATE:
Cptrlist m_list;
Then add several member functions for the dialog box class:
Void cdxdlg: insertnode (uint N) // insert n elements {for (uint I = 0; I <n; I ++) {node * pnode = new node; pnode-> name. format ("name % d", I + 1); pnode-> age = 10 * (I + 1); m_list.addtail (pnode) ;}} void cdxdlg: Display () // display the {position Pos; cstring Info; For (Pos = m_list.getheadposition (); pos! = NULL;) {node * pnode = (node *) m_list.getnext (POS); If (pnode! = NULL) {info. format ("name = % s, age = % d", pnode-> name, pnode-> age); MessageBox (Info) ;}} bool cdxdlg :: removenode (node * pnode) // depending on the specific node {If (pnode) {position POs, temp; For (Pos = m_list.getheadposition (); pos! = NULL;) {temp = Pos; node * node = (node *) m_list.getnext (POS); If (node & node-> name = pnode-> name) // Delete the first element with the same name {delete node; // The allocated space m_list.removeat (temp) should be released before deletion; // Delete the element return true ;}} return false; // deletion failed (the elements meeting the deletion criteria cannot be found )}
Call the insert node function in oninitdialog () in the dialog box, such as insertnode (5 );
Add a click event for button 1:
Void cdxdlg: onbutton1 () // Delete the node with the same name as in the edit box from the list {cedit * pedit = (cedit *) getdlgitem (idc_edit1); cstring delname; // name of the node to be deleted pedit-> getwindowtext (delname); node. name = delname; node. age = 0; If (removenode (& node) // delete node {MessageBox ("Remove node succ") ;}else {MessageBox ("Remove node failed ");} display (); // whether the deletion is successful or not, display the elements in the list after the operation}
Note thatIn the close dialog box, release the allocated space.So the destroywindow (),CodeAs follows:
Bool cdxdlg: destroywindow () {position POs, temp; For (Pos = m_list.getheadposition (); Pos;) {temp = Pos; node * node = (node *) m_list.getnext (POS); If (node) {delete node; // release space m_list.removeat (temp); // Delete element} return cdialog: destroywindow ();}
To release the elements in the cptrlist queue, you can use the following function:
/* function: Release the elements in the specified queue and the queue itself (the element type in plist must be cmessbuf) @ plist: cmessbuf queue @ bdeleteobject: whether to release the space pointed to by plist (the default value is true. If the space pointed to by plist is not allocated by new, you can set this value to false when calling. */void cgateview: freelist (cptrlist * plist, bool bdeleteobject) {While (plist-> getcount ()> 0) {node * P = (node *) plist-> removehead (); If (p) delete p;} If (bdeleteobject & plist) delete plist;}