Tanya Tazzy Hegarty
Tanya Tazzy Hegarty

Reputation: 111

Checking if a singly linked list is empty c++

I am trying to figure out what the best and simplest way is of determining if a singly linked list is empty or not.

Would I need to create a boolean method?

Thanks

Read Method

void List::Read(istream& r) {

char c[13];
r >> c;
r >> numberOfInts;

Node *node = new Node();

for(int i = 0; i < numberOfInts; i++)
{
    r >> node->data;
    cout << node->data << endl;
    node->next = new Node;
    //node = node->next;

    head = node;
}
}
else
{
    if(node->data > head->data)
    {
        head->next;
    }
    else if(node->data < head->data)
    {
        Node* tempNode;
        tempNode = head;
        head->data = node->data;
        node->data = tempNode->data;
    }
}
system("pause");

}

Header file

class Node
{
public:
    Node() {}
    Node(int d, Node* q = 0) : data(d), next(q) {} //constructor with parameters data and next
    int data; //holds data in node
    Node* next;//pointer to next node
};

class List
{
public:
    void Read(istream&);
    void Write(ostream&);

    void setReadSort(bool);
    void sortOwn();
    void insertionSort(Node*);
    bool isEmpty();

    bool _sortRead;
    int numberOfInts;

    List(void);
    ~List(void);
protected:
    Node *head;
    Node current;
};

Upvotes: 1

Views: 9967

Answers (3)

Charles Ray
Charles Ray

Reputation: 480

I didn't test this, but it should work.

[edit] Changed program to account for a head that always exists.

[edit] Changed program to account for class, instead of a struct

bool isEmpty(){return (this->head == NULL)};

This will give a more proper result

One more thing, I see that you are using system("pause");. I know that this is homework, but that is extremely bad practice. A better method would be to first clear the buffer in stdin (if needed), then ignore a character. An example would be:

cin >> somenumber; // Extract a number (assuming its safe)
cin.ignore(1, 10); // Ignore 1 character, or a newline, whichever comes first
                   // ^ this clears the buffer, it does NOT wait for input since
                   // the formatted extraction left a character in the buffer ('\n')
cin.ignore();      // Essentially the same as above, but since there is nothing in
                   // the buffer, it reads a single character from stdin

Upvotes: 0

Naszta
Naszta

Reputation: 7744

Yes. The reason is: sometimes we use the iterator to insert element, it would be very uncomfortable (or impossible) to handle the number of the elements over the iterators. This is the reason why many STL implementation has linear time for size_t size(void) function. bool empty(void) is an effective way to check if the list is empty and it has constant time.

Just to note: when you use STL prefer to use bool empty(void) against size_t size(void). More info in: Meyers: Effective STL.

The function is simple:

bool empty( void ) const
{
  return ( this->begin() == this->end() );
}

In your case:

bool empty( void ) const
{
  return ( this->head == 0 );
}

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564403

This completely depends on the implementation. However, this typically can be done very quickly by checking to see if the first node exists/has contents/etc.

Upvotes: 2

Related Questions