Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int data;
struct node* next;
};

struct node*createlist(int n)
{
struct node *head=0,*newnode,*temp;
int x,i;
for(i=0;i<n;i++)
{
printf("\nEnter the data: ");
fflush(stdout);
scanf("%d",&x);
newnode=(struct node*) malloc(sizeof(struct node));
newnode->data =x;
newnode->next=0;
if (head==0)
{
head=temp=newnode;
}
else{
temp->next=newnode;
temp=newnode;
}
}
return head;
}
void swap (struct node *a, struct node*b)
{
int tp;
tp=a->data;
a->data=b->data;
b->data=tp;
}
void sort (struct node *head_s)
{
int cl;
struct node *ptr1,*ptr2=NULL;
if(head_s==NULL)
return;
do
{
cl=0;
ptr1=head_s;
while(ptr1->next!=ptr2)
{
if(ptr1->data>ptr1->next->data)
{
swap(ptr1,ptr1->next);
cl=1;
}
ptr1=ptr1->next;
}
ptr2=ptr1;
}
while(cl);
}
struct node* unionlist(struct node *first_node, struct node *second_node ,int m,int n)
{
int j,i;
struct node *ptr,*ptr1,*ptr2,*ptr3,*head_p=NULL;
ptr1=first_node;
for(i=0;i<m;i++)
{
ptr=(struct node*) malloc(sizeof(struct node));
ptr->data=ptr1->data;
ptr->next=0;
if(head_p==NULL)
head_p=ptr;
else
ptr3->next=ptr;
ptr3=ptr;
ptr1=ptr1->next;
}
ptr1=second_node;
while(ptr1!=NULL)
{
ptr2=head_p;
j=0;
while(ptr2!=NULL)
{
if(ptr1->data==ptr2->data)
j++;
ptr2=ptr2->next;

}
if(j==0)
{
ptr=(struct node*) malloc(sizeof(struct node));
ptr->data=ptr1->data;
ptr->next=NULL;
ptr3->next=ptr;
ptr3=ptr;
}
ptr1=ptr1->next;
}
return head_p;
}
struct node* intersec_list(struct node *first_node, struct node *second_node )
{
int j;
struct node *ptr,*ptr1,*ptr2,*ptr3,*head_pi=NULL;
ptr1=first_node;
while(ptr1!=NULL)
{
ptr2=second_node;
j=0;
while(ptr2!=NULL)
{
if(ptr1->data==ptr2->data)
j++;
ptr2=ptr2->next;

}
if(j!=0)
{
ptr=(struct node*) malloc(sizeof(struct node));
ptr->data=ptr1->data;
ptr->next=NULL;
if(head_pi==NULL)
head_pi=ptr;
else
ptr3->next=ptr;
ptr3=ptr;
}
ptr1=ptr1->next;
}
return head_pi;
}
void display(struct node *head)
{
struct node *dis=head;
while(dis!=NULL)
{
printf("%d ", dis->data);
fflush(stdout);
dis=dis->next;
}
}
int main()
{
struct node *lis1,*lis2,*union_list,*inter_list;
int n1,n2;
printf("\nEnter the number of elements in first list: ");
fflush(stdout);
scanf("%d", &n1);
printf("\nEnter the number of elements in second list: ");
fflush(stdout);
scanf("%d", &n2);
printf("\n creating first linked list");
lis1=createlist(n1);
printf("\n creating second linked list");
lis2=createlist(n2);
printf("\nThe first list is :");
display(lis1);
printf("\nThe second list is :");
display(lis2);
union_list=unionlist(lis1,lis2,n1,n2);
printf("\n The Union of Two list is ");
sort(union_list);
display(union_list);
inter_list=intersec_list(lis1,lis2);
printf("\n The intersection of Two list is ");
display(inter_list);
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.