Intro:

There are usually two ways to use linked lists: with a root element and without.

Without a root, your list pointer is NULL when the list is empty:

node *list;
...
if (list == NULL) { /* empty list */ }

With root, there is always one element. But it can be used in two ways:

Either simply to provide a pointer to the first element.

node *root;
...
if (root->next == NULL) { /* empty list */ }

Or to have the last element link back to the root to form a cycle. This concept has the advantage that the "next" element is never NULL and you thus don't need to check for it. In this case, the list is empty if the root points back to itself.

node *root;
...
if (root->next == root) { /* empty list */ }

Answer:

Now, according to your description, you have allocated a node. This either means you want the "root" approach (second or third example). But if you want to go with the first variant, you must not allocate the node, since it doesn't hold data.

For the "root" approach, you indeed have one (and only one) node that doesn't hold data. But for simple linked lists, all nodes must contain data.

Answer from DarkDust on Stack Overflow
Top answer
1 of 2
1

First of all you don't need double pointer. Initialization with NULL is important here. (Denotes the empty list).

SortedList *list = NULL;

This has the benefit that now if you want to make changes to it pass it's address.

func(&list);

And checking the empty list would be

   void func(SortedList **list){
      if( *list == NULL )
        /* empty */
   }

This is usually the way lists are implemented.


In your case you simply allocated to a local variable and instead of returning the memory address you have overwritten the pointer's value with NULL - there is memory leak in the code.

With your code it will be something like. Yes all that the initialization does is assigning it to NULL. This is one easy way out towards checking the NULL. At the very beginning making it NULL basically denotes that list is empty.(which is the case generally).

2 of 2
0

list is a pointer to a pointer. The actual list is in the value it points to, so you need to dereference it.

An empty list is just a null pointer, so you don't need to allocate anything in init().

void init(SortedList** list)
{
    *list=NULL;
    printf("%p\n", *list);
}
void isEmpty(SortedList** list)
{
    printf("%p\n", *list);
    if(*list == NULL) printf("List is empty\n");]
}

And in the main function, you don't need a double pointer, you use the & (address-of) operator to get the address of a pointer variable.

int main()  
{
    SortedList *list;
    init(&list);
    printf("Initialization with succes\n");
    isEmpty(&list);

    return 0;
}

You only really need double pointers for functions that modify the list. So you could use an ordinary pointer for isEmpty:

void isEmpty(SortedList* list)
{
    printf("%p\n", list);
    if(list == NULL) printf("List is empty\n");]
}

and then call it as:

isEmpty(list);

But it's probably simplest to be consistent across all your functions.

