Monday, May 24, 2021

Published May 24, 2021 by Anonymous with 0 comment

Find quadruplets with given sum in a Doubly Linked List

#include <bits/stdc++.h>

using namespace std;

  

struct Node {

    int data;

    struct Node *next, *prev;

};

  

void insert(struct Node** head, int data)

{

    

    struct Node* temp = new Node();

  

    

    temp->data = data;

    temp->next = temp->prev = NULL;

  

    if ((*head) == NULL)

        (*head) = temp;

    else {

        temp->next = *head;

        (*head)->prev = temp;

        (*head) = temp;

    }

}

  

void PrintFourSum(struct Node* head, int x)

{

    

    struct Node* first = head;

  

    

    

    struct Node* second;

  

    

    

    struct Node* third;

  

    

    struct Node* fourth = head;

  

    

    

    while (fourth->next != NULL) {

        fourth = fourth->next;

    }

  

    

    

    struct Node* temp;

  

    while (first != NULL

           && fourth != NULL

           && first != fourth

           && fourth->next != first) {

  

        

        

        second = first->next;

  

        while (second != NULL

               && fourth != NULL

               && second != fourth

               && fourth->next != second) {

  

            int reqsum = x - (first->data

                              + second->data);

  

            

            

            third = second->next;

  

            

            temp = fourth;

  

            while (third != NULL && temp != NULL

                   && third != temp

                   && temp->next != third) {

  

                

                int twosum = third->data

                             + temp->data;

  

                

                

                if (twosum == reqsum) {

  

                    cout << "(" << first->data

                         << ", "

                         << second->data

                         << ", "

                         << third->data

                         << ", "

                         << temp->data

                         << ")\n";

  

                    third = third->next;

                    temp = temp->prev;

                }

  

                

                

                

                else if (twosum < reqsum) {

                    third = third->next;

                }

  

                

                

                

                else {

                    temp = temp->prev;

                }

            }

  

            

            

            second = second->next;

        }

  

        

        

        first = first->next;

    }

}

  

int main()

{

    struct Node* head = NULL;

    insert(&head, 2);

    insert(&head, 1);

    insert(&head, 0);

    insert(&head, 0);

    insert(&head, -1);

    insert(&head, -2);

    int X = 0;

    PrintFourSum(head, X);

  

    return 0;

}

Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment