text 将新节点插入单个链接列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 将新节点插入单个链接列表相关的知识,希望对你有一定的参考价值。

Insert a new node into the linked list:

So we will make a function that does the insert operation. And because we always insert the new node at the beginning of the linked list, therefore the new inserted node will become the new head of the linked list. So this function will return a pointer which points to the new head of the linked list.

	sllnode* insert(sllnode* head, VALUE val); // obviously inside () are the pointer that points to the head of the linked list, and the value that we want to insert into the new node.

It will look like something like this:  list = insert(list, 12);

The reason why we would prefer to insert at the begining of the list is because: to be able to access each node in this linked list, we always have to start from the head of the linked list because the only way we refer to a linked list is the pointer that points to the beginning of the list. Like mentioned before, the difference of array and linked list, is the for linked list we always start from the pointer that points to the beginning of the list and then follow the chain to reach each node inside the list. We can't get random access of nodes like in an array. For an array we can say Array[8], so we can directly access a member in the middle of the array. But we can't do that with linked lists. We have to start from the very beginning, and then track down one by one.

So because of the above reason, if we want to insert a new node at the end of the list, we would have to start from the beginning, and go through the whole list and reach the end. So it's like taking "n" steps to complete this operation. But if we insert at the beginning of the list, we can do that right away.


So now, here are the steps involved for insert a new node:

a. Dynamically allocate space for a new sllnode.
b. Check to make sure we didn't run out of memory.
c. Populate and insert the node at the begining of the linked list.
d. Return a pointer to the new head of the linked list.

One very important thing that you have to consider for a linked list, is to arrange the pointers in the linked list in the correct order. The correct order is very important. If the order is not correct, we could end up "orphaning" some of the nodes.

In case of inserting a new node into the list, first we malloc() a memory space for the new node and make sure the pointer doesn't return NULL. Now we have this new node with it's own memory address. Do we first move the pointer that points to the head of the list to this new node? Although we have a new head pointer, but after we have done this, we have lost the record of the memory address of the old head of the list. The "next" section of this new node is suppose to point to the old head of the list, but now we don't know what that memory address is anymore. So now we have orphaned the new node.

So the correct order of doing this, is to have "next" section of the new node points to the old head of the list first. And now this new node is linked with the rest of the list. After that, we move the pointer that points to the head of the list to the new node. So now we have a new memory address for the pointer that points to the head of the list.

以上是关于text 将新节点插入单个链接列表的主要内容,如果未能解决你的问题,请参考以下文章

在给定节点之后将新节点插入到双向链表中

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

Django:我可以通过单个方法调用将新模型实例列表添加到数据库吗?

c_cpp 链接列表|设置2(插入节点)

在 Flutter 中按下按钮时将新的小部件作为列表插入

HTML--三种样式插入方法--链接---表格---列表