You are on page 1of 10

Name of the Course: Data structures (DS)

Assignment – 2
1602-19-733-080
M. Meghana
27-10-2020

SET IV ( 1602-19-733-080 to 1602-19-733-085):

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 ?
Input : head : 1 <-> 2 <-> 4 <-> 5 <-> 6 <-> 8 <-> 9
x=7
Output: (6, 1), (5,2)
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
}*head=NULL,*p,*q;
int x;
void newNode(int ele)
{
if(head==NULL)
{
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->prev=NULL;
temp->next==NULL;
temp->data=ele;
head=temp;
}
else if(head!=NULL)
{
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->data=ele;
q=head;
while(q->next!=NULL)
{
q=q->next;
}
temp->prev=q;
q->next=temp;
temp->next=NULL;
}
}
void display()
{
q=head;
if(head==NULL)
{
printf("List is empty\n");
return;
}
printf("The list is:\n");
while(q->next!=NULL)
{
printf("%d <-> ",q->data);
q=q->next;
}
printf("%d\n",q->data);
}
void pairs()
{
p=head;
q=head;
int flag=0;
while(q->next!=NULL)
{
q=q->next;
}
while (p != NULL && q != NULL && p != q && q->next != p)
{
if ((p->data + q->data) == x)
{
flag = 1;
printf("(%d , %d)\n",q->data,p->data);
p = p->next;
q = q->prev;
}
else
{
if ((p->data + q->data) < x)
p = p->next;
else
q = q->prev;

}
if (flag == 0)
printf("No pair found");
}
int main()
{
printf("Enter number of elements in list:\n");
int n;
scanf("%d",&n);
printf("Enter elements:\n");
int a[n];
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
newNode(a[i]);
}
printf("Enter x:\n");
scanf("%d",&x);
display();
printf("The pair(s) is/are:\n");
pairs();
}

Output:
2. Given a mathematical equation, develop a program to verify the correctness by
checking for matching parenthesis.
ex: (a+((b+(c+d))) ( (a+(b+(c+d)))
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
char data;
struct node *link;
}*p,*q,*top;
void push(char brace)
{
struct node *temp=(struct node *)malloc(sizeof(struct node));
temp->data=brace;
temp->link=top;
top=temp;
}
char pop()
{
char a;
struct node *temp=(struct node *)malloc(sizeof(struct node));
if(top==NULL)
{
return '\0';
}
else
{
temp=top;
a= top->data;
top=top->link;
free(temp);
}
return a;
}
int isOperator(char symbol) {

switch(symbol) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '(':
case ')':
return 1;
break;
default:
return 0;
}
}
int checker(char str[])
{
int i=0;
char check;
while(str[i]!='\0')
{
if(str[i]=='(' || str[i]=='{' || str[i]=='[')
{
push(str[i]);
}
else if(str[i]==')' || str[i]=='}' || str[i]==']')
{
if(top==NULL)
{
return 0;
}
else
{
check = pop();
if(str[i]==')' && (check!='(' || isOperator(str[i-1])))
{
return 0;
}
else if(str[i]=='}' && (check!='{' || isOperator(str[i-1])))
{
return 0;
}
else if(str[i]==']' && (check!='[' || isOperator(str[i-1])))
{
return 0;
}
else if(check=='\0')
{
return 0;
}
}
}
i++;
}
if(top!=NULL)
{
return 0;
}
else
{
return 1;
}
}
int main()
{
char str[20];
printf("Enter the expression to check for matching paranthesis\n");
scanf("%[^\n]%*c",str);
int flag = checker(str);
if(flag==1)
printf("Balanced");
else
printf("Not balanced");
}
Output:
3. Raj and Rahul are competing with each other in a race tournament. Raj
path while racing can be represented using a linked list, and so can be the
path of the Rahul. They will be in danger if they intersect each other. Raj
wants to know how safe he is, i.e. he wants to know if there is a possibility that
his path and the Rahul path intersect.
For example, if linked list A denotes Raj's path and linked list B denotes the Rahul
path,while the following situation is safe :
A: a1 → a2 → a3 → NULL
B: b1 → b2 → b3 → b4 → b5 → NULL
Following situation would be dangerous for harry :
A: a1 → a2

c1→ c2 → c3 → NULL

B: b1 → b2 → b3
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*list1,*list2,*p,*q;
void insertList1(int x)
{
if(list1==NULL)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=list1;
list1=temp;
}
else
{
q=list1;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=NULL;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
void insertList2(int x)
{
if(list2==NULL)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=list2;
list2=temp;
}
else
{
q=list2;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=NULL;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
void display(struct node *list)
{
struct node *temp=list;
while(temp!=NULL)
{
printf("%d -> ",temp->data);
temp=temp->link;
}
printf("NULL\n");
}
int intersection()
{
p=list1;
int flag=1;
while(p!=NULL)
{
q=list2;
while(q!=NULL)
{
if(p->data==q->data)
{
flag=0;
break;
}
q=q->link;
}
if(flag==0)
break;
p=p->link;
}
return flag;
}
int main()
{
int n1,n2,i,data;
printf("Enter number of nodes in list1\n");
scanf("%d",&n1);
printf("Enter data:\n");
for(i=0;i<n1;i++)
{
scanf("%d",&data);
insertList1(data);
}
printf("Enter number of nodes in list2\n");
scanf("%d",&n2);
printf("Enter data:\n");
for(i=0;i<n2;i++)
{
scanf("%d",&data);
insertList2(data);
}
printf("List1 is:\n");
display(list1);
printf("List2 is:\n");
display(list2);
int flag=intersection();
if(flag==1)
{
printf("No danger- They do not intersect\n");
}
else
{
printf("Danger- They intersect\n");
}
}

Output:

You might also like