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>

struct node { // creating a node

int data;

struct node *link;

};

// functions' declaration

struct node* union_lists(struct node*, struct node*);

struct node* intersection_lists(struct node*, struct node*);

struct node* insert_at_end (struct node*, int);

void print_data(struct node*);

//function for printing data in a linked list

void print_data(struct node *head) {

if(head == NULL)

printf("Linked list is empty");

struct node *ptr= NULL;

ptr= head;

while (ptr != NULL)

{

printf("%d\t",ptr->data);

ptr= ptr->link;

}

printf("\n");

}

// function for inserting a node in the linked list at the end

struct node* insert_at_end (struct node *head, int data) {

struct node *ptr, *temp;

ptr = head;

temp=(struct node*)malloc(sizeof(struct node));

//storing the user value in the temp variable

temp->data= data;

temp->link= NULL;

//traversing the list until the end node

while(ptr->link != NULL) {

ptr= ptr->link;

}

ptr->link= temp; // inserting at the end node

head= ptr; // storing the updated head and returning

return head;

}

// defining a function for union of two lists

struct node* union_lists(struct node* list1, struct node*list2) {

struct node *p, *q, *uni_list;// p, q, uni_list are three structure pointers

p=list1;//assigning lists to pointer variable for performing operations

q=list2;

uni_list= (struct node*) malloc(sizeof(struct node)); // allocating memory location

while (p!= NULL && q!= NULL ) { // checking if its not end of list

if ( p->data < q->data) { // calls function if the data is less than list 2

insert_at_end(uni_list, p->data);

p= p->link; // control shifts to next node

}

else if (q->data < p->data) { // same vice versa

insert_at_end(uni_list,q->data);

q= q->link;

}

else { // if both elements are equal, one data is added and pointers of bith the list traverses to next node

insert_at_end(uni_list, q->data);

p=p->link;

q=q->link;

}

}

while (p!= NULL) {// if pointer reaches end of list before another this loop executes and inserts at the end

insert_at_end(uni_list, p->data);

p=p->link;

}

while (q!= NULL) {// similar as above wrt q pointer

insert_at_end(uni_list,q->data);

q= q->link;

}

print_data(uni_list);

return uni_list;// returns the updated list

}

//function for intersection of two lists

struct node* intersection_lists(struct node* list1, struct node*list2) {

struct node *p, *q, *intersec_list;

p= list1;

q= list2;

intersec_list= (struct node*)malloc(sizeof(struct node));

// while not end of lists

while (p!= NULL && q!= NULL) { // if both data are equal insertion will be done in intersec_list and both the pointers are shifted to next node

if (p->data == q->data) {

insert_at_end(intersec_list, p->data);

p= p->link;

q=q->link;

} // else the traversal eill happen in each list untill end

else if (p->data < q->data) {

p= p->link;

}

else {

q=q->link;

}

}

return intersec_list; // returns the updated list which stored the common data in both the lists

}

int main() { // declaring structure pointer variables

struct node *list1;

struct node *list2;

struct node *list3;

struct node *list4;

list1= (struct node *)malloc(sizeof(struct node));

list2= (struct node*)malloc(sizeof(struct node)); // lists 1 and 2 stores the actual lists

list2->data= 445;

list2->link=NULL;

list1->data= 300;

list1->link= NULL;

// creating the list1 by calling the function insert at end

insert_at_end(list1, 350);

insert_at_end(list1, 400);

insert_at_end(list1, 500);

insert_at_end(list1, 600);

insert_at_end(list1, 700);

insert_at_end(list1, 800);

// creating the list 2 by calling the function insert at end

insert_at_end(list2, 500);

insert_at_end(list2, 600);

insert_at_end(list2, 650);

insert_at_end(list2, 750);

// printing the original list

printf("list 1 is : ");

print_data(list1);

printf("list 2 is : ");

print_data(list2);

// calling union and printing the same

list3=union_lists(list1, list2);

printf("union of lists is : \n");

print_data(list3);

//calling the intersection function and printing the same

list4= intersection_lists(list1,list2);

printf("intersection of lists is : \n");

print_data(list4);

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.