Two-way linked list code

xiaoxiao2021-03-14  183

// | 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 * getNext (); void * getnext (); 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 remove (void * resovefirst (); void * removefirst ();}; #define linkedlist clinkedList #ENDIF / / / / | file: linklist.cpp = a doubly linked list. #Include "LINKLIST" 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 it it // the current node. Void * clinkedlist :: getLast () {pcurposition = 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 (pnext-> pdata) } // GetFirst - Return App Data That Follows A Given entry and make it it /// Current Node. Void * ClinkedList :: getNext (void * pdata) {pcurposition = find (pdata); return ());} . // 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. Void CLINKEDLIST:: Add (void * pdata) {lpnode pnew = new node; // setup node and prepare it for ipt pnew-> pdata = PDATA; PNEW-> PNEXT = PHEAD; PNEW-> PPREV = NULL; // the old head of list (if any) Should point to new node) f (PHEAD! = null) PHEAD-> pprev = pnew; // Make New node the head and current position phet = pnew; pcurposition = PNEW;

// check to see if new node is also the tail (ie. Only List entry) 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 ips role as the new tail of the list pnew-> pdata = pdata; pnew-> pprev = ptail; PNEW-> PNEXT = null; // the old tail of list (if any) Should 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) {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 untric node. for (pcur! = null) && (pcur! > PDATA! = PDATA); PCUR = PCUR-> PNEXT); RETURN (PCUR);} // INSERT - CRE ate 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 suhe, = pcurposition-> pprev; pnext = pcurposition-> pnext; // set new nodes point; INSERTION INTO the list pnew-> pprev = pprev; pnew-> pnext = pcurposition;

// set the node in front of new node (if any) to point 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 Put IN Front of Put the specified // node and make it the current position void CLinkedList :: Insert (void * pData, void * pBefore) {// simply make the specified 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 (pprev! = Null) {// YES -> Update it to not point points us. pprev-> pnext = pnext;} else {// no -> Update head to not point to us. PHEAD = PNEXT; PCURPSITION = 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. // NODE: We do not de de de de de de de de de de de de de de de de de de de de de de de de de de de de de de de de de de de de delte! Void ClinkedList :: Remove (void * p Data) {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 = node at the head? If (pcur! = Null) {// Take first node out of list. Pnext = pcur-> pnext; PHEAD = PNext; pcurPosition = PNEXT; // area? {// yes-> make it it new head pnext-> pprev = null;} else {// no -> the list is now Empty PTAIL = NULL;

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

New Post(0)