flowinwind
flowinwind

Reputation: 69

Empty/Full Linked List

So for my first question. I'm working on a project and I need my constructor to create an empty linked list. Would this be correct?

// 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
}

Second question. I need to create a isEmpty() function and a isFull() function. For the isEmpty() function I just see if howmany == 0, right? But to check if list is FULL how would I do that? For normal arrays there is a CAPACITY, but there is no capacity given for linked lists.

    //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

Upvotes: 0

Views: 2469

Answers (1)

bwinata
bwinata

Reputation: 191

First question:

  • This is fine. Although, you probably want to add a node to keep track of the tail.

Second question

  • isEmpty() can either be done by checking if how_many is zero or simply by checking if headptr == NULL.

  • isFull() depends if you really want to set a limit to the length of the list. Generally speaking, lists can keep going only constrained by the amount of allocated memory available in the system. If your intent is to set a limit, then I would allow for the user to pass in a number through the constructor specifying their list size. From here you can simply use a counter to keep track...

Upvotes: 2

Related Questions