# Find pairs with given sum in doubly linked list

Here given code implementation process.

``````// C Program
// Find pairs with given sum in doubly linked list
#include <stdio.h>

#include <stdlib.h> //for malloc function

//create structure
struct Node {
int data;
struct Node *next;
struct Node *prev;
};

struct Node *head = NULL, *tail = NULL;

//insert Node element of end of linked list
void insert(int value) {

//Create a dynamic Node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL) {
printf("Memory overflow\n");
} else {
//Set data value
node->data = value;
node->next = NULL;
node->prev = NULL;
tail = node;

} else {
node->prev = tail;
tail->next = node;

tail = node;

}
}
}
//display element of Node
void display() {
if (temp == NULL) {
} else {

printf("\n Head to Tail Nodes : \n");
//Traverse doubly linked list from front to rear
while (temp != NULL) {
//print Node value
printf("%3d", temp->data);
temp = temp->next;
}
printf("\n Tail to Head Nodes : \n");

temp = tail;

//Traverse doubly linked list from rear to front
while (temp != NULL) {
//print Node value
printf("%3d", temp->data);
temp = temp->prev;
}
}

}
//Find all pair with given sum
void find_pair(int sum)
{
{
return;
}
int status = 0;

printf("\n All Pairs of given sum %d is : \n",sum);
while(front!=NULL)
{

//next node
auxiliary=front->next;

while(auxiliary!=NULL)
{
if(auxiliary->data+front->data == sum)
{
status=1;
printf(" (%d ,%d)",auxiliary->data,front->data );
}
auxiliary=auxiliary->next;
}
front=front->next;
}
if(status==0)
{
printf("\nNone" );
}

}
int main() {

insert(8);
insert(2);
insert(6);
insert(12);
insert(-2);
insert(7);
insert(9);
insert(3);
insert(4);

//display all node

display();
find_pair(10);

return 0;
}```
```

#### Output

`````` Head to Tail Nodes :
8  2  6 12 -2  7  9  3  4
4  3  9  7 -2 12  6  2  8
All Pairs of given sum 10 is :
(2 ,8) (4 ,6) (-2 ,12) (3 ,7)``````
``````/*
C++ Program
Find pairs with given sum in doubly linked list
*/

#include<iostream>

using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
Node(int value) {
this->data = value;
this->next = NULL;
this->prev = NULL;
}
};
public:
Node *tail;
this->tail = NULL;
}
void insert(int value) {
Node *node = new Node(value);
if (node == NULL) {
cout << "Memory overflow\n";
return;
}
this->tail = node;
} else {
node->prev = this->tail;
this->tail->next = node;
this->tail = node;
}
}
void display() {
if (temp == NULL) {
return;
}
cout << "\n Head to Tail Nodes : \n  ";
while (temp != NULL) {
cout << temp->data << "  ";
temp = temp->next;
}
temp = this->tail;
cout << "\n Tail to Head Nodes : \n  ";
while (temp != NULL) {
cout << temp->data << "  ";
temp = temp->prev;
}
}
void find_pair(int sum) {
return;
}
int status = 0;
Node *auxiliary = NULL, *front = this->head;
cout << "\n All Pairs of given sum " << sum << " is : \n";
while (front != NULL) {
auxiliary = front->next;
while (auxiliary != NULL) {
if (auxiliary->data + front->data == sum) {
status = 1;
cout << " (" << auxiliary->data << "," << front->data << ")";
}
auxiliary = auxiliary->next;
}
front = front->next;
}
if (status == 0) {
cout << "\nNone";
}
}
};
int main() {
obj.insert(8);
obj.insert(2);
obj.insert(6);
obj.insert(12);
obj.insert(-2);
obj.insert(7);
obj.insert(9);
obj.insert(3);
obj.insert(4);
obj.display();
obj.find_pair(10);
return 0;
}```
```

#### Output

`````` Head to Tail Nodes :
8  2  6 12 -2  7  9  3  4
4  3  9  7 -2 12  6  2  8
All Pairs of given sum 10 is :
(2 ,8) (4 ,6) (-2 ,12) (3 ,7)``````
``````/*
Java Program
Find pairs with given sum in doubly linked list
*/
class Node {

public int data;

public Node next;

public Node prev;

public Node(int value) {
//Setup initial values of linked list node
this.data = value;
this.next = null;
this.prev = null;
}
}

public Node tail;

