簡體   English   中英

空/全鏈接列表

[英]Empty/Full Linked List

所以對於我的第一個問題 我正在一個項目上,需要構造函數創建一個空的鏈表。 這是正確的嗎?

// member variables
private:
   node* headptr;
   size_type howmany;

//constructor
SuperList::SuperList(){
    headptr = NULL; //does this create an empty list?
    howmany = 0; //counts how many nodes are in the list
}

第二個問題。 我需要創建一個isEmpty()函數和一個isFull()函數。 對於isEmpty()函數,我只看howmany == 0,對嗎? 但是要檢查清單是否完整,我該怎么做? 對於普通陣列,有一個容量,但沒有為鏈表提供容量。

    //these are the specifications I was given for the isFull function

    Effect: Determines if this list is full
    Precondition:  NONE
    Postcondition:  this object is unchanged
    Returns:  true if it is not possible to allocate any more memory, false otherwise

    bool SuperList::isFull() const

第一個問題:

  • 這可以。 雖然,您可能想要添加一個節點來跟蹤尾部。

第二個問題

  • isEmpty()可以通過檢查來完成how_many是零或簡單地通過檢查是否headptr == NULL

  • isFull()取決於您是否真的要設置列表長度的限制。 一般而言,列表只能繼續受到系統中可用的已分配內存量的限制。 如果您打算設置一個限制,那么我將允許用戶通過構造函數傳遞一個數字,以指定其列表大小。 從這里您可以簡單地使用計數器進行跟蹤...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM