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

时间:2018-03-18 19:58:53

标签: c++ pointers linked-list doubly-linked-list

关于双链表的这段代码的解释是否正确?

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

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

<div class="container">
  <div class="first child">first first first first first first first</div>
  <div class="second child">second second second second second second second second second second second second second second second second second second second</div>
  <div class="third child">third</div>
</div>

1 个答案:

答案 0 :(得分:1)

我已经改变了看似错误的地方 请记住,在dll中,将有2个指针指向next,而另一个指向前一个元素。 dll的每个元素都有数据,* next和* prev。

void DoublyLinkedList :: SortedInsert(const int&amp; 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;
            }
        }


    }

希望它有所帮助!