Changeset 132 for trunk/src/helpers/linklist.c
- Timestamp:
- Jan 19, 2002, 11:50:39 AM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/linklist.c
r113 r132 292 292 // item pointers upon destruction? 293 293 { 294 PLINKLIST pNewList = (PLINKLIST)malloc(sizeof(LINKLIST));295 if (pNewList )294 PLINKLIST pNewList; 295 if (pNewList = (PLINKLIST)malloc(sizeof(LINKLIST))) 296 296 lstInit(pNewList, fItemsFreeable); 297 297 return (pNewList); … … 444 444 PLISTNODE pNode = NULL; 445 445 446 if (pList) 447 if (pList->ulMagic == LINKLISTMAGIC) 448 if (pList->pFirst) 449 { 450 unsigned long ulCount = 0; 451 pNode = pList->pFirst; 452 for (ulCount = 0; 453 ((pNode) && (ulCount < ulIndex)); 454 ulCount++) 455 { 456 if (pNode->pNext) 457 pNode = pNode->pNext; 458 else 459 pNode = NULL; // exit 460 } 461 } 446 if ( (pList) 447 && (pList->ulMagic == LINKLISTMAGIC) 448 && (pList->pFirst) 449 ) 450 { 451 unsigned long ulCount = 0; 452 pNode = pList->pFirst; 453 for (ulCount = 0; 454 ((pNode) && (ulCount < ulIndex)); 455 ulCount++) 456 { 457 if (pNode->pNext) 458 pNode = pNode->pNext; 459 else 460 pNode = NULL; // exit 461 } 462 } 462 463 463 464 return (pNode); … … 480 481 pNodeFound = 0; 481 482 482 if (pList) 483 if ( (pList->ulMagic == LINKLISTMAGIC) 484 && (pItemData) 485 ) 486 { 487 pNode = pList->pFirst; 488 while (pNode) 483 if ( (pList) 484 && (pList->ulMagic == LINKLISTMAGIC) 485 && (pItemData) 486 ) 487 { 488 pNode = pList->pFirst; 489 while (pNode) 490 { 491 if (pNode->pItemData == pItemData) 489 492 { 490 if (pNode->pItemData == pItemData) 491 { 492 pNodeFound = pNode; 493 break; 494 } 495 496 pNode = pNode->pNext; 493 pNodeFound = pNode; 494 break; 497 495 } 498 } 496 497 pNode = pNode->pNext; 498 } 499 } 499 500 500 501 return (pNodeFound); … … 518 519 unsigned long ulIndex) 519 520 { 520 PLISTNODE pNode = lstNodeFromIndex(pList, ulIndex);521 if (pNode )521 PLISTNODE pNode; 522 if (pNode = lstNodeFromIndex(pList, ulIndex)) 522 523 return (pNode->pItemData); 523 524 else … … 580 581 const char *function) 581 582 { 582 PLISTNODE pNewNode = 0; 583 584 if (pList) 585 if (pList->ulMagic == LINKLISTMAGIC) 586 { 587 if (pNewNode = (PLISTNODE)memdMalloc(sizeof(LISTNODE), file, line, function)) 588 { 589 memset(pNewNode, 0, sizeof(LISTNODE)); 590 pNewNode->pItemData = pNewItemData; 591 592 if (pList->pLast) 593 { 594 // list is not empty: append to tail 595 596 // 1) make last item point to new node 597 pList->pLast->pNext = pNewNode; 598 // 2) make new node point to last item 599 pNewNode->pPrevious = pList->pLast; 600 // 3) store new node as new last item 601 pList->pLast = pNewNode; 602 603 pList->ulCount++; 604 } 605 else 606 { 607 // list is empty: insert as first 608 pList->pFirst 609 = pList->pLast 610 = pNewNode; 611 612 pList->ulCount = 1; 613 } 614 } 615 } 583 PLISTNODE pNewNode = NULL; 584 585 if ( (pList) 586 && (pList->ulMagic == LINKLISTMAGIC) 587 && (pNewNode = (PLISTNODE)memdMalloc(sizeof(LISTNODE), file, line, function)) 588 ) 589 { 590 memset(pNewNode, 0, sizeof(LISTNODE)); 591 pNewNode->pItemData = pNewItemData; 592 593 if (pList->pLast) 594 { 595 // list is not empty: append to tail 596 597 // 1) make last item point to new node 598 pList->pLast->pNext = pNewNode; 599 // 2) make new node point to last item 600 pNewNode->pPrevious = pList->pLast; 601 // 3) store new node as new last item 602 pList->pLast = pNewNode; 603 604 pList->ulCount++; 605 } 606 else 607 { 608 // list is empty: insert as first 609 pList->pFirst 610 = pList->pLast 611 = pNewNode; 612 613 pList->ulCount = 1; 614 } 615 } 616 616 617 617 return (pNewNode); … … 635 635 void* pNewItemData) // in: data to store in list node 636 636 { 637 PLISTNODE pNewNode = 0; 638 639 if (pList) 640 if (pList->ulMagic == LINKLISTMAGIC) 641 { 642 if (pNewNode = (PLISTNODE)malloc(sizeof(LISTNODE))) 643 { 644 memset(pNewNode, 0, sizeof(LISTNODE)); 645 pNewNode->pItemData = pNewItemData; 646 647 if (pList->pLast) 648 { 649 // list is not empty: append to tail 650 651 // 1) make last item point to new node 652 pList->pLast->pNext = pNewNode; 653 // 2) make new node point to last item 654 pNewNode->pPrevious = pList->pLast; 655 // 3) store new node as new last item 656 pList->pLast = pNewNode; 657 658 pList->ulCount++; 659 } 660 else 661 { 662 // list is empty: insert as first 663 pList->pFirst 664 = pList->pLast 665 = pNewNode; 666 667 pList->ulCount = 1; 668 } 669 } 670 } 637 PLISTNODE pNewNode = NULL; 638 639 if ( (pList) 640 && (pList->ulMagic == LINKLISTMAGIC) 641 && (pNewNode = (PLISTNODE)malloc(sizeof(LISTNODE))) 642 ) 643 { 644 memset(pNewNode, 0, sizeof(LISTNODE)); 645 pNewNode->pItemData = pNewItemData; 646 647 if (pList->pLast) 648 { 649 // list is not empty: append to tail 650 651 // 1) make last item point to new node 652 pList->pLast->pNext = pNewNode; 653 // 2) make new node point to last item 654 pNewNode->pPrevious = pList->pLast; 655 // 3) store new node as new last item 656 pList->pLast = pNewNode; 657 658 pList->ulCount++; 659 } 660 else 661 { 662 // list is empty: insert as first 663 pList->pFirst 664 = pList->pLast 665 = pNewNode; 666 667 pList->ulCount = 1; 668 } 669 } 671 670 672 671 return (pNewNode); … … 703 702 unsigned long ulIndex) 704 703 { 705 PLISTNODE pNewNode = 0; 706 707 if (pList) 708 if (pList->ulMagic == LINKLISTMAGIC) 709 { 710 if (pNewNode = (PLISTNODE)malloc(sizeof(LISTNODE))) 704 PLISTNODE pNewNode = NULL; 705 706 if ( (pList) 707 && (pList->ulMagic == LINKLISTMAGIC) 708 && (pNewNode = (PLISTNODE)malloc(sizeof(LISTNODE))) 709 ) 710 { 711 memset(pNewNode, 0, sizeof(LISTNODE)); 712 pNewNode->pItemData = pNewItemData; 713 714 if (ulIndex == 0) 715 { 716 // 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)++; 730 } 731 else 732 { 733 // insert at a later position: 734 PLISTNODE pNodeInsertAfter = lstNodeFromIndex( 735 pList, 736 (ulIndex-1)); 737 738 if (pNodeInsertAfter) 711 739 { 712 memset(pNewNode, 0, sizeof(LISTNODE)); 713 pNewNode->pItemData = pNewItemData; 714 715 if (ulIndex == 0) 716 { 717 // insert at beginning: 718 if (pList->pFirst) 719 pList->pFirst->pPrevious = pNewNode; 720 721 pNewNode->pNext = pList->pFirst; 722 pNewNode->pPrevious = NULL; 723 724 pList->pFirst = pNewNode; 725 726 if (!pList->pLast) 727 // the list was empty: 728 pList->pLast = pNewNode; // V0.9.14 (2001-07-14) [umoeller] 729 730 (pList->ulCount)++; 731 } 732 else 733 { 734 // insert at a later position: 735 PLISTNODE pNodeInsertAfter = lstNodeFromIndex( 736 pList, 737 (ulIndex-1)); 738 739 if (pNodeInsertAfter) 740 { 741 // 1) set pointers for new node 742 pNewNode->pPrevious = pNodeInsertAfter; 743 pNewNode->pNext = pNodeInsertAfter->pNext; 744 745 // 2) adjust next item 746 // so that it points to the new node 747 if (pNodeInsertAfter->pNext) 748 pNodeInsertAfter->pNext->pPrevious = pNewNode; 749 750 // 3) adjust previous item 751 // so that it points to the new node 752 pNodeInsertAfter->pNext = pNewNode; 753 754 // 4) adjust last item, if necessary 755 if (pList->pLast == pNodeInsertAfter) 756 pList->pLast = pNewNode; 757 758 (pList->ulCount)++; 759 } 760 else 761 { 762 // item index too large: append instead 763 free(pNewNode); 764 pNewNode = lstAppendItem(pList, pNewItemData); 765 } 766 } 740 // 1) set pointers for new node 741 pNewNode->pPrevious = pNodeInsertAfter; 742 pNewNode->pNext = pNodeInsertAfter->pNext; 743 744 // 2) adjust next item 745 // so that it points to the new node 746 if (pNodeInsertAfter->pNext) 747 pNodeInsertAfter->pNext->pPrevious = pNewNode; 748 749 // 3) adjust previous item 750 // so that it points to the new node 751 pNodeInsertAfter->pNext = pNewNode; 752 753 // 4) adjust last item, if necessary 754 if (pList->pLast == pNodeInsertAfter) 755 pList->pLast = pNewNode; 756 757 (pList->ulCount)++; 767 758 } 768 } 759 else 760 { 761 // item index too large: append instead 762 free(pNewNode); 763 pNewNode = lstAppendItem(pList, pNewItemData); 764 } 765 } 766 } 769 767 770 768 return (pNewNode); … … 797 795 BOOL fFound = FALSE; 798 796 799 if ( pList)800 if ((pList->ulMagic == LINKLISTMAGIC)801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 797 if ( (pList) 798 && (pList->ulMagic == LINKLISTMAGIC) 799 && (pRemoveNode) 800 ) 801 { 802 if (pList->pFirst == pRemoveNode) 803 // item to be removed is first: adjust first 804 pList->pFirst = pRemoveNode->pNext; // can be NULL 805 if (pList->pLast == pRemoveNode) 806 // item to be removed is last: adjust last 807 pList->pLast = pRemoveNode->pPrevious; // can be NULL 808 809 if (pRemoveNode->pPrevious) 810 // adjust previous item 811 pRemoveNode->pPrevious->pNext = pRemoveNode->pNext; 812 813 if (pRemoveNode->pNext) 814 // adjust next item 815 pRemoveNode->pNext->pPrevious = pRemoveNode->pPrevious; 816 817 // decrease list count 818 pList->ulCount--; 819 820 // free node data 821 if (pList->fItemsFreeable) 822 if (pRemoveNode->pItemData) 823 free(pRemoveNode->pItemData); 824 // free node 825 free(pRemoveNode); 826 827 fFound = TRUE; 828 } 831 829 832 830 return (fFound); … … 852 850 BOOL lstRemoveItem(PLINKLIST pList, void* pRemoveItem) 853 851 { 854 BOOL brc = FALSE; 855 856 PLISTNODE pNode = lstNodeFromItem(pList, pRemoveItem); 857 858 if (pNode) 859 brc = lstRemoveNode(pList, pNode); 860 861 return (brc); 852 PLISTNODE pNode; 853 854 if (pNode = lstNodeFromItem(pList, pRemoveItem)) 855 return (lstRemoveNode(pList, pNode)); 856 857 return (FALSE); 862 858 } 863 859 … … 875 871 PLISTNODE pNode2) 876 872 { 877 BOOL brc = FALSE;878 879 873 if ( (pNode1) && (pNode2) ) 880 874 { … … 883 877 pNode2->pItemData = pTemp; 884 878 885 brc = TRUE;879 return (TRUE); 886 880 } 887 881 888 return ( brc);882 return (FALSE); 889 883 } 890 884 … … 907 901 { 908 902 long ll = lLeft, 909 lr = lRight -1,903 lr = lRight - 1, 910 904 lPivot = lRight; 911 905 … … 919 913 { 920 914 // compare left item data to pivot item data 921 while ( (*pfnSort)(pNodeLeft->pItemData,922 923 915 while ( pfnSort(pNodeLeft->pItemData, 916 pNodePivot->pItemData, 917 pStorage) 924 918 < 0 ) 925 919 { … … 930 924 931 925 // compare right item to pivot 932 while ( ( (*pfnSort)(pNodeRight->pItemData,933 934 926 while ( ( pfnSort(pNodeRight->pItemData, 927 pNodePivot->pItemData, 928 pStorage) 935 929 >= 0 ) 936 930 && (lr > ll) … … 953 947 954 948 // recurse! 955 lstQuickSort2(pList, pfnSort, pStorage, lLeft, ll-1); 956 lstQuickSort2(pList, pfnSort, pStorage, ll+1, lRight); 949 lstQuickSort2(pList, pfnSort, pStorage, 950 lLeft, 951 ll - 1); 952 lstQuickSort2(pList, pfnSort, pStorage, 953 ll + 1, 954 lRight); 957 955 } 958 956 } … … 994 992 BOOL brc = FALSE; 995 993 996 if ( pList)997 if ((pList->ulMagic == LINKLISTMAGIC)998 999 1000 1001 long lRight = lstCountItems(pList)-1;1002 1003 1004 1005 1006 1007 994 if ( (pList) 995 && (pList->ulMagic == LINKLISTMAGIC) 996 && (pfnSort) 997 ) 998 { 999 long lRight = lstCountItems(pList) - 1; 1000 1001 lstQuickSort2(pList, pfnSort, pStorage, 1002 0, // lLeft 1003 lRight); 1004 brc = TRUE; 1005 } 1008 1006 1009 1007 return (brc);
Note:
See TracChangeset
for help on using the changeset viewer.