Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear
Solution
Program
#include <stdio.h> //header file
#include<stdlib.h>
struct node {
int data;
struct node *next;//self referential structure or linked list structure
}*num1,*num2,*tmp,*ptr; //global variable
int main()
{
printf("First group element should be less than 7\n Element should not repeat more than one in any group");
int p,q;
struct node *fn;
printf(" \n Number of Elements = ");
scanf("%d",&q);
printf("Type 1 Element = ");
num1=malloc(sizeof(struct node));//memory allocation for num1
scanf("%d",&p);
num1->data=p;
num1->next=NULL;
ptr=num1;
for(int i=2;i<=q;i++)
{
fn=malloc(sizeof(struct node));//memory allocation for fn
printf("Type %d Element = ",i);
scanf("%d",&p); //reading values from console
fn->data=p;
fn->next=NULL;
ptr->next=fn;
ptr=ptr->next;
}
ptr=num1;
printf("First Group Elements = {");
while(ptr!=NULL)
{
printf("%d,",ptr->data);//first group element writing on console
ptr=ptr->next;
}printf("}");
printf("\n Number of Elements = ");
{
int p,y;
struct node *fn;
scanf("%d",&y);
printf("\n Type your Second Group Element = ");
num2=malloc(sizeof(struct node));
scanf("%d",&p);
num2->data=p;
num2->next=NULL;
tmp=num2;
for(int i=2;i<=y;i++)
{
fn=malloc(sizeof(struct node)); //second group element reading from console
printf("Type %d Element = ",i);
scanf("%d",&p);
fn->data=p;
fn->next=NULL;
tmp->next=fn;
tmp=tmp->next;
}
}
{
tmp=num2;
printf("Second group Elements = { ");
while(tmp!=NULL)//second group element writing on console
{
printf("%d,",tmp->data);tmp=tmp->next;
}
printf("}");
}
{
ptr=num1;
tmp=num2;
}
printf("\n Union1={");
while(ptr!=NULL)
{
//condition for union just I am printing first group elements
printf("%d,",ptr->data);
ptr=ptr->next;
}ptr=num1;
while(tmp!=0) //condition that we are adding second group elements which is not present in first group
{
if(tmp->data!=ptr->data && tmp->data!=ptr->next->data && tmp->data!=ptr->next->next->data && tmp->data!=ptr->next->next->next->data && tmp->data!=ptr->next->next->next->next->data&& tmp->data!=ptr->next->next->next->next->next->data && tmp->data!=ptr->next->next->next->next->next->next->data)
{
printf(",%d",tmp->data);
//checking element by element
}
tmp=tmp->next;
}
printf("}\n");
printf("intersection={"); //condition for intersection
ptr=num1;
tmp=num2;
while(ptr!=NULL)
{tmp=num2;
while(tmp!=NULL)
{
if(tmp->data==ptr->data) //if elements are present in both the group then we should write on console
{
printf("%d,",tmp->data);
}
tmp=tmp->next;
}
ptr=ptr->next;
}
printf("}");
}
Output
