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>

// list as a structure
typedef struct Node {
int data;
struct Node* next;
}Node;

// function to allocate space and to create a new node
Node* newNode(int data)
{
Node* new_node = (Node *)malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}

//function to add a particular data to a list
void push(Node** head_ref, int new_data)
{
Node* new_node = newNode(new_data);
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

// function to find union of two lists
Node* union_lists(Node* a, Node* b)
{

// initialization of lists
Node *first = a;
Node *second = b;
Node *res = NULL;
Node *temp = NULL;
int flag;
// first data is directly added to output for comparing purpose
push(&res, first->data);
temp = res;

// first list is compared with output and all the data is entered in output only once
while(first != NULL){
while(temp != NULL){
if(first->data == temp->data){
flag = 1;//flagged if the data is already present in the output
}
temp = temp->next;
}
if(flag != 1){
push(&res, first->data);// if flag is 0 then output doesnot contain data
// so data is pushed into output
}
flag = 0;
temp = res;
first = first->next;
}

// second list is compared with output and all the data is entered in output only once
temp = res;
while(second != NULL){
while(temp != NULL){
if(second->data == temp->data){
flag = 1;//flagged if the data is already present in the output
}
temp = temp->next;
}
if(flag != 1){
push(&res, second->data);// if flag is 0 then output doesnot contain data
// so data is pushed into output
}
flag = 0;
temp = res;
second = second->next;
}
fflush(stdout);
return res;
}

//function to find intersection of two lists
Node * intersection(Node *a, Node *b){

//initialization of empty lists
Node *first = a;
Node *second = b;
Node *res = NULL;
Node *temp = NULL;
int flag;

//comparision of first and second lists
while(first != NULL){
while(second != NULL){
if(first->data == second->data){
flag = 1;
//flag will be 1 if the data is in both lists
}
second = second->next;
}
if(flag == 1){
temp = res;
flag = 0;

//if no data in output it is directly pushed
if(temp == NULL){
push(&res, first->data);
flag = 1;
}

// if data is in both lists it is checked again in output
while(temp != NULL){
if(first->data == temp->data){
flag = 1;
}
temp = temp->next;
}
if(flag != 1){
// if data is not in output then it is pushed
push(&res, first->data);
}
}
flag = 0;
second = b;
first = first->next;
}
return res;
}

// function to print the given list
void printList(Node* node)
{
while (node != NULL) {
printf("%d ",node->data);
fflush(stdout);
node = node->next;
}
printf("\n");
fflush(stdout);
}


int main(void)
{

// declare all lists
Node* uni = NULL;
Node* inter = NULL;
Node* first = NULL;
Node* second = NULL;

// commented code for taking lists from user input
/*int n,a;
printf("Enter length of the list: ");
fflush(stdout);
scanf("%d",&n);
while(n>0){
scanf("%d",&a);
push(&first, a);
n-=1;
}
printf("First list is ");
fflush(stdout);
printList(first);
printf("Enter length of the list: ");
fflush(stdout);
scanf("%d",&n);
while(n>0){
scanf("%d",&a);
push(&second, a);
n-=1;
}
printf("Second list is ");
fflush(stdout);
printList(second);
fflush(stdout);*/


// code to initialize first and second lists
push(&first,1);
push(&first,2);
push(&first,3);
push(&first,1);
push(&first,2);

push(&second, 1);
push(&second, 2);
push(&second, 3);
push(&second, 4);
push(&second, 5);
push(&second, 6);
push(&second, 7);
push(&second, 8);

//calling of functions
uni = union_lists(first, second);
fflush(stdout);
printf("union of both lists is ");
printList(uni);
inter = intersection(first, second);
printf("Intersection of two lists is ");
printList(inter);
return 0;
}

output of the code:

output of code using commented code for getting input from user:

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.