It's really easy:
myList.Clear();
Answer from Øyvind Bråthen on Stack OverflowIt's really easy:
myList.Clear();
If by "list" you mean a List<T>, then the Clear method is what you want:
List<string> list = ...;
...
list.Clear();
You should get into the habit of searching the MSDN documentation on these things.
Here's how to quickly search for documentation on various bits of that type:
- List Class - provides the
List<T>class itself (this is where you should've started) - List.Clear Method - provides documentation on the method Clear
- List.Count Property - provides documentation on the property Count
All of these Google queries lists a bundle of links, but typically you want the first one that google gives you in each case.
Is there an "Empty List" singleton in C#? - Stack Overflow
function - Trying to create an empty linked list in C - Stack Overflow
Any good reason to allow for empty linked list?
How to check if a linked list is empty in C - Stack Overflow
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.
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
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.
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).
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.