It's really easy:

myList.Clear();
Answer from Øyvind Bråthen on Stack Overflow
🌐
Medium
medium.com › @martinstm › c-shorts-the-best-way-to-create-empty-collections-6fd00bcec1f3
C# Shorts: The best way to create empty collections | by Tiago Martins | Medium
February 19, 2025 - IReadOnlyList<string> readOnlyList = ReadOnlyCollection<string>.Empty; Ensures the collection cannot be modified.
Discussions

Is there an "Empty List" singleton in C#? - Stack Overflow
In C# I use LINQ and IEnumerable a good bit. And all is well-and-good (or at least mostly so). However, in many cases I find myself that I need an empty IEnumerable as a default. That is, I More on stackoverflow.com
🌐 stackoverflow.com
function - Trying to create an empty linked list in C - Stack Overflow
I'm trying to create an empty linked list, which asks the user for the maximum number of terms that the list can hold. (I didn't add my code for that as its simply a printf). I then have to create ... More on stackoverflow.com
🌐 stackoverflow.com
Any good reason to allow for empty linked list?
Suppose the bank has a list of debts that you have to pay. It’s a linked list. What happens when you pay the last debt? What kind of list do you have now? I would think that you have an empty list… otherwise you will owe money to the bank forever. More on reddit.com
🌐 r/C_Programming
82
6
November 24, 2023
How to check if a linked list is empty in C - Stack Overflow
I have this code: I try to initialize a pointer and after i use it to check if my list is empty #include #include typedef long T; typedef struct SortedList { T More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/csharp › best way to create an empty collection (array and list) in c# (.net)
r/csharp on Reddit: Best way to create an empty collection (array and list) in C# (.NET)
August 4, 2020 - For creating a empty collection, I always did var myList = new List<myobject>(); Never gave it much thought on what was more efficient.
🌐
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.
🌐
Wordpress
ianvink.wordpress.com › 2022 › 08 › 01 › c-how-to-return-an-empty-list-the-right-way
C#: How to return an empty list the right way (And save #Azure $) | Asp.Net Core, Angular, Xamarin and Maui
August 1, 2022 - They all work, but some fill the Heap and then need to be garbage collected, and others don't. Here's a common way to return a list of Cats. return new List (); Works perfectly well, however, the Heap will be…
Top answer
1 of 3
1

In C, linked lists are usually implemented as a series of nodes stored on the heap that point to eachother. The heap is a persistent memory area that runs throughout the life-cycle of the program.

When you create a variable normally in a C function, and the function returns, the variable that you created is no longer accessible. However when you create something on the heap in a function, and the function is returned, the data you allocated on the heap is still there. However, you have no way of accessing it-- unless the function returns a pointer.

So what you would do for create_q() would be to create the linked list on the heap (using a function in stdlib.h called "malloc"), and then you would return a pointer to your first node, letting the main function know where on the heap to find the first node. Then that first node would have a pointer in it, telling the program where on the heap to find the second node, and so forth.

However, you're probably approaching linked lists the wrong way. Unless this is for some sort of homework project, you probably wouldn't want to create an empty linked list. One of the benefits of a linked list is that it's a dynamic structure in which you can easily insert new nodes. You could still have some variable keeping track of the maximum size you want the list to be, but you probably wouldn't want to actually create the nodes until you had to.

Just keep in mind what a linked list is. It's a set of nodes floating on the heap (in C) that each store some data, and contain a pointer to the next node floating on the heap. All you need, to access the linked list, is a pointer to the first node. To add a new node, you simply "walk" through the list till you reach the last node, and then create a new node and have the old-last node point to it.

2 of 3
0
Is this what you had in mind?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node_t
{
    int value;
    int priority;

    struct node_t *next;
};

static int current_size;
static int max_size;
static struct node_t* head = NULL;

struct node_t* create_q(int);

struct node_t* create_q(int max_terms)
{
    int i; // loop counter/index

    current_size = max_terms;
    max_size = max_terms;

