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>
//structure of linked list
struct node{
int data;
struct node *next;
}*linklistone,*linklisttwo,*unionll,*intersectionll;
void initialise(){
linklistone=linklisttwo=NULL;
}
//function used to insert the num into list
void insert(struct node **head,int num)
{
//we allocate node
struct node *newnode=(struct node*)malloc(sizeof(struct node));
//put data
newnode->data=num;//newnode point to data, num will be put into the data
//new node as header
newnode->next=*head;
//head pointer to new node
*head=newnode;
}
//searching the element by tranversal
int search(struct node *head,int num)
{
while(head!=NULL)
{
if(head->data==num)
{return 1;
}
head=head->next;
}
return 0;
}
//union of two link list
struct node*findunion(struct node *linklistone,struct node *linklisttwo)
{
unionll=NULL;
//temping adding linklist one node to unionll
struct node *temp=linklistone;
while(temp!=NULL)
{
insert(&unionll, temp->data);
temp=temp->next;
}
//insert the nodes from linklist two that are not in linklistone
while(linklisttwo!=NULL){
if(!search(linklistone,linklisttwo->data)){
insert(&unionll,linklisttwo->data);
}
linklisttwo=linklisttwo->next;
}
return unionll;
}
//finding intersection of the two link list
struct node* intersection(struct node *linklistone,struct node *linklisttwo){
intersectionll=NULL;
while(linklistone!=NULL)
{
if(search(linklisttwo,linklistone->data)){
insert(&intersectionll,linklistone->data);
}
linklistone=linklistone->next;
}
return intersectionll;
}
//function used to print the link list
void printLL(struct node *nodeptr)
{
while(nodeptr!=NULL){
printf("%d",nodeptr->data);
nodeptr=nodeptr->next;
if(nodeptr!=NULL)
printf("--->");
}
return unionll;
}
//finding intersection of the two link list
struct node* intersection(struct node *linklistone,struct node *linklisttwo){
intersectionll=NULL;
while(linklistone!=NULL)
{
if(search(linklisttwo,linklistone->data)){
insert(&intersectionll,linklistone->data);
}
linklistone=linklistone->next;
}
return intersectionll;
}
//function used to print the link list
void printLL(struct node *nodeptr)
{
while(nodeptr!=NULL){
printf("%d",nodeptr->data);
nodeptr=nodeptr->next;
if(nodeptr!=NULL)
printf("--->");
}
}
int main()
{
int i,linklist1count,linklist2count,temp;
//creating first set of link list
printf("Enter no. of nodes in 1 st node:\n");
scanf("%d",&linklist1count);
printf("Enter the %d integer\n",linklist1count);
for(i=0;i<linklist1count;i++)
{
scanf("%d",&temp);
insert(&linklistone,temp);
}
printLL(linklisttwo);
//union of the two list function calling
findunion (linklistone,linklisttwo);
printf("/n union of the two set\n");
intersection(linklistone,linklisttwo);
printf("\n intersection of the two set\n");
printLL(intersectionll);
return 0;
}
output:
2
3
4
Enter no. of nodes in 1 st node:
Enter the 2 integer
/n union of the two set
intersection of the two set