Challenge

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

Aim:

Aim of the Project is to create a 2 linked list and combine those linked list with the help of union function and then display the union value and the intersection value.

#include<stdio.h>
#include<stdlib.h>


struct node // creating node by use of self referential structure
{
int value; // value of the node
struct node *next; //address of the node
};

//function prototypes
struct node *insert(struct node *headnode,int n);
void display_list(struct node *head);
struct node *union_node(struct node *h1,struct node *h2);
struct node *intersection_node(struct node *h1,struct node *h2);
int compare(struct node *headnode,int n);

//declaring variables for structure as static
static struct node *h1;
static struct node *h2;
static struct node *head;
static struct node *tail;
static struct node *union_list;
static struct node *intersection;
static struct node *result_union;
static struct node *result_intersection;
static struct node *headnode;
static struct node *newnode;

int main()
{
int total_first; //declaring variables for no of nodes and value of nodes
int total_second;
int first_list_value;
int second_list_value;
printf("\n enter the number of nodes in first list:");
fflush(stdout);
scanf("%d",&total_first);//getting the no of nodes in first list
printf("\n enter the number of nodes in second list:");
fflush(stdout);
scanf("%d",&total_second); //getting no of nodes in second list

int i;
for(i=0;i<total_first;i++) //getting the first list value using for loop
{
printf("\n enter the value:");
fflush(stdout);
scanf("%d",&first_list_value);
h1=insert(h1,first_list_value); /*inserting the node values and address in first list
using insert function*/
}
display_list(h1); //displaying the first list
for(i=0;i<total_second;i++) //getting second list value using for loop
{
printf("\n enter the value:");
fflush(stdout);
scanf("%d",&second_list_value);
h2=insert(h2,second_list_value);/*inserting the node values and address in second list
using insert function*/
}
display_list(h2); //displaying the second List
result_union=union_node(h1,h2); // return union list from union function
printf("\display the union values:");
display_list(result_union); /*combining both list by use of union function
and displaying the value*/

result_intersection = intersection_node(h1,h2);
printf("\n display the intersection value:");
display_list(result_intersection);
return 0;
}

struct node *insert(struct node *headnode,int n) //insert function
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->value=n;
newnode->next=headnode;
headnode=newnode;
return headnode;
}

void display_list(struct node *head) //display function
{
if(head==0)
printf("\n List is empty");
else
tail = head;
while(tail!=0)
{
printf("\n value =%d",tail->value);
tail=tail->next;
}
}

struct node *union_node(struct node *h1,struct node *h2) //union function
{
while(h1!=NULL)
{
union_list=insert(union_list,h1->value);
h1 = h1->next;
}
while(h2!=NULL)
{
if(!(compare(union_list,h2->value)))
{
union_list = insert(union_list,h2-> value);
}
h2 = h2->next;
}
return union_list;
}

struct node *intersection_node(struct node *h1,struct node *h2) //intersection function
{
while(h1!=NULL)
{
if(compare(h2,h1 ->value))
{
intersection=insert(intersection,h1 -> value);
}
h1 =h1->next;
}
return intersection;
}

int compare(struct node *headnode,int n) //compare function
{
while(headnode!=NULL)
{
if((headnode->value)==n)
{
return 1;
}

headnode = headnode ->next;
}
return 0;
}

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.