    if( NULL == (head = malloc(sizeof(struct node_t)*max_terms)))
    { // then, malloc failed
        perror("malloc failed for struct node_t list");
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    // set all fields to '0,0,Null'
    memset( head, 0x00, sizeof(struct node_t)*max_terms);

    // set all next links, except last link
    for(i=0;i<(max_terms-1);i++)
    {
        head[i].next = &head[i+1];
    }

    // set last link
    head[i].next = NULL;

    return( head );
} // end function: create_q
Find elsewhere
🌐
Krishnraj Rana
krishnrajrana.wordpress.com › 2017 › 11 › 20 › tip-of-the-day-different-ways-of-creating-empty-list-of-objects-in-c
Tip of the day: Different ways of creating empty List of Objects in c# – Krishnraj Rana
November 20, 2017 - It has always been fun to play with C#. In this post we will see that how we can create a list of objects with different approach. Well, in this article’s title i have used the word “empty list of objects” which might be confusing for some developers. Actually, empty list mean – create an object but without assigning any value to the object’s property.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › list-empty-function-in-c-stl
list empty() function in C++ STL - GeeksforGeeks
May 29, 2023 - This function does not accept any parameter, it simply checks whether a list container is empty or not.
🌐
TutorialsPoint
tutorialspoint.com › how-to-initialize-a-list-to-an-empty-list-in-chash
How to empty a C# list?
September 6, 2023 - The Clear() method is the most efficient way to empty a list as it removes all elements in a single operation.
🌐
cppreference.com
en.cppreference.com › cpp › container › list › empty
std::list<T,Allocator>::empty - cppreference.com
#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() ...
🌐
Medium
sheldonrcohen.medium.com › avoiding-null-the-case-for-returning-empty-lists-or-arrays-in-c-6b51648ea5ca
Avoiding null: The Case for Returning Empty Lists or Arrays in C# | by Sheldon Cohen | Medium
September 2, 2024 - Beside not having to do a null check, by using an Empty Collection, you simplify the conditional checks, and simplify your code’s flow. Like in the below example, you don’t need a lot of conditional checking in your code, slimming it down and making it easier to read. // Simplified flow: List<string> myList = GetList("Alice"); foreach (var item in myList) { Console.WriteLine(item); // Works even if the list is empty }
🌐
Programming Idioms
programming-idioms.org › idiom › 260 › create-an-empty-list-of-strings › 4819 › csharp
Create an empty list of strings, in C#
var items = new List<string>(); C# var items = Enumerable.Empty<string>(); Clojure · (def items '()) C++ std::vector<std::string> items; Dart · var items = <String>[]; Dart · List<String> items = []; Fortran · character(len=:), allocatable, dimension(:) :: items allocate (items(0),source="") Go ·
🌐
Reddit
reddit.com › r/c_programming › any good reason to allow for empty linked list?
r/C_Programming on Reddit: Any good reason to allow for empty linked list?
November 24, 2023 -

Hello, I've been making a linked list, and talking about it with my dad too, he insists that you should be allowed to make an empty linked list, but I don't think there should be, since there's "no reason to store nothing."

Thoughts?

Edit: feel free to continue posting your thoughts, but after some thought, I'll reconsider allowing an empty list, since having the end user work around this issue would probably make it overall more work. Thank you very much for your input though! I'll let my dad know most of you agree with him 😂

Edit 2: alrighty, I've thought about it, I'll definitely be implementing the support for an empty linked list (though I'll have to rewrite a large chunk of code, rip lol) I definitely enjoyed talking with you guys, and I look forward to finally posting my implementation.

Top answer
1 of 18
52
Suppose the bank has a list of debts that you have to pay. It’s a linked list. What happens when you pay the last debt? What kind of list do you have now? I would think that you have an empty list… otherwise you will owe money to the bank forever.
2 of 18
32
There's no reason to store nothing, but my gosh there absolutely are reasons to represent nothing. This thread is full of them. So the question, then, is how do you want to represent an empty list in your code? How do you signal to the program that sometimes there is a list here, and right now, it's empty? You seem to currently want to represent this empty condition external to the list, by creating and freeing the list on-demand. But consider: now every bit of code you have that has to add something to the list has to first check if there even is a list, and create one if there isn't. And every bit of code that removes something from the list has to check if it's the last element, and free the list if it is. People here are suggesting instead to move your representation of an empty list inside the list. Whatever function or bit of code you hopefully re-use to add or remove items from the list can handle this emptiness internally, so any code that uses your list can ignore this fiddly detail. Which one will be a nicer interface will depend on your code, but there's a reason everybody here is suggesting the second one. In almost all situations it makes reading and writing code easier and less error prone.
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.

🌐
Webdevtutor
webdevtutor.net › blog › c-sharp-create-empty-list
How to Create an Empty List in C#
One of the most common ways to create an empty list in C# is by using the List<T> class from the System.Collections.Generic namespace.
🌐
TutorialsPoint
tutorialspoint.com › empty-list-in-chash
How to remove an empty string from a list of empty strings in C#?
June 22, 2020 - In C#, you can remove empty strings from a list using several methods. Empty strings can be truly empty ("") or contain only whitespace characters (" ").
🌐
Quora
quora.com › How-do-I-create-an-empty-list-in-C-where-the-user-can-freely-add-string-variables-in-it-as-well-as-remove-some-of-the-variables-they-add
How to create an empty list in C++ where the user can freely add string variables in it as well as remove some of the variables they add - Quora
Answer (1 of 2): Use std::list mylist. Unless you give it content on construction, it is initialized to empty. Very handy. It has methods for push_back, push_front and insert/remove. But I presume you want to write your own code. In which case you need to write a class, which has at...
🌐
Medium
medium.com › @martinstm › performance-wars-checking-empty-lists-c-47be0557161e
Performance Wars — Checking Empty Lists — C# | by Tiago Martins | Medium
October 30, 2021 - The worst result was for FirstOrDefault() method from LINQ. Again this method cast the list to IList and checks if the Count is greater than 0, if so returns the first element. If can’t convert to IList calls the MoveNext and return that element. In case of the list be empty, will return null (source code).