Data structure is one of the greatest challenges behind pointer learning C/C ++. The core of this issue is the linked list. Linked List is the most widely used data structure. It can be said that it is a stepping stone for you to learn how to enter the intermediate level of computer! This article provides complete two-way linked list Code. Although there are only several page partitions, it is worth your attention to every line! I hope that beginners will spend a week (of course you have to understand the pointer without any difficulty) and get familiar with it to the point where they can talk to others! This code is from the DirectX example of vc5 SDK. In vcsamples \ SDK \ DirectX \ include and vcsamples \ SDK \ DirectX \ cgutilview plaincopy to clipboardprint? // | File: linklist. h # ifndef _ linklist_h # DEFINE _ linklist_h typedef struct _ node {struct _ node * pprev; struct _ node * pnext; void * pdata;} node, * lpnode; class clinkedlist {PRIVATE: lpnode find (void * pdata); lpnode phead; lpnode ptail; lpnode pcurposition; public: clinkedlist ();~ Clinkedlist (); void * getfirst (); void * getlast (); void * getnext (void * pdata); void * getprev (); void * getprev (void * pdata); void add (void * pdata); void insert (void * pdata, void * pbefore ); void append (void * pdata); void remove (void * pdata); void * removefirst (); void * removelast ();}; # define publish list clinkedlist # endif // | file: linklist. h # Ifndef _ linklist_h # DEFINE _ linklist_h typedef struct _ node {struct _ node * pprev; struct _ node * pnext; void * pdata;} node, * lpnode; Class clinkedlist {PRIVATE: lpnode find (void * pdata); lpnode phead; lpnode ptail; lpnode pcurposition; public: clinkedlist ();~ Clinkedlist (); void * getfirst (); void * getlast (); void * getnext (void * pdata); void * getprev (); void * getprev (void * pdata); void add (void * pdata); void insert (void * pdata, void * pbefore ); void append (void * pdata); void remove (void * pdata); void * removefirst (); void * removelast ();}; # define publish list clinkedlist # endif view plaincopy to clip Boardprint? // | File: linklist. CPP = a doubly linked list. # include "linklist. H "# define null 0l clinkedlist: clinkedlist () // clinkedlist constructor {phead = ptail = pcurposition = NULL;} clinkedlist ::~ Clinkedlist () // clinkedlist destructor-free each node {lpnode pcur, pnext; pcur = phead; phead = ptail = pcurposition = NULL; // go thru list and free each node while (pcur! = NULL) {pnext = pcur-> pnext; Delete (pcur); pcur = pnext ;}} // getfirst-return APP data for first entry in list and make it the current node. void * clinkedlist: getfirst () {pcurposition = phead; If (pcurposition = NULL) {return (null);} return (pcurposition-> pdata );} // getlast-return APP data to last entry in list and make it the current node. void * clinkedlist: getlast () {pcurpositio N = ptail; If (pcurposition = NULL) {return (null);} return (pcurposition-> pdata );} // getnext-return next app data entry in list and make it the current node. void * clinkedlist: getnext () {lpnode pnext; // check for empty list or already at end of list. if (pcurposition = NULL) | (pcurposition-> pnext = NULL) {return (null);} pnext = pcurposition-> pnext; pcurposition = pnext; return (PN EXT-> pdata);} // getfirst-return APP data that follows a given entry and make it the current node. void * clinkedlist: getnext (void * pdata) {pcurposition = find (pdata); Return (getnext ());} // getprev-return APP data for previous entry in list and make it the current node. void * clinkedlist: getprev () {lpnode pprev; // check for empty list or already at start if (pcurposition = NULL) | (Pcurposition-> pprev = NULL) {return (null);} pprev = pcurposition-> pprev; pcurposition = pprev; Return (pprev-> pdata );} // getfirst-return APP data that preceeds a given entry and make it the current node. void * clinkedlist: getprev (void * pdata) {pcurposition = find (pdata); Return (getprev ());} // Add-create a new node and put it at the start of the list and make it the current node. voi D clinkedlist: add (void * pdata) {lpnode pnew = new node; // setup node and prepare it for its role as the new head of the list pnew-> pdata = pdata; pnew-> pnext = phead; pnew-> pprev = NULL; // The old head of list (if any) shocould point to new node) if (phead! = NULL) phead-> pprev = pnew; // make new node the head and current position phead = pnew; pcurposition = pnew; // check to see if new node is also the tail (ie. only List entry) if (ptail = NULL) ptail = pnew;} // append-create a new node and put it at the end of the list. void clinkedlist: append (void * pdata) {lpnode pnew = new node; // setup node and prepare it for its role as the new tail o F The list pnew-> pdata = pdata; pnew-> pprev = ptail; pnew-> pnext = NULL; // The Old Tail of list (if any) shocould point to new node. if (ptail! = NULL) ptail-> pnext = pnew; // make new node the tail ptail = pnew; // check to see if new node is also the head (ie. only List entry) if (phead = NULL) {phead = pnew; pcurposition = pnew ;}} // find-Private method to find the node with the specified APP data attached to it. lpnode clinkedlist: Find (void * pdata) {lpnode pcur; // go thru list until we reach end or we find the right node. for (Pcur = phead; (pcur! = NULL) & (pcur-> pdata! = Pdata); pcur = pcur-> pnext); Return (pcur );} // insert-create a new node and put it in front of the current position node and make it the current position. void clinkedlist: insert (void * pdata) {lpnode pnext, pprev; lpnode pnew = new node; pnew-> pdata = pdata; // check to be sure that there is a current node if (pcurposition! = NULL) {// get pointers of current position pprev = pcurposition-> pprev; pnext = pcurposition-> pnext; // set new nodes pointers for insertion into the list pnew-> pprev = pprev; pnew-> pnext = pcurposition; // set the node in front of new node (if any) to point to it if (pprev! = NULL) {pprev-> pnext = pnew; // No node in front-> new node is at head} else {phead = pnew ;} // make new node the current node pcurposition = pnew; // no current node, just add to front} else {Add (pdata );}} // insert-create a new node and put it in front of the specified node and make it the current position. void clinkedlist: insert (void * pdata, void * pbefore) {// simply make the spec Ified node current and insert the new // node. pcurposition = find (pbefore); insert (pdata);} // remove-remove a specified node from the list. // note: we do not delete the APP data attached to the node! Void clinkedlist: Remove () {lpnode pcur, pnext, pprev; pcur = pcurposition; If (pcur! = NULL) {// save a copy of The Links pprev = pcur-> pprev; pnext = pcur-> pnext; // is there a node ahead of us? If (pprev! = NULL) {// yes-> Update it to not point to us. pprev-> pnext = pnext;} else {// No-> Update head to not point to us. phead = pnext; pcurposition = pnext;} // is there a node behind us? If (pnext! = NULL) {// yes-> Update it to not point to us. pnext-> pprev = pprev; pcurposition = pnext;} else {// No-> Update tail to not point to us. ptail = pprev; pcurposition = pprev;} Delete (pcur) ;}// remove-remove a specified node from the list. // note: we do not delete the APP data attached to the node! Void clinkedlist: Remove (void * pdata) {pcurposition = find (pdata); remove ();} // removefirst-remove the first node in the list and return the APP data associated with it. void * clinkedlist: removefirst () {lpnode pcur, pnext; void * pdata = NULL; pcur = phead; // is there a node at the head? If (pcur! = NULL) {// take first node out of list. pnext = pcur-> pnext; phead = pnext; pcurposition = pnext; // are there any nodes after us? If (pnext! = NULL) {// yes-> make it the new head pnext-> pprev = NULL;} else {// No-> the list is now empty ptail = NULL ;} // get APP data for node and then delete it pdata = pcur-> pdata; Delete (pcur);} return (pdata );} // removelast-remove the last node in the list and return the APP data associated with it. void * clinkedlist: removelast () {lpnode pcur, pprev; void * pdata = NULL; pcur = ptail ;// Is there a node at the tail? If (pcur! = NULL) {// take last node out of list. pprev = pcur-> pprev; ptail = pprev; // are there any nodes ahead of us? If (pprev! = NULL) {// yes-> make it the new tail node pprev-> pnext = NULL;} else {// No-> list is now empty phead = NULL; pcurposition = NULL;} // get APP data for node and then delete it pdata = pcur-> pdata; Delete (pcur) ;}return (pdata );}