簡體   English   中英

如何解釋這個雙重鏈表代碼?

[英]How to explain this doubly linked list code?

這段關於雙鏈表的代碼解釋正確嗎?

我無法理解部分代碼。 在這段代碼中發生了什么?

我已經解釋了代碼的一部分,但不是很確定它是否正確。

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;
            }
        }


    }

希望能幫助到你 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM