0

我有一个通用列表,它 100% 有效并保持:

/**
 * Generic List Container
 *
 * Implements a list container type.
 * The list his an internal iterator for external use. For all functions
 * where the state of the iterator after calling that function is not stated,
 * it is undefined. That is you cannot assume anything about it.
 *
 * The following functions are available:
 *
 *   listCreate               - Creates a new empty list
 *   listDestroy              - Deletes an existing list and frees all resources
 *   listCopy                 - Copies an existing list
 *   listGetSize              - Returns the size of a given list
 *   listFirst                - Sets the internal iterator to the first element
 *                              in the list, and returns it.
 *   listInsertFirst          - Inserts an element in the beginning of the list
 *   listInsertLast           - Inserts an element in the end of the list
 *   listInsertBeforeCurrent  - Inserts an element right before the place of
 *                              internal iterator
 *   listInsertAfterCurrent   - Inserts an element right after the place of the
 *                              internal iterator
 *   listRemoveCurrent        - Removes the element pointed by the internal
 *                              iterator
 *   listGetCurrent           - Return the current element (pointed by the 
 *                              internal iterator)
 *   listGetFirst             - Sets the internal iterator (also called current 
 *                              element) to be the first element in the list and 
 *                              return it.
 *   listGetNext              - Advances the list's iterator to the next element 
 *                              and return it 
 *   listSort                 - Sorts the list according to a given criteria
 *   listFilter               - Creates a copy of an existing list, filtered by
 *                              a boolean predicate
 *   listClear            - Clears all the data from the list
 */

我提供

  /** Element data type for list container */
typedef void* ListElement;

/** Type of function for copying an element of the list */
typedef ListElement (*CopyListElement)(ListElement);

/** Type of function for deallocating an element of the list */
typedef void (*FreeListElement)(ListElement);

List listCreate(CopyListElement copyElement, FreeListElement freeElement);

现在,我想使用这个列表来保存指向数据的指针,而不是数据本身。例如,我想要一个类型的结构列表

  struct CASH_element {
        int id;
        int passengers;
        int unpaidPassengers;
        bool reset;
        int profit;
    } Cash;

    struct Transportation_element {
        List cashes; //list of cashes
        int travels;
        int profit;
    };

所以当我创建我使用的运输类型时:

    Transportation* systemToAdd;
    systemToAdd->cashes = listCreate(cashCopy, DestroyCash);
    /* MORE CODE*/ 

现在,有趣的函数是 copyElement 函数:

  void* cashCopy(void* cashCopy) {
    Cash* toCopy = (Cash*) cashCopy;
    if (toCopy == NULL) {
        return NULL;
    }

    Cash* toAdd =  (Cash*) malloc(sizeof(*toAdd));
    if (toAdd == NULL) {
        return NULL;
    }

    toAdd->id = toCopy->id;
    toAdd->passengers=toCopy->passengers;
    toAdd->unpaidPassengers = toCopy->unpaidPassengers;
    toAdd->reset=toCopy->reset;
    toAdd->unpaidPassengers=toCopy->unpaidPassengers;
    toAdd->profit=toCopy->profit;

    return  (Cash*)  toAdd;;

显然,这会按价值复制现金及其所有数据,一旦我独立更新现金,列表节点就不会更新。我希望它被更新。我想我必须使用指针。我试过修改函数但失败了:

  void* cashCopy(void* cashCopy) {

        return  (Cash*)&cashCopy;
   }

   and adding is straightforward:
     TransportationStatus trAddCash(Transportation* system, Cash c) {
        if (system == NULL) {
            return TR_FAIL;
        }
    Cash* cash=&c;
    if (LIST_SUCCESS != listInsertLast(system->cashes, cash)) {
        return TR_FAIL;
    }

    return TR_SUCCESS;

   }

有一个我想要工作的代码示例:

 cs1 = cashCreate(&c1, 123);
    Transportation trans1;
        TransportationStatus tr1;
    tr1=trCreate(&trans1);
    ASSERT_TEST(tr1==TR_SUCCESS);

    cs1 = cashTravel(&c1, &rq1, 660, "09/08/2011-02:51"); //THE FIRST TRAVEL
    tr1=trAddCash(&trans1,c1); //ADD THE CASH TO THE TRANSPORTATION STRUCT
    ASSERT_TEST(tr1==TR_SUCCESS);
    tr1=getTotalTravels(trans1, &result);
    ASSERT_TEST(result==1); //THAT'S OKAY BECAUSE TRAVEL BEFORE COPYING
    cs1 = cashTravel(&c1, &rq1, 660, "13/08/2011-02:51"); //THE SECOND TRAVEL
   tr1=getTotalTravels(trans1, &result);
   ASSERT_TEST(result==2); //FAIL!!!!
      cashTravel(listGetFirst(trans1.cashes),  &rq1, 660, "13/08/2010-02:51"); //TRAVEL WITH THE LIST   NODE
        tr1=getTotalTravels(trans1, &result);
  ASSERT_TEST(result==2); //SUCCESS!!!!

所以基本上,我想修改我的复制功能,以便我可以独立更新节点

4

1 回答 1

1

尽管您的代码“有效”,但它非常复杂。现在您发现“工作”并不是代码的主要优点——您需要有足够简单的东西,以便当需求发生变化时,您可以更改代码或编写新代码。

完全放弃此代码,并使用您所学的知识来制作一个的列表模块,您可以轻松地对其进行修改以保存指针。

提示:你怎么知道谁的责任mallocfree那些指针?

于 2012-08-13T01:00:14.960 回答