Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

/*
* essencials of c project1.c
*
* Created on: 19 Dec 2022
* Author: shahbaz
*/

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


struct node
{
int num; // value member
struct node *nextptr; // next element address member
};

struct node *list_1;
struct node *list_2;

void displayList(struct node *list); //function prototyping
void Intersection(struct node *,struct node *);
void insertnode(struct node *list, int val);
bool isPresent(struct node* head, int data);
void Union(struct node* head1, struct node* head2);

int main(){ // main function
int n,val,i,j,num;
printf("creating 1st list\n");
printf("Input for number of nodes");
fflush(stdout);
scanf("%d",&n);
list_1=(struct node *)malloc(sizeof(struct node));
printf("Input data for node 1");
fflush(stdout);
scanf("%d",&num);
list_1->num=num;
list_1->nextptr=NULL;
for(j=2;j<=n;j++){
printf("enter the node%d:",j);
fflush(stdout);
scanf("%d",&val);
insertnode(list_1,val);
}
displayList(list_1);

printf("creating 2nd list\n");
printf("Input for number of nodes");
fflush(stdout);
scanf("%d",&n);
list_2=(struct node *)malloc(sizeof(struct node));
printf("Input data for node 1");
fflush(stdout);
scanf("%d",&num);
list_2->num=num;
list_2->nextptr=NULL;
for(i=2;i<=n;i++){
printf("enter the node%d:",i);
fflush(stdout);
scanf("%d",&val);
insertnode(list_2,val);
}
displayList(list_2);

Intersection(list_1, list_2);
Union(list_1, list_2);
}


void displayList(struct node *list){ //A utility function to display list entities
struct node *tmp;
if(list==NULL){
printf("List is empty");
}
else{
tmp=list;
while(tmp!=NULL)
{
printf("Data = %d\n",tmp->num);
tmp=tmp->nextptr;
}
}
}

void insertnode(struct node *head,int val){ //A utility function to insert a node at the end of a linked list
struct node *tmp, *new;
new = (struct node *)malloc(sizeof(struct node));
new->num=val;
new->nextptr= NULL;

tmp= head;
while(tmp->nextptr!=NULL){
tmp=tmp->nextptr;
}
tmp->nextptr=new;
}

void Intersection(struct node* head1,struct node* head2){ //Function to get intersection of two linked lists head1 and head2
struct node* t1 = head1;
printf("intersection of list a & b: ");
while (t1 != NULL) {
if (isPresent(head2, t1->num)){
printf("%d ,",t1->num);
}
t1 = t1->nextptr;
}

}

void Union(struct node* head1, struct node* head2){ //Function to get union of two linked lists head1 and head2
struct node *t1 = head1, *t2 = head2;
printf("\nUnion of list a&b: ");

while (t1 != NULL) {
printf("%d ,",t1->num);
t1 = t1->nextptr;
}

while (t2 != NULL) {
if (!isPresent(head1, t2->num)){
printf("%d ,",t2->num);
}
t2 = t2->nextptr;
}

}


bool isPresent(struct node* head, int data) //A utility function to compare each element of each list
{
struct node* t = head;
while (t != NULL) {
if (t->num == data)
return 1;
t = t->nextptr;
}
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.