0% found this document useful (0 votes)
47 views3 pages

Data Structures and Algorithms Digital Assignment

The document describes a C program to find pairs of nodes in a sorted doubly linked list whose sum is equal to a given value x, without using extra space. It includes functions to insert nodes into the linked list, and to find pairs by traversing the list from the beginning and end simultaneously, comparing the sum of each node pair to x. If a pair is found with a matching sum, the nodes' data is printed; otherwise if no pair is found, a message is printed.

Uploaded by

kartik8586
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

Data Structures and Algorithms Digital Assignment

The document describes a C program to find pairs of nodes in a sorted doubly linked list whose sum is equal to a given value x, without using extra space. It includes functions to insert nodes into the linked list, and to find pairs by traversing the list from the beginning and end simultaneously, comparing the sum of each node pair to x. If a pair is found with a matching sum, the nodes' data is printed; otherwise if no pair is found, a message is printed.

Uploaded by

kartik8586
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DATA STRUCTURES AND ALGORITHMS

DIGITAL ASSIGNMENT
1.Given a sorted doubly linked list of positive distinct elements, the task is to find pairs in
doubly linked list whose sum is equal to given value x, without using any extra space ?
CODE :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define bool int
#define true 1
#define false 0
struct Node
{
int data;
struct Node *next, *prev;
};

void pairSum(struct Node *head, int x)


{
struct Node *first = head;
struct Node *second = head;
while (second->next != NULL)
second = second->next;

bool found = false;

while (first != NULL && second != NULL &&


first != second && second->next != first)
{
if ((first->data + second->data) == x)
{
found = true;
printf("(%d,%d)\n" , first->data , second->data);

first = first->next;

second = second->prev;
}
else
{
if ((first->data + second->data) < x)
first = first->next;
else
second = second->prev;
}
}

if (found == false)
printf("No pair found");
}

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


{
struct Node *temp;
temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = temp->prev = NULL;
if (!(*head))
(*head) = temp;
else
{
temp->next = *head;
(*head)->prev = temp;
(*head) = temp;
}
}
int main()
{
struct Node *head = NULL;
int i , x , num;
printf("Give the value of x :");
scanf("%d" , &x);
printf("Enter the number of elements in the linked list : ");
scanf("%d" , &num);
for( i =0 ;i < num ; i++){
int n ;
printf("Enter the value of element %d :" , i+1 );
scanf("%d" , &n);
insert(&head , n);
}
pairSum(head, x);
return 0;}

You might also like