A universal chain table class

xiaoxiao2021-03-06  43

The linked list is an indispensable data structure, and many runtime libraries offer ready-made linked tables. Of course we can also implement it yourself. Here is an example. The definition of the class is as follows: // *************************************************** ***************************************** //// CLASS COBLIST Defination //// ****** *********************************************************** *******************

#define position cobnode *

Struct cobnode {position pnext; void * pitem;};

class COBLIST {protected: POSITION m_pHead; POSITION m_pTail; virtual BOOL Compare (void * pItemToCompare, void * pComparator) {return (pItemToCompare == pComparator);}; public: COBLIST (): m_pHead (NULL), m_pTail (NULL) { }; virtual ~ COBLIST (); virtual void * RemoveAt (POSITION rPos); void emptyList (); POSITION AddTail (void * pItem); void * getTail (); void * GetNext (POSITION & rPos); void * SafeGetFromPosition (POSITION rPos ); Position getPosition; position lookup (void * pcomparetor); position getHeadPosition () {return (m_phead);}; position gettailposition () {return (m_ptail);}; bool} () {Return (! m_pHead);}; # ifdef DEBUG void * GetHead (); void * RemoveHead (); void * RemoveTail (); void * GetFromPosition (POSITION rPos); # else void * GetHead () {return GetFromPosition (GetHeadPosition ()); }; Void * removehead () {RETURN REMOVEAT;}; void * removetail () {return removeat;}; void * getFromPosition (position rpos) {return (null == rpos) NULL: (rpos-> pitem); # }d 实 实 下下 下下 下: 下::::: 如: 如:;:;:;:;:;:;:;:;:;:;:;:;:;:}:;::}: ********************************************** / / // CLASS COBLIST / / / / *** *********************************************************** **********************

// ******************************************************** *************************** //// DESTRUCTOR / / / / ************** *********************************************************** ************

Coblist :: ~ coblist () {assert (isempty ());

#ifdef debugvoid * coblist :: getHead () {assert (m_phead); if (null == m_phead) Return NULL;

Return m_phead-> pitem;} # endif void * coblist :: gettail () {assert (m_ptail); if (null == m_ptail) Return NULL;

Return m_ptail-> pitem;}

// ******************************************************** ************************************* / //// Void * Coblist :: getNext (position & rpos) //// **** *********************************************************** *********************

Void * coblist :: getNext (position & rpos) {assert (rpos); if (null == rpos) Return NULL;

Void * PRETURN = RPOS-> Pitem; rpos = rpos-> pnext;

Return PRETURN;

// ******************************************************** *************************** / / //VOID * Coblist :: removeat (position pos) //// **** *********************************************************** *********************

Void * coblist :: removeat (position pos) {if (null == pOS) Return NULL;

Void * PRETURN = NULL;

IF (m_pHead) {if (m_pHead == POS) {// Removing the first element in the list m_phead = POS-> PNEXT; PRETURN = POS-> Pitem; delete POS;

IF (NULL == m_phead) {// Removing the only element! m_ptail = null;}} else {position pcur = m_phead;

While (pcur && pcur-> pnext) {if (pcur-> pnext == pOS) {// removing pcur-> pnext = POS-> PNEXT; if (m_ptail == pOS) {m_ptail = pcur;} PRETURN = POS -> Pitem; delete pos; break;} pcur = pcur-> pnext;}}}

Return PRETURN;

// ******************************************************** *************************** / / / / POSION COBLIST :: Addtail (void * pitem) //// **** *********************************************************** *********************

Position coblist :: addtail (void * pitem) {position posret = NULL;

IF (m_ptail) {if (m_ptail-> pnext = new cobnode) {m_ptail = m_ptail-> pnext; m_ptail-> pithm = pitem; m_ptail-> pnext = null;} else return null;} else {assert (! m_phead) ; If (m_phead = new cobnode) {m_ptail = m_phead; m_ptail-> pitem = pitem; m_ptail-> pnext = null;}}

Return M_PTAIL;

// ******************************************************** ************************************* / / // void coblist :: EmptyList () //// ********* *********************************************************** *******************

Void Coblist :: EmptyList () {whele (! iSempty ()) {transheadposition ());}}

#ifdef debugvoid * coblist :: removetail () {assert (m_phead); assert (m_ptail); if ((null == m_phead) || (NULL == m_ptail) Return NULL;

Return Removeat;}

Void * coblist :: removehead () {assert (m_phead); assert (m_ptail); if ((NULL == m_phead) || (null == m_ptail) Return NULL;

Return Removeat;}

Void * coblist :: getFromPosition (position POS) {void * result = safegetfromPosition (POS); assert (result); return result;} # endif / * debug * /

// ******************************************************** ************************************* / / / / POSITION COBLIST:: getPosition (void * _pitem) //// **** *********************************************************** ****************************** POSITION COBLIST :: getPosition (void * _pitem) {position position = m_phead;

While (position) {if (position-> pitem == _pitem) {Break;} getNext (position);} returnit

// ******************************************************** ************************************ / / // Position Coblist :: Lookup (void * pcomparetor) //// **** *********************************************************** *********************

Position Coblist :: lookup (void * pcomparetor) {position position = m_phead;

While (position) {if (compare (position-> pitem, pcomparator)) {breaf;} getnext (position);} returnit position;}

// ******************************************************** ************************************* / / //VOID * Coblist :: SafegetFromPosition (Position POS) //// **** *********************************************************** *********************

void * COBLIST :: SafeGetFromPosition (POSITION Pos) {// Safe way to validate that an entry is still in the list, // which ensures bugs that would reference deleted memory, // reference a NULL pointer instead // (eg an event Handler Fires Late / Twice. // Note That Versioning On Entries Would The Find The Find Entry. // Walk List To Find Entry.

Position poswork = m_phead; while (poswork) {if (poswork == pOS) {RETURN POS-> Pitem;} getNext (POSWORK);} return null;}

// ******************************************************** *************************** / / / / / COBLIST UTILITY ROUTINES / / / / *********** *********************************************************** *************** // ****************************************** *************************************************** / / / POSITION AddNode PVOID PV, COBLIST ** PPLIST) /// Add a node to a list.//initializes the oblist, if Necessary.//////** in *********************************************************** ***********************

Position addNode (pvoid pv, coblist ** pplist) {assert (null! = Pplist); if (null == pplist) Return NULL;

IF (null == * pplist) {* pplist = new coblist (); if (null == * pplist) Return NULL;}

Return (* pplist) -> addtail (pv);

// ******************************************************** ***************************************** PVOID Removenode (position * ppos, coblist * plist) /// remove a node From a list.// sets ppos to null //// ******************************************** ************************************************

PVOID Removenode (position * ppos, coblist * plist) {if (null == plist) | (NULL == PPOS)) Return NULL;

PVOID PV = PLIST-> Removeat (* PPOS); * PPOS = NULL; RETURN PV;} This class is very simple, and the CLIST of the MFC is almost.

转载请注明原文地址:https://www.9cbs.com/read-57452.html

New Post(0)