2012-02-11 25 views
1

所以我試圖執行以下;列表 - C(作業)

// Destructive Abstract data type ilist 

    struct ilist_ADT; 
    typedef struct ilist_ADT *ilist; 
    // prototype for secret implementation 
    // do not rely on ilist being a pointer 

    ilist iempty(); 
    // returns an empty ilist 

    int iempty_huh(ilist il); 
    // returns 1 (true) if il is empty 
    // returns 0 (false) if il is not empty 

    int ifirst(ilist il); 
    // returns the first element in il 
    // il must not be empty 

    ilist icons_destroy(int in, ilist il); 
    // returns an ilist with in added as the first element of il 
    // references to il cease to be valid ilists 
    // the result must eventually be consumed by one of: 
    //  icons_destroy, irest_destroy, idelete 

    ilist irest_destroy(ilist il); 
    // modifies il to remove the first element, and returns the modified ilist 
    // frees the memory associated with the first element 
    // references to il cease to be valid ilists 
    // the result (if non-empty) must eventually be consumed by one of: 
    //  icons_destroy, irest_destroy, idelete 

    ilist icopy(ilist il); 
    // returns a new copy of il that continues to be a valid 
    // ilist with the same elements even when il is destroyed 
    // the result must eventually be consumed by one of: 
    //  icons_destroy, irest_destroy, idelete 

    int ilength(ilist il); 
    // computes the number of elements in il 

    void idelete(ilist il); 
    // frees the storage for ilist 
    // all further references to il become invalid 
    // NOTE: every ilist created by icons_destroy or 
    //   irest_destroy or icopy must eventually be destroyed 
    //   by being consumed by icons_destroy or 
    //   irest_destroy or idelete 

我專注於圖標第一,和我有一個非破壞性的圖標,如:

ilist icons(int in, ilist il) {  
    ilist r = malloc(sizeof(struct ilist_ADT)); 
    r->first = in; 
    r->rest = il; 
    r->length = 1 + ilength(il); 
    return r; 

} 

究竟是如何將我讓這個以適應執行?換句話說,我該如何破壞它?

+5

我在這裏找不到問題。你可以問一個簡單的問題,在這裏提出整個問題,而不是在其他地方提到頁面? – ugoren 2012-02-11 20:07:00

+0

更新的問題 – Thatdude1 2012-02-12 01:30:42

+0

什麼是破壞性圖標?我們可以改變返回類型嗎? – sehe 2012-02-12 01:36:11

回答

1

「破壞性」意味着允許修改函數(或oop方法中的接收方)的參數。非破壞性函數意味着它不會修改其參數,而是返回修改後的副本。允許破壞性函數的優點是,您不必創建該副本,因此您通常需要更少的malloc(在此示例中,但數字相同)。

icons實施你提供,你可以看到,con'ing條目保持原有ilist il使用ilist il不會注意到你做了什麼有效的未修改列表!:任何人(只用單鏈表工作課程)。

破壞性的實現首先允許你修改的參數,但它也意味着,你應該修改參數以有意義的方式:你應該修改它使得人誰仍與參考原件ilist il請參閱您的修改。你可以這樣做,像這樣:

// observation: the names are not the best, i would call it destructiveICons 
// (and ->first should be called ->value) 
ilist icons_destroy(int in, ilist il) { 
    ilist second = malloc(sizeof(struct ilist_ADT)); 
    second->length = il->length 
    second->first = il->first 
    second->rest = il->rest; 
    il->length += 1; 
    il->rest = second; 
    il->first = in; 
    return il; 
} 

現在別人誰擁有裁判ilist il將看到新的,第一個元素之後的老以前第一要素。

0

我認爲這個實現可以被看作是破壞性的(儘管我並不真正理解「破壞性」的意義)。
當使用動態分配的數據結構(不收集垃圾的語言)時,最重要的問題是誰最終負責釋放數據。使用icons實現時,必須使用新手柄完成發佈,而不要使用舊手柄。在這個意義上,舊的句柄不再有效。

你可以改變的東西,所以舊的句柄將真的無法使用。您可以添加一個字段,指示ilist是否是鏈中的第一個,並在添加/刪除元素時進行維護。然後,如果給予他們的句柄不是第一個,那麼你的所有API都可以拒絕工作。