tail = null;
}
public void insert(int value) {
Node node = new Node(value);

if (node == null) {

System.out.print("Memory overflow\n");
return;
}
tail = node;
} else {
node.prev = tail;
tail.next = node;
tail = node;
}
}

public void display() {
if (temp == null) {
return;
}

System.out.print("\n Head to Tail Nodes : \n  ");

while (temp != null) {
System.out.print(temp.data + "  ");
temp = temp.next;
}

temp = tail;

System.out.print("\n Tail to Head Nodes : \n  ");

while (temp != null) {
System.out.print(temp.data + "  ");
temp = temp.prev;
}

}
//Find all pair with given sum
void find_pair(int sum) {

return;
}
int status = 0;
Node auxiliary = null, front = head;

System.out.print("\n All Pairs of given sum " + sum + " is : \n");

while (front != null) {
auxiliary = front.next;

while (auxiliary != null) {

if (auxiliary.data + front.data == sum) {
status = 1;

System.out.print(" (" + auxiliary.data + "," + front.data + ")");
}
auxiliary = auxiliary.next;
}
front = front.next;
}

if (status == 0) {

System.out.print("\nNone");
}
}

public static void main(String[] args) {

obj.insert(8);
obj.insert(2);
obj.insert(6);
obj.insert(12);
obj.insert(-2);
obj.insert(7);
obj.insert(9);
obj.insert(3);
obj.insert(4);

//display all node

obj.display();
obj.find_pair(10);

}
}```
```

#### Output

`````` Head to Tail Nodes :
8  2  6 12 -2  7  9  3  4
4  3  9  7 -2 12  6  2  8
All Pairs of given sum 10 is :
(2 ,8) (4 ,6) (-2 ,12) (3 ,7)``````
``````/*
C# Program
Find pairs with given sum in doubly linked list
*/
using System;
public class Node {

public int data;

public Node next;

public Node prev;

public Node(int value) {
//Setup initial values of linked list node
this.data = value;
this.next = null;
this.prev = null;
}
}

public Node tail;

tail = null;
}
public void insert(int value) {
Node node = new Node(value);

if (node == null) {

Console.Write("Memory overflow\n");
return;
}
tail = node;
} else {
node.prev = tail;
tail.next = node;
tail = node;
}
}

public void display() {
if (temp == null) {
return;
}

Console.Write("\n Head to Tail Nodes : \n  ");

while (temp != null) {
Console.Write(temp.data + "  ");
temp = temp.next;
}

temp = tail;

Console.Write("\n Tail to Head Nodes : \n  ");

while (temp != null) {
Console.Write(temp.data + "  ");
temp = temp.prev;
}

}
//Find all pair with given sum
void find_pair(int sum) {

return;
}
int status = 0;
Node auxiliary = null, front = head;

Console.Write("\n All Pairs of given sum " + sum + " is : \n");

while (front != null) {
auxiliary = front.next;

while (auxiliary != null) {

if (auxiliary.data + front.data == sum) {
status = 1;

Console.Write(" (" + auxiliary.data + "," + front.data + ")");
}
auxiliary = auxiliary.next;
}
front = front.next;
}

if (status == 0) {

Console.Write("\nNone");
}
}

public static void Main(String[] args) {

obj.insert(8);
obj.insert(2);
obj.insert(6);
obj.insert(12);
obj.insert(-2);
obj.insert(7);
obj.insert(9);
obj.insert(3);
obj.insert(4);

//display all node

obj.display();
obj.find_pair(10);

}
}```
```

#### Output

`````` Head to Tail Nodes :
8  2  6 12 -2  7  9  3  4
4  3  9  7 -2 12  6  2  8
All Pairs of given sum 10 is :
(2 ,8) (4 ,6) (-2 ,12) (3 ,7)``````
``````# Python 3 Program
# Find pairs with given sum in doubly linked list

class Node :

def __init__(self, value) :
self.data = value
self.next = None
self.prev = None

def __init__(self) :
self.tail = None

def insert(self, value) :
node = Node(value)
if (node == None) :
print("Memory overflow\n")
return

self.tail = node
else :
node.prev = self.tail
self.tail.next = node
self.tail = node

def display(self) :
if (temp == None) :
return

print("\n Head to Tail Nodes :  ")
while (temp != None) :
print(temp.data ,end="  ")
temp = temp.next

temp = self.tail
print("\n Tail to Head Nodes :  ")
while (temp != None) :
print(temp.data ,end="  ")
temp = temp.prev

