在已排序的双向链接列表中插入节点

时间:2013-12-01 11:00:38

标签: c++ algorithm

我无法弄明白,为什么我的代码插入到排序的双向链表中,在某些测试用例中失败。请让我知道。我不知道测试用例,它们是系统生成的。

Node* SortedInsert(Node *head,int data)
{
    // Complete this function
    // Do not write the main method.
    Node * temp = (Node*)malloc(sizeof(Node));
    temp->data = data;
    temp->next = NULL;
    temp->prev = NULL;
    if (head == NULL)
    {
        head = temp;
        return head;
    }

    if (temp->data <= head->data)
    {
        temp->next = head;
        head->prev = temp;
        head = temp;
        return head;
    }

    Node *curr = head;
    while (curr->next!=NULL)
    {
        if (temp->data <= curr->data)
        {   
            curr->prev->next = temp;
            temp->prev = curr->prev;
            temp->next = curr;
            curr->prev = temp;
            return head;
        }

        curr = curr->next;
    }

    curr->next = temp;
    temp->prev = curr;
    return head;
}

谢谢

7 个答案:

答案 0 :(得分:7)

到达最后一个节点后,您应该再次将其数据与新节点进行比较并相应插入。

    curr->next = temp;
    temp->prev = curr;
    return head;
}

如果执行到达此部分,则当前curr指向最后一个节点。现在你应该再次比较它的数据如下:

    if (temp->data <= curr->data)
    {   // insert before last node
        curr->prev->next = temp;
        temp->prev = curr->prev;
        temp->next = curr;
        curr->prev = temp;
        return head;
    }
    // else insert at the end.
    curr->next = temp;
    temp->prev = curr;
    return head;
}

答案 1 :(得分:1)

或者,您可以在列表末尾为节点编写if条件

Node* SortedInsert(Node *head,int data)
{
struct Node* p = head;
struct Node* q = NULL;
    struct Node* r = new Node;
    r->data=data;
    r->prev=NULL;
    r->next=NULL;

    if(p==NULL){
        p=r;
        head=p;
    }
    else if(p!=NULL&&p->data>r->data){
        p->prev=r;
        r->next=p;
        p->next=NULL;
        head = r;
    }
    else{
                p=head;
        while(p!=NULL) {

              if(p!=NULL&&p->data>r->data){   //If node is not at the end of list and smaller than some node

                  (p->prev)->next = r;
                  r->next = p;
                  r->prev = p->prev;
                  p->prev=r;
             return head;
        }
        else if(p->next==NULL)           //If node is at the end of list
            {

            p->next = r;
            r->prev = p;
           r->next = NULL;
            return head;
        }
            else{}
     p=p->next; 
          }
    }
 return head;

}

答案 2 :(得分:0)

/*
* For reference:
*
* link to the problem :- 
* https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list
*
* DoublyLinkedListNode {
*     int data;
*     DoublyLinkedListNode* next;
*     DoublyLinkedListNode* prev;
* };
*
* Function to insert a node in sorted DLL with given data & return head 
*/
DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
    DoublyLinkedListNode** prev = &head;
    DoublyLinkedListNode* newNode = new DoublyLinkedListNode(data);

    while ( *prev && ((*prev)->data <= data) ) {
        prev = &(*prev)->next;
    }

    newNode->prev = *prev;
    newNode->next = *prev;
    *prev = newNode;

    return head;
}

此解决方案在所有情况下均有效(在中间插入,在头部插入,在末端插入等)。我只是试图在没有任何条件的情况下编写代码。在搜索某些错误时,我最终遇到了这个SO线程。如果此解决方案不适用于某些测试用例,请纠正我。

答案 3 :(得分:0)

这是我通过所有测试用例的代码。

DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
    DoublyLinkedListNode *temp = head;
    if (!temp) {
        temp = new DoublyLinkedListNode(data);
        head = temp;
        return head;
    }
    while (temp->data < data && temp->next != nullptr) {
        temp = temp->next;
    }
    if (temp->data > data) {
        DoublyLinkedListNode *t = new DoublyLinkedListNode(data);
        t->next = temp;
        if (temp == head) {
            head = t;
            temp->prev = t;
            return head;
        }
        temp->prev->next = t;
        t->next = temp;
        temp->prev = t;
    } else {
        DoublyLinkedListNode *t = new DoublyLinkedListNode(data);
        t->next = temp->next;
        temp->next = t;
        temp->next->prev = temp;
        if (t->next != nullptr) t->next->prev = t;
    }
    return head;   
}

答案 4 :(得分:-1)

以下是一些代码,以便在排序的双向链接列表

