Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
project-1)
#include<stdio.h>//in preprocessor section include stdio.h to use printf function//
#include<stdlib.h>//in preprocessor section include stdio.h to use malloc//
struct node{// creating a node in linked list//
int data;//integer data of node//
struct node* next;// Pointer pointing towards next node//
}*FirstLinkedList,*SecondLinkedList,*LinkedListIntersection,*LinkedListUnion;
void get(struct node **head,int num)//function to insert a node at the begining of linked list//
{
struct node* newNode = (struct node*)malloc(sizeof(struct node));//a new linked list node allocate//
newNode->data = num;//put num in data of new node//
newNode->next = *head;//next of new node point to head of linked list//
*head = newNode;//head point to new node//
}
int find(struct node *head,int num)//Searches an element in Linked List and return 1 if data is present in linked list else return 0//
{
while(head != NULL)
{
if(head->data == num)
{
return 1;
}
head = head->next;
}
return 0;
}
struct node* unionof(struct node *FirstLinkedList,struct node *SecondLinkedList)//function for union of two linked lists//
{
LinkedListUnion = NULL;
struct node *temp = FirstLinkedList;
while(temp != NULL)//while loop for insert all elements of firstlinkedlist to linkedlistunion//
{
get(&LinkedListUnion,temp->data);
temp = temp->next;
}
while(SecondLinkedList != NULL)//again while loop for insert elements of secondlinkedlist which are not present in linkedlistunion//
{
if(!find(FirstLinkedList,SecondLinkedList->data))
{
get(&LinkedListUnion,SecondLinkedList->data);
}
SecondLinkedList = SecondLinkedList->next;
}
return LinkedListUnion;
}
struct node* intersectionof(struct node *FirstLinkedList,struct node *SecondLinkedList)//function for intersection of two linked lists//
{
LinkedListIntersection = NULL;
while(FirstLinkedList != NULL)//search every elements of FirstLinkedList and SecondLinkedList.if same element found then insert in LinkedListIntersection//
{
if(find(SecondLinkedList,FirstLinkedList->data))
{
get(&LinkedListIntersection,FirstLinkedList->data);
}
FirstLinkedList = FirstLinkedList->next;
}
return LinkedListIntersection;
}
void printlinkedlist(struct node *nodeptr)
{
while(nodeptr != NULL)
{
printf("%d ",nodeptr->data);
fflush(stdout);
nodeptr = nodeptr->next;
}
}
int main()//the main function inside it we have to write our logic//
{
int i,n,m,temp;//integer i,n,m,temp declared//
//Creating First linked List//
printf("enter number of nodes in first linked list :");
fflush(stdout);
scanf("%d",&n);
printf("enter %d integers\n",n);
fflush(stdout);
for(i=0; i<n; i++)
{
printf("enter the value of linked list1 %d:",i);
fflush(stdout);
scanf("%d",&temp);
get(&FirstLinkedList,temp);
}
printf("\nfirst linked list is:");
fflush(stdout);
printlinkedlist(FirstLinkedList);
//Creating second linked List//
printf("\nenter number of nodes in second linked list :");
fflush(stdout);
scanf("%d",&m);
printf("enter %d integers\n",m);
fflush(stdout);
for(i=0; i<m; i++)
{
printf("enter the value of linked list2 %d:",i);
fflush(stdout);
scanf("%d",&temp);
get(&SecondLinkedList,temp);
}
printf("\nsecond linked list is:");
fflush(stdout);
printlinkedlist(SecondLinkedList);
intersectionof(FirstLinkedList,SecondLinkedList);
printf("\n\nintersection linked list is:");
fflush(stdout);
printlinkedlist(LinkedListIntersection);
unionof(FirstLinkedList,SecondLinkedList);
printf("\nunion linked list is:");
fflush(stdout);
printlinkedlist(LinkedListUnion);
return 0;
}