def find_pair(self, sum) :
return

status = 0
auxiliary = None
print("\n All Pairs of given sum ", sum ," is : ")
while (front != None) :
auxiliary = front.next
while (auxiliary != None) :
if (auxiliary.data + front.data == sum) :
status = 1
print(" (", auxiliary.data ,",", front.data ,")",end="")

auxiliary = auxiliary.next

front = front.next

if (status == 0) :
print("\nNone")

def main() :
obj.insert(8)
obj.insert(2)
obj.insert(6)
obj.insert(12)
obj.insert(-2)
obj.insert(7)
obj.insert(9)
obj.insert(3)
obj.insert(4)
obj.display()
obj.find_pair(10)

if __name__ == "__main__":
main()```
```

#### Output

`````` Head to Tail Nodes :
8  2  6 12 -2  7  9  3  4
4  3  9  7 -2 12  6  2  8
All Pairs of given sum 10 is :
(2 ,8) (4 ,6) (-2 ,12) (3 ,7)``````
``````# Ruby Program
# Find pairs with given sum in doubly linked list

class Node
attr_accessor :data, :next, :prev
def initialize(value)
self.data = value
self.next = nil
self.prev = nil
end
end

def initialize()
@tail = nil
end
def insert(value)
node = Node.new(value)
if (node == nil)
print("Memory overflow\n")
return
end
@tail = node
else
node.prev = @tail
@tail.next = node
@tail = node
end
end
def display()
if (temp == nil)
return
end
print("\n Head to Tail Nodes  :\n  ")
while (temp != nil)
print(temp.data ,"  ")
temp = temp.next
end
temp = @tail
print("\n Tail to Head Nodes  :\n  ")
while (temp != nil)
print(temp.data ,"  ")
temp = temp.prev
end
end
def find_pair(sum)
return
end
status = 0
auxiliary = nil
print("\n All Pairs of given sum ", sum ," is  :\n")
while (front != nil)
auxiliary = front.next
while (auxiliary != nil)
if (auxiliary.data + front.data == sum)
status = 1
print(" (", auxiliary.data ,",", front.data ,")")
end
auxiliary = auxiliary.next
end
front = front.next
end
if (status == 0)
print("\nNone")
end
end
end
def main()
obj.insert(8)
obj.insert(2)
obj.insert(6)
obj.insert(12)
obj.insert(-2)
obj.insert(7)
obj.insert(9)
obj.insert(3)
obj.insert(4)
obj.display()
obj.find_pair(10)
end

main()```
```

#### Output

`````` Head to Tail Nodes :
8  2  6 12 -2  7  9  3  4
4  3  9  7 -2 12  6  2  8
All Pairs of given sum 10 is :
(2 ,8) (4 ,6) (-2 ,12) (3 ,7)``````
``````<?php

/*
Php Program
Find pairs with given sum in doubly linked list
*/

class Node {
public \$data;
public \$next;
public \$prev;

function __construct(\$value) {
\$this->data = \$value;
\$this->next = null;
\$this->prev = null;
}
}
public \$tail;

function __construct() {
\$this->tail = null;
}
public  function insert(\$value) {
\$node = new Node(\$value);
if (\$node == null) {
echo("Memory overflow\n");
return;
}
\$this->tail = \$node;
} else {
\$node->prev = \$this->tail;
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public  function display() {
if (\$temp == null) {
return;
}
echo("\n Head to Tail Nodes : \n  ");
while (\$temp != null) {
echo(\$temp->data ."  ");
\$temp = \$temp->next;
}
\$temp = \$this->tail;
echo("\n Tail to Head Nodes : \n  ");
while (\$temp != null) {
echo(\$temp->data ."  ");
\$temp = \$temp->prev;
}
}

function find_pair(\$sum) {
return;
}
\$status = 0;
\$auxiliary = null;
echo("\n All Pairs of given sum ". \$sum ." is : \n");
while (\$front != null) {
\$auxiliary = \$front->next;
while (\$auxiliary != null) {
if (\$auxiliary->data + \$front->data == \$sum) {
\$status = 1;
echo(" (". \$auxiliary->data .",". \$front->data .")");
}
\$auxiliary = \$auxiliary->next;
}
\$front = \$front->next;
}
if (\$status == 0) {
echo("\nNone");
}
}
}