中插入节点
Node* SortedInsert(Node *head,int data)
 {
  struct Node *temp;
  struct Node *newNode = new Node();
  newNode->data=data;
  newNode->next=NULL;
  newNode->prev=NULL;
  temp=head;

if(head==NULL)
 {
    head = newNode;      
 }

else
 {
    while(temp!=NULL)
    {
        if(temp->data<data&&temp->next!=NULL)
        {
            temp=temp->next;
        }
        else
            {
            if(temp->next==NULL&&temp->data<data)
                {
                temp->next = newNode;
                newNode->prev = temp;
                break;
            }

           else if(temp->prev==NULL&&temp->data>data)
                {
                newNode->next=temp;
                temp->prev = newNode;
                head=newNode;
                break;
            }

           else
                {
                newNode->next = temp;
                newNode->prev = temp->prev;
                temp->prev->next =newNode;
                temp->prev=newNode;
                break;
                }    
             }
         }
   }
  return head;
}

答案 5 :(得分:-1)

这是一个完整的C ++程序,用于在排序的双向链表中的适当位置插入节点:

void InsertInSortedDoublyLinkedList()
    {
        Node *head;
        head = CreateDoublyLinkList(3);
        PrintDoublyLinkedList(head);
        head = SortedInsert(head, 6);
        printf("\n Linked list after insertion in sorted order\n");
        PrintDoublyLinkedList(head);
    }

void PrintDoublyLinkedList(Node *head)
{
    Node *temp = head;
    printf("NULL -> ");
    while (temp)
    {
        printf("%d ->", temp->data);
        temp = temp->next;
    }
    printf("NULL");
}


Node* CreateDoublyLinkList(int numberOfNodes)
{
    Node *head = NULL, *temp, *temp1;
    int startingValue = 3;

    if (numberOfNodes == 0)
    {
        return head;
    }

    temp = (Node*)malloc(sizeof(Node));
    temp->data = startingValue;
    temp->next = NULL;
    temp->prev = NULL;
    head = temp;
    startingValue += 2;
    numberOfNodes--;
    for (; numberOfNodes > 0; numberOfNodes--, startingValue += 2, temp = temp->next)
    {
        temp1 = (Node*)malloc(sizeof(Node));
        temp1->data = startingValue;
        temp1->next = NULL;
        temp1->prev = temp;
        temp->next = temp1;
    }

    return head;
}

Node* SortedInsert(Node *head,int data)
{
        Node *temp = NULL, *temp1,*newNode;
        int nodeInserted = 0;
        if (head == NULL)
        {
            head = (Node*) malloc(sizeof(Node));
            head->data = data;
            head->next = NULL;
            head->prev = NULL;
        }
        else
        {
            if (head->data > data)
            {
                //insertion need to take place before head itself.
                newNode = (Node*)malloc(sizeof(Node));
                newNode->data = data;
                newNode->next = head;
                newNode->prev = NULL;
                head->prev = newNode;
                head = newNode;
            }
            else
            {
                temp1 = head;
                temp = head ->next;
                while (temp)
                {
                    if (temp->data > data)
                    {
                        //we need to insert the node before temp
                        newNode = (Node*)malloc(sizeof(Node));
                        newNode->data = data;
                        newNode->prev = temp1;
                        newNode->next = temp;


                        temp1->next = newNode;
                        temp->prev = newNode;
                        nodeInserted = 1;
                        break;
                    }
                    temp1 = temp;
                    temp = temp->next;
                }
                if (!nodeInserted)
                {
                    //node insertion need to take place at tail.
                    newNode = (Node*)malloc(sizeof(Node));
                    newNode->data = data;
                    newNode->prev = temp1;
                    newNode->next = NULL;
                    temp1->next = newNode;
                }

            }

        }

        return head;
}

答案 6 :(得分:-2)

               struct Node
               {
                 int data;
                 Node *next;
                 Node *prev;
               }

            Node* SortedInsert(Node *head,int data)
            {
                Node* p1,*p2;
                int n=10;
                p2=(Node*)malloc(sizeof(struct Node));
                p2->next=NULL;
                p2->prev=NULL;
                p2->data=data;

                p1=head;
                if(p1==NULL)
                    return p2;

                while(p1->next!=NULL)
                    {
                    if(p1->data<data)
                        p1=p1->next;
                    else 
                        break;
                    }
                /*Three cases arise when p1->next == NUll i.e. we are end of list
                case 1: insert after the end
                case 2: insert in between last and second last node
                case 3: insert node at the beginning of the list
                */
                if(p1->next==NULL)
                    {
                        if(p1->data<data)
                            {
                                p2->prev=p1;
                                p1->next=p2;
                            }
                        else if(p1->data > data && p1->prev!=NULL)
                            {
                            p2->next=p1;
                            p2->prev=p1->prev;
                            if(p1->prev!=NULL)
                            p1->prev->next=p2;
                            p1->prev=p2;
                        }
                        else
                            {
                                p2->next=p1;
                                p2->prev=p1->prev;
                                if(p1->prev!=NULL)
                                p1->prev->next=p2;
                                p1->prev=p2;
                                head=p2;
                            }
                    }

                // Here we have only one case where new node is inserted between two nodes of the list
                else
                    {
                        p2->next=p1;
                        p2->prev=p1->prev;
                        if(p1->prev!=NULL)
                        p1->prev->next=p2;
                        p1->prev=p2;
                }

                return head;
            }