【发布时间】:2015-02-11 12:32:16
【问题描述】:
所以对于我的第一个问题。我正在做一个项目,我需要我的构造函数来创建一个空链表。这是正确的吗?
// 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
【问题讨论】:
-
第一个问题:看起来不错。第二个问题:一个普通的链表不可能是满的(除了满的计算机 RAM、int 溢出等)。你认为你想检查什么?
-
嗯,你的编辑很清楚。如果您无法分配,则为 true。而且您可能也应该检查 int。
-
还在后置条件中使用术语对象而不是列表。这是一个错字还是它实际上意味着对象?
-
当元素数量等于
std::numeric_limits<size_type>::max()时,您可以说列表已满?我不会打扰 -
创建一个空列表真的很容易:
void* list = nullptr。当您希望能够向其中添加内容时,它会变得[更]复杂。
标签: c++ list pointers linked-list