function main() {
\$obj->insert(8);
\$obj->insert(2);
\$obj->insert(6);
\$obj->insert(12);
\$obj->insert(-2);
\$obj->insert(7);
\$obj->insert(9);
\$obj->insert(3);
\$obj->insert(4);
\$obj->display();
\$obj->find_pair(10);
}
main();```
```

#### Output

`````` Head to Tail Nodes :
8  2  6 12 -2  7  9  3  4
4  3  9  7 -2 12  6  2  8
All Pairs of given sum 10 is :
(2 ,8) (4 ,6) (-2 ,12) (3 ,7)``````
``````/*
Node Js Program
Find pairs with given sum in doubly linked list
*/

class Node {

constructor(value) {
this.data = value;
this.next = null;
this.prev = null;
}
}

constructor() {
this.tail = null;
}
insert(value) {
var node = new Node(value);
if (node == null) {
process.stdout.write("Memory overflow\n");
return;
}
this.tail = node;
} else {
node.prev = this.tail;
this.tail.next = node;
this.tail = node;
}
}
display() {
if (temp == null) {
return;
}
process.stdout.write("\n Head to Tail Nodes : \n  ");
while (temp != null) {
process.stdout.write(temp.data + "  ");
temp = temp.next;
}
temp = this.tail;
process.stdout.write("\n Tail to Head Nodes : \n  ");
while (temp != null) {
process.stdout.write(temp.data + "  ");
temp = temp.prev;
}
}
find_pair(sum) {
return;
}
var status = 0;
var auxiliary = null;
process.stdout.write("\n All Pairs of given sum " + sum + " is : \n");
while (front != null) {
auxiliary = front.next;
while (auxiliary != null) {
if (auxiliary.data + front.data == sum) {
status = 1;
process.stdout.write(" (" + auxiliary.data + "," + front.data + ")");
}
auxiliary = auxiliary.next;
}
front = front.next;
}
if (status == 0) {
process.stdout.write("\nNone");
}
}
}

function main() {
obj.insert(8);
obj.insert(2);
obj.insert(6);
obj.insert(12);
obj.insert(-2);
obj.insert(7);
obj.insert(9);
obj.insert(3);
obj.insert(4);
obj.display();
obj.find_pair(10);
}

main();```
```

#### Output

`````` Head to Tail Nodes :
8  2  6 12 -2  7  9  3  4
4  3  9  7 -2 12  6  2  8
All Pairs of given sum 10 is :
(2 ,8) (4 ,6) (-2 ,12) (3 ,7)``````
``````/*
Swift 4 Program
Find pairs with given sum in doubly linked list
*/

class Node {
var data: Int;
var next: Node? ;
var prev: Node? ;
init(_ value: Int) {
self.data = value;
self.next = nil;
self.prev = nil;
}
}
var tail: Node? ;
init() {
self.tail = nil;
}
func insert(_ value: Int) {
let node: Node? = Node(value);
if (node == nil) {
print("Memory overflow\n");
return;
}
self.tail = node;
} else {
node!.prev = self.tail;
self.tail!.next = node;
self.tail = node;
}
}
func display() {
if (temp == nil) {
return;
}
print("\n Head to Tail Nodes : ");
while (temp != nil) {
print(temp!.data ,terminator:"  ");
temp = temp!.next;
}
temp = self.tail;
print("\n Tail to Head Nodes : ");
while (temp != nil) {
print(temp!.data ,terminator:"  ");
temp = temp!.prev;
}
}
func find_pair(_ sum: Int) {
return;
}
var status: Int = 0;
var auxiliary: Node? = nil;
print("\n All Pairs of given sum ", sum ," is : ");
while (front != nil) {
auxiliary = front!.next;
while (auxiliary != nil) {
if (auxiliary!.data + front!.data == sum) {
status = 1;
print(" (", auxiliary!.data ,",", front!.data ,")",terminator:"");
}
auxiliary = auxiliary!.next;
}
front = front!.next;
}
if (status == 0) {
print("\nNone");
}
}
}
func main() {
obj.insert(8);
obj.insert(2);
obj.insert(6);
obj.insert(12);
obj.insert(-2);
obj.insert(7);
obj.insert(9);
obj.insert(3);
obj.insert(4);
obj.display();
obj.find_pair(10);
}
main();```
```

#### Output

`````` Head to Tail Nodes :
8  2  6 12 -2  7  9  3  4
4  3  9  7 -2 12  6  2  8
All Pairs of given sum 10 is :
(2 ,8) (4 ,6) (-2 ,12) (3 ,7)``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.