text 如何在C中创建单个链接列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 如何在C中创建单个链接列表相关的知识,希望对你有一定的参考价值。

Create a linked list:

So we want to make a function that creates a linked list from scratch, something look like this:

sllnode* create(VALUE val);

Inside the bracket, (VALUE val) is the initial data value that this going to be put into the first element that we are creating for this linked list. 

So it could look something like this:

sllnode* new = create(6);  // So we are going to create a linked list, and put "6" into the 'val' field for the first member of the linked list that we will create.

And then this function returns a pointer "sllnode*" which tells you where is the start of this linked list. Because all we need to know is the start point, all members are linked, so once we know the start address then we can trace down to access all other members in this linked list.

sllnode* new = create(6);  // So in this example, 'new' is the variable name for the pointer that we are going to return. When we write:" sllnode* new " that means 'new' is a pointer variable that is of the type 'sllnode*'.

So the steps that we need to do for creating a new linked list are:

a. Dynamically allocate a spece for a new sllnode.

	So we use malloc() to allocate space for the first member of this linked list that we have created.

b. ofcourse when we use malloc() to allocate memory space we always need to check to make sure we didn't run out of memory. That means make sure the returned pointer is not NULL.

c. Initialize the node's val field. Like we put "6" into the value field of this first node.

d. Initialize the node's next field. In this case, we are creating the very first member of this linked list, so 'next' field shouldn't be pointing to anywhere. Therefor it should be "NULL". And we can see later on, when we insert new nodes into this linked list, we always insert it to the front of the previous one. So actually this very first node that we created will always be the last one in this list, and the 'next' field will always be "NULL".

e. Return a pointer to the newly created sllnode.

	In this example we are returning the variable named 'new' which is a pointer of the 'sllnode*' type.



以上是关于text 如何在C中创建单个链接列表的主要内容,如果未能解决你的问题,请参考以下文章

Swift Combine:如何从发布者列表中创建单个发布者?

如何在 Perl 中创建 Zip 存档?

如何在单个 spfx 解决方案中创建两个 webpart?

如何在颤动中创建加载更多列表视图

如何查看从电子商务中创建单个产品详细信息的页面

如何编辑列表中的每个项目并在 dart 中创建一个新项目