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>
struct node{ // node structure
int data;
struct node* link;
};
struct node *new_node(){ // Create new node
struct node* new = malloc(sizeof(struct node));
return new;
}
struct node* addtoempty(int data){ // add a node to an empty list
struct node* temp = malloc(sizeof(struct node));
temp->data = data;
temp->link = NULL;
return temp;
}
struct node* addtoend(struct node* head,int data){ // add a node at end
struct node* newnode = malloc(sizeof(struct node));
newnode->data = data;
newnode->link = NULL;
struct node* temp = head;
while(temp->link!=NULL)
temp = temp->link;
temp->link = newnode;
return 0;
}
struct node* insert(struct node *head , int data){ // insert a node at sorted place
struct node* newnode = malloc(sizeof(struct node));
newnode->data = data;
newnode->link = NULL;
struct node* temp;
int key = data;
if(head==NULL || key<head->data){
newnode->link = head;
head = newnode;
}
else{
temp = head;
while(temp->link!=NULL && temp->link->data<key)
temp = temp->link;
newnode->link = temp->link;
temp->link = newnode;
}
return head;
}
insert_begining(struct node **list,int data){ // Insert node at begining
struct node* temp;
temp = new_node();
temp->data = data;
temp->link = *list;
*list = temp;
}
insert_end(struct node **list, int data){ // Insert node at the end
struct node *temp;
struct node *newnode;
temp = *list;
if(*list == NULL)
insert_begining(&(*list),data);
else{
while(temp->link!=NULL)
temp=temp->link;
newnode = new_node();
newnode->data = data;
newnode->link = NULL;
temp->link = newnode;
}
}
struct node* unionoflist(struct node* head1 , struct node* head2){ // Union Function
struct node* list1 = head1;
struct node* list2 = head2;
struct node* list3 = NULL;
while(list1!=NULL && list2!=NULL){
if(list1->data==list2->data){
insert_end(&list3,list1->data);
list1=list1->link;
list2=list2->link;
}
else{
if(list1->data<list2->data){
insert_end(&list3,list1->data);
list1=list1->link;
}
else{
insert_end(&list3,list2->data);
list2=list2->link;
}
}
}
while(list1!=NULL){
insert_end(&list3,list1->data);
list1=list1->link;
}
while(list2!=NULL){
insert_end(&list3,list2->data);
list2=list2->link;
}
return list3;
}
struct node* Intersection(struct node* head1 , struct node* head2){ // Intersection function
struct node* list1 = head1;
struct node* list2 = head2;
struct node* list3 = NULL;
while(list1!=NULL && list2!=NULL){
if(list1->data==list2->data){
insert_end(&list3,list1->data);
list1=list1->link;
list2=list2->link;
}
else{
if(list1->data<list2->data){
list1=list1->link;
}
else{
list2=list2->link;
}
}
}
return list3;
}
void printlist(struct node* head){ // printing linked list
struct node* temp=head;
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->link;
printf("\n");
}
printf("\n");
}
int main() // main function
{
struct node* head1 =NULL;
printf("\nEnter 5 elements for list1:\n");
fflush(stdout);
int temp1;
scanf("%d",&temp1);
head1 = addtoempty(temp1);
for(int i=0;i<4;i++){
scanf("%d",&temp1);
head1 = insert(head1,temp1);
}
struct node* head2 =NULL;
printf("\nEnter 5 elements for list2:\n");
fflush(stdout);
int temp2;
scanf("%d",&temp2);
head2 = addtoempty(temp2);
for(int i=0;i<4;i++){
scanf("%d",&temp2);
head2 = insert(head2,temp2);
}
struct node* head3=NULL;
head3 = unionoflist(head1,head2);
printf("\nUnion of 2 lists is:\n");
printlist(head3);
struct node* head4=NULL;
head4 = Intersection(head1,head2);
printf("\nIntersection of 2 lists is:\n");
printlist(head4);
return 0;
}