Wednesday 11 January 2017

Write a c program to find pairs with given sum in doubly linked list.

Write a c program to find pairs with given sum in doubly linked list.
Program:
#include <stdio.h>
struct node
{
int data;
struct node *next,*back;
};

struct node *head,*tail,*c,*p;
create();
main()
{
int sum,i,value,flag=0;
create();
printf("enter the sum");
scanf("%d",&sum);
for(c=head;c->next!=NULL;c=c->next)
for(p=c->next;p!=NULL;p=p->next)
if(c->data+p->data == sum)
printf("\nsum found \t%d + %d = %d ",c->data,p->data,sum);

}
create()
{
int value,opt;
while(1)
{
printf("Press 1 to continue and Press 0 to stop");
scanf("%d",&opt);
if(opt == 1)
{
printf("\nenter a value");
scanf("%d",&value);
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=value;
if(head == NULL)
{
newnode->next=NULL;
newnode->back=NULL;
head=tail=newnode;

}
else
{
tail->next=newnode;
newnode->back=tail;
newnode->next=NULL;
tail=newnode;
}
}
else
return;
}
}

Output:

No comments:

Post a Comment