如何解释这个双向链表代码

How to explain this doubly linked list code?

本文关键字:双向链表 代码 解释 何解释      更新时间:2023-10-16
这段

关于双向链表的代码解释正确吗?

我无法理解代码的一部分。这段代码发生了什么?

我已经解释了部分代码,但不太确定它是否正确。

void DoublyLinkedList::SortedInsert(const int& new_element) {

    if (new_element != 0) {                      //creates new node np with value equal to new element
        Node* np = new Node(new_element); //if new element is not equal to zero and if its not head, it points to both                                              
                                                         head and  tail
        if (!Head)
        {
            Head = np;
            Tail = np;

        }
        else if (new_element < Head->Element)   //if new element less than element in Head np's  next pointer pointing to                                        
                                                        Head and Head prev pointer pointing to np and Head is pointing to np
        {
            np->Next = Head;
            Head->Prev = np;
            Head = np;

        }
        else                                     //if new element is greater than Head we take cur node which is pointing                                      
                                                   to Head's next pointer pointing to node after Head
        {
            Node *cur = Head->Next;
            while ((cur) && (new_element >= cur->Element))  //new element greater than Node after Head,cur will point to                                            cur's next pointer which is node after cur
                cur = cur->Next;
            if (cur)
            {                                            // ?? whats happening here
                np->Prev = cur->Prev;
                np->Next = cur;
                cur->Prev->Next = np;
                cur->Prev = np;
            }
            else                                         // ?? whats happening here
            {
                Tail->Next = np;
                np->Prev = Tail;
                Tail = np;
            }
        }

    }

我已经改变了似乎有问题的地方。
请记住,在 dll 中,将有 2 个指针,一个指向下一个元素,另一个指向上一个元素。dll 的每个元素都有数据、*next 和 *prev。

void DoublyLinkedList::SortedInsert(const int& new_element( {

    if (new_element != 0) {                      //creates new node np with value equal to new element
        Node* np = new Node(new_element); 
       //Here we create just a pointer of type 'node' if new element is not 
       //equal to zero and if no head exists then obviously this is the 1st 
       //element, hence head and tail  points to both 1st element.                                  
        if (!Head)
        {
            Head = np;
            Tail = np;
        }
        //if new element less than element in Head np's  next pointer 
        //pointing to Head and Head prev pointer pointing to np and Head is  
        //pointing to np
        //Here it seems you are thinking wrong. Remember head never has next 
        //or previous pointer. It always points to first element unless we 
        //make it move. You mentioned Head np's next pointer, that will be 
        //Head->element->next and not head->element. Head->element means the 
        //first position and if our list already has a element to which head
        //is pointing we compare it with our new element which we want to 
        //insert. 
        else if (new_element < Head->Element)                                                                               
        {                                 //if new elm is less than 1st elm
            np->Next = Head;              //now new elm bcms 1st elm as it next pointer points to the one is currently being pointed by head
            Head->Prev = np;              //now elm pointed by head prev pointer will point to new element
            Head = np;                    //finally our new element is 1st //element in list hence we assigb head pointer to point towards it.    

        }
        //this is the condition when new elmt which we are inserting is 
        //greater than our element pointed by head. Thus obviously  
        //we need to traverse and find the element which is greater than
        //our new element. This code does that and then adjusts the next and 
        //previous pointer
        else                                     
        {
            Node *cur = Head->Next;
            while ((cur) && (new_element >= cur->Element))  
                cur = cur->Next;
            if (cur)
            {                                           
                np->Prev = cur->Prev;
                np->Next = cur;
                cur->Prev->Next = np;
                cur->Prev = np;
            }
            else                                       
            {
                Tail->Next = np;
                np->Prev = Tail;
                Tail = np;
            }
        }

    }

希望对您有所帮助!