Ignore:
Timestamp:
Jan 19, 2003, 8:42:16 PM (23 years ago)
Author:
umoeller
Message:

First attempt at new container contol.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/linklist.c

    r238 r242  
    675675
    676676/*
     677 *@@ InsertFront:
     678 *
     679 *@@added V1.0.1 (2003-01-17) [umoeller]
     680 */
     681
     682VOID InsertFront(PLINKLIST pList,
     683                 PLISTNODE pNewNode)
     684{
     685    if (pList->pFirst)
     686        pList->pFirst->pPrevious = pNewNode;
     687
     688    pNewNode->pNext = pList->pFirst;
     689    pNewNode->pPrevious = NULL;
     690
     691    pList->pFirst = pNewNode;
     692
     693    if (!pList->pLast)
     694        // the list was empty:
     695        pList->pLast = pNewNode;        // V0.9.14 (2001-07-14) [umoeller]
     696
     697    (pList->ulCount)++;
     698}
     699
     700/*
     701 *@@ InsertAfterNode:
     702 *
     703 *@@added V1.0.1 (2003-01-17) [umoeller]
     704 */
     705
     706VOID InsertAfterNode(PLINKLIST pList,
     707                     PLISTNODE pNewNode,
     708                     PLISTNODE pNodeInsertAfter)
     709{
     710    // 1) set pointers for new node
     711    pNewNode->pPrevious = pNodeInsertAfter;
     712    pNewNode->pNext = pNodeInsertAfter->pNext;
     713
     714    // 2) adjust next item
     715    // so that it points to the new node
     716    if (pNodeInsertAfter->pNext)
     717        pNodeInsertAfter->pNext->pPrevious = pNewNode;
     718
     719    // 3) adjust previous item
     720    // so that it points to the new node
     721    pNodeInsertAfter->pNext = pNewNode;
     722
     723    // 4) adjust last item, if necessary
     724    if (pList->pLast == pNodeInsertAfter)
     725        pList->pLast = pNewNode;
     726
     727    (pList->ulCount)++;
     728}
     729
     730/*
    677731 *@@ lstInsertItemBefore:
    678732 *      this inserts a new node to the list. As opposed to
     
    699753
    700754PLISTNODE lstInsertItemBefore(PLINKLIST pList,
    701                               void* pNewItemData,     // data to store in list node
    702                               unsigned long ulIndex)
     755                              void* pNewItemData,       // in: data to store in list node
     756                              unsigned long ulIndex)    // in: index before which to insert
    703757{
    704758    PLISTNODE pNewNode = NULL;
     
    715769        {
    716770            // insert at beginning:
    717             if (pList->pFirst)
    718                 pList->pFirst->pPrevious = pNewNode;
    719 
    720             pNewNode->pNext = pList->pFirst;
    721             pNewNode->pPrevious = NULL;
    722 
    723             pList->pFirst = pNewNode;
    724 
    725             if (!pList->pLast)
    726                 // the list was empty:
    727                 pList->pLast = pNewNode;        // V0.9.14 (2001-07-14) [umoeller]
    728 
    729             (pList->ulCount)++;
     771            InsertFront(pList,
     772                        pNewNode);
    730773        }
    731774        else
     
    735778
    736779            if (pNodeInsertAfter = lstNodeFromIndex(pList,
    737                                                     (ulIndex - 1)))
     780                                                    ulIndex - 1))
    738781            {
    739                 // 1) set pointers for new node
    740                 pNewNode->pPrevious = pNodeInsertAfter;
    741                 pNewNode->pNext = pNodeInsertAfter->pNext;
    742 
    743                 // 2) adjust next item
    744                 // so that it points to the new node
    745                 if (pNodeInsertAfter->pNext)
    746                     pNodeInsertAfter->pNext->pPrevious = pNewNode;
    747 
    748                 // 3) adjust previous item
    749                 // so that it points to the new node
    750                 pNodeInsertAfter->pNext = pNewNode;
    751 
    752                 // 4) adjust last item, if necessary
    753                 if (pList->pLast == pNodeInsertAfter)
    754                     pList->pLast = pNewNode;
    755 
    756                 (pList->ulCount)++;
     782                InsertAfterNode(pList,
     783                                pNewNode,
     784                                pNodeInsertAfter);
    757785            }
    758786            else
     
    763791            }
    764792        }
     793    }
     794
     795    return pNewNode;
     796}
     797
     798/*
     799 *@@ lstInsertItemAfterNode:
     800 *      this inserts a new node to the list. As opposed to
     801 *      lstAppendItem, the new node can be appended anywhere
     802 *      in the list, that is, it will be appended AFTER
     803 *      the given existing list node.
     804 *
     805 *      If pNodeAfter is NULL, the new item will be made the
     806 *      first item.
     807 *
     808 *      As opposed to lstInsertItemBefore, this does not
     809 *      need to traverse the list, so it is very quick.
     810 *
     811 *      This returns the LISTNODE of the new list item,
     812 *      or NULL upon errors.
     813 *
     814 *@@added V1.0.1 (2003-01-17) [umoeller]
     815 */
     816
     817PLISTNODE lstInsertItemAfterNode(PLINKLIST pList,
     818                                 void* pNewItemData,            // in: data to store in list node
     819                                 PLISTNODE pNodeInsertAfter)    // in: node to insert after or NULL to make new node the first
     820{
     821    PLISTNODE pNewNode = NULL;
     822
     823    if (    (pList)
     824         && (pList->ulMagic == LINKLISTMAGIC)
     825         && (pNewNode = (PLISTNODE)malloc(sizeof(LISTNODE)))
     826       )
     827    {
     828        memset(pNewNode, 0, sizeof(LISTNODE));
     829        pNewNode->pItemData = pNewItemData;
     830
     831        if (!pNodeInsertAfter)
     832            // insert at beginning:
     833            InsertFront(pList,
     834                        pNewNode);
     835        else
     836            InsertAfterNode(pList,
     837                            pNewNode,
     838                            pNodeInsertAfter);
    765839    }
    766840
Note: See TracChangeset for help on using the changeset viewer.