🌐
Cplusplus
cplusplus.com › reference › list › list › empty
std::list::empty
Returns whether the list container is empty (i.e. whether its size is 0). This function does not modify the container in any way.
🌐
Bubble
forum.bubble.io › questions
Determining if List is Empty - Questions - Bubble Forum
February 6, 2019 - What’s the best (fastest) way to determine if a list is empty - checking if “count = 0” or if “:first item is empty”? Is there another way? I initially thought the list itself could be checked if empty since the “is empty” operator is available, but apparently that can’t be done.
🌐
Quora
quora.com › What-is-the-best-way-to-check-if-a-linked-list-is-empty-or-not-in-C
What is the best way to check if a linked list is empty or not in C++? - Quora
Usually, you will use the Standard Template Library, where a linked list is implemented as std::list. In this case, you can just use the empty() method that returns true if the list is empty and false if it is not empt...
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 156691 › linked-lists-find-length-and-check-for-empty-list
Linked Lists: Find length and check for empty list - c++
In printList you currently allocate a node and then overwrite the pointer; drop that allocation to avoid a leak and just iterate from first. Your insertBack uses a local first that is never passed in; pass both first and last by reference so you can handle the empty-list case. In deleteFront, do not test first->info == 1; instead, check first != NULL before dereferencing, unlink the head, delete it, and set last = NULL if the list becomes empty.
🌐
Cppreference
en.cppreference.com › w › cpp › container › list › empty
std::list<T,Allocator>::empty
Checks if the container has no elements. ... #include <list> #include <iostream> int main() { std::list<int> numbers; std::cout << std::boolalpha; std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n'; numbers.push_back(42); numbers.push_back(13317); std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n'; }
🌐
SourceBae
sourcebae.com › home › how do i check if a list is empty?
How do I check if a list is empty? - SourceBae
August 21, 2025 - It may seem like a simple task, but overlooking an empty list can lead to errors and unexpected behavior in your code. In this blog post, we will delve into the importance of checking if a list is empty, explore different methods to accomplish this task, and provide examples and code snippets ...
🌐
OpenGenus
iq.opengenus.org › check-if-linked-list-is-empty
Check if Linked List is Empty
November 27, 2020 - List contains:"<<" "; printList(list); } // Inserting some elements in list push(&list,8); push(&list,4); if(isEmpty(&list)) cout<<"List is Empty"<<endl; else{ cout<<"List is not empty! List contains:"<<" "; printList(list); } return 0; } ... Complexity: The complexity to check whether the ...
Find elsewhere
🌐
Javatpoint
javatpoint.com › post › cpp-list-empty-function
C++ List empty() Function - Javatpoint
C++ List empty() function checks whether the list is empty or not. It returns true, if the list is empty otherwise false. It does not modify the content of the list · It does not contain any parameter
🌐
Maixiangyuan
maixiangyuan.ca › post › how-to-check-if-a-list-is-empty-c
how to check if a list is empty c
January 18, 2025 - Determining whether a list is empty in C requires different approaches depending on whether you're using an array or a linked list. Using a size variable for arrays and checking the head pointer for linked lists provides the most reliable and ...
🌐
TutorialsPoint
tutorialspoint.com › list-empty-function-in-cplusplus-stl
list empty() function in C++ STL
In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards. list::empty() is an inbuilt function in C++ STL which is declared in header file. list::empty() checks whether the given ...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › list-empty-function-in-c-stl
list empty() function in C++ STL - GeeksforGeeks
May 29, 2023 - It returns True is the size of the list container is zero otherwise it returns False. The below program illustrates the list::empty() function. ... // CPP program to illustrate the // list::empty() function #include <bits/stdc++.h> using namespace ...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › how-to-check-if-list-is-empty-in-cpp
How to Check if a List is Empty in C++? - GeeksforGeeks
July 23, 2025 - To check if a std::list is empty or not, we can use the std::list::empty() function that returns true if the list is empty and returns false if the list is not empty.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 744707 › how-can-i-find-if-particular-index-in-a-list-is-em
How can I find if particular index in a list is Empty ? - Microsoft Q&A
Add comment · Answer accepted by question author · P a u l • · 10,766 Reputation points · 2022-02-21T19:08:12.127+00:00 · If you want to check if the zero'th element of every list is an empty string you could use .All(): bool isEmpty = ...
🌐
Narkive
comp.lang.lisp.narkive.com › Mz5P3P6Z › how-to-check-if-a-list-is-empty-or-nil
how to check if a list is empty or nil?
Or list if you realize you are not doing scheme. kt huh? 1. nil is false 2. there is no need to mangle the name 3. 1 and 2 are false in Scheme kt ... Post by ssecorp (not (= lst nil)) (/= lst nil) = and /= are for numbers. EQ is the predicate you should use if you want to compare with NIL. Post by ssecorp empty?, list?, nil?
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-check-if-empty-array-in-c
How to Check if Empty Array in C? - GeeksforGeeks
July 23, 2025 - On our machine, the int data type consists of 4 bytes and the above array has 5 elements inside it, so the total size of the array is 5 x 4 = 20 bytes. Now, we will create an array by specifying only its size. We will not insert any elements into it. Look at the below program: ... // C Program to check // above condition #include <stdio.h> int main() { int arr[5] = {}; printf("Size of the array: %d bytes\n", sizeof(arr)); return 0; }