1. <tfoot id='uzJQh'></tfoot>
      <legend id='uzJQh'><style id='uzJQh'><dir id='uzJQh'><q id='uzJQh'></q></dir></style></legend>
      • <bdo id='uzJQh'></bdo><ul id='uzJQh'></ul>
      <i id='uzJQh'><tr id='uzJQh'><dt id='uzJQh'><q id='uzJQh'><span id='uzJQh'><b id='uzJQh'><form id='uzJQh'><ins id='uzJQh'></ins><ul id='uzJQh'></ul><sub id='uzJQh'></sub></form><legend id='uzJQh'></legend><bdo id='uzJQh'><pre id='uzJQh'><center id='uzJQh'></center></pre></bdo></b><th id='uzJQh'></th></span></q></dt></tr></i><div id='uzJQh'><tfoot id='uzJQh'></tfoot><dl id='uzJQh'><fieldset id='uzJQh'></fieldset></dl></div>

      <small id='uzJQh'></small><noframes id='uzJQh'>

    2. 删除链表中的所有节点

      时间:2023-10-18
        <bdo id='iNB0L'></bdo><ul id='iNB0L'></ul>

        <small id='iNB0L'></small><noframes id='iNB0L'>

          <i id='iNB0L'><tr id='iNB0L'><dt id='iNB0L'><q id='iNB0L'><span id='iNB0L'><b id='iNB0L'><form id='iNB0L'><ins id='iNB0L'></ins><ul id='iNB0L'></ul><sub id='iNB0L'></sub></form><legend id='iNB0L'></legend><bdo id='iNB0L'><pre id='iNB0L'><center id='iNB0L'></center></pre></bdo></b><th id='iNB0L'></th></span></q></dt></tr></i><div id='iNB0L'><tfoot id='iNB0L'></tfoot><dl id='iNB0L'><fieldset id='iNB0L'></fieldset></dl></div>

            <tbody id='iNB0L'></tbody>

              • <legend id='iNB0L'><style id='iNB0L'><dir id='iNB0L'><q id='iNB0L'></q></dir></style></legend><tfoot id='iNB0L'></tfoot>
                本文介绍了删除链表中的所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个链表包含 3 个节点,如图所示:

                I have a linked list contains 3 nodes like the image shown:

                有一个head指针和temp1指针指向链表的前端,尾点指向链表的尾部.

                There is a head pointer and temp1 pointer point to the front of the list, and tail point points at the end of the list.

                我想删除所有节点,并将其改回原来的初始形式( tail = NULL, head = first_node ,但第一个节点在数据和下一个字段中没有任何值).

                I want to remove all the nodes, and change it back to its original initial form ( tail = NULL, head = first_node , but the first node doesn't have any value in the data and next field).

                因为我想开始在其中添加一些新值.要删除所有这些数据,此代码是否将删除此链表中的节点并留下第一个节点,数据和下一个字段中没有值?

                Because I want to start putting up some new values in it. To remove all those data, is this code going to remove nodes inside this linked list and left with the first node with no values in data and next field?

                此代码是用 C++ 编写的:

                This code is in C++:

                while(temp1!=tail)
                {
                    temp1 = temp1->next;
                    if(temp1->next == tail)
                    {
                        tail=temp1;
                        temp1 = temp1->next;
                        free(temp1);    
                    }
                }
                

                但是,这是否意味着只有最后一个节点会被删除?有没有办法删除除第一个节点以外的所有节点?

                But then, does this mean only the last node will be deleted? are there any way to delete all the nodes except the first one?

                推荐答案

                要删除除第一个节点以外的所有节点,可以试试下面的代码.

                To delete all nodes except the first node, you can try below code.

                temp1 = head->next;
                while(temp1!=NULL) // as I am considering tail->next = NULL
                {   
                    head->next = temp1->next;
                    temp1->next = NULL;
                    free(temp1);
                    temp1 = head->next;
                }
                

                这将删除除第一个节点之外的所有节点.但第一个节点的数据将保持原样.

                This will delete all nodes except first one. But the data with the first node will remain as it is.

                这篇关于删除链表中的所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:C++ 通用链表 下一篇:正确实现单链表 C++

                相关文章

                  1. <small id='Q8zae'></small><noframes id='Q8zae'>

                  2. <i id='Q8zae'><tr id='Q8zae'><dt id='Q8zae'><q id='Q8zae'><span id='Q8zae'><b id='Q8zae'><form id='Q8zae'><ins id='Q8zae'></ins><ul id='Q8zae'></ul><sub id='Q8zae'></sub></form><legend id='Q8zae'></legend><bdo id='Q8zae'><pre id='Q8zae'><center id='Q8zae'></center></pre></bdo></b><th id='Q8zae'></th></span></q></dt></tr></i><div id='Q8zae'><tfoot id='Q8zae'></tfoot><dl id='Q8zae'><fieldset id='Q8zae'></fieldset></dl></div>

                  3. <tfoot id='Q8zae'></tfoot>
                      <bdo id='Q8zae'></bdo><ul id='Q8zae'></ul>
                    <legend id='Q8zae'><style id='Q8zae'><dir id='Q8zae'><q id='Q8zae'></q></dir></style></legend>