0% found this document useful (0 votes)
40 views23 pages

Shivam Sharma Discrete Lab File 1

The document describes a program that performs set operations like union, intersection, and difference on two sets represented as linked lists. It includes source code to define a node structure, functions to add elements to sets, check presence, print sets, and perform the various set operations. The output shows sample runs of the program.

Uploaded by

Aman Mourya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views23 pages

Shivam Sharma Discrete Lab File 1

The document describes a program that performs set operations like union, intersection, and difference on two sets represented as linked lists. It includes source code to define a node structure, functions to add elements to sets, check presence, print sets, and perform the various set operations. The output shows sample runs of the program.

Uploaded by

Aman Mourya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

RAJKIYA ENGINEERING COLLEGE

KANNAUJ

Topic:
KCS-353 Lab Experiment Part-1

Under the Guidance of:


Mr Naveen Tiwari

Made by:
Shivam Sharma
1908390100058
Computer Science and Engineering
2nd Year
Experiment 1
OBJECTIVE :-
Write a program in C to create two sets and perform the Union
operation on sets using Linked List

Source Code

#include <stdio.h> if(!ispresent(head1,value))

#include <stdlib.h> push(&head1, value);

struct node printf("Want to enter more values


press(0,1)");
{
scanf("%d",&choice);
int data;
}
struct node* next;
printf("Elements in first set are \n");
};

void push(struct node**, int); int print(head1);

ispresent(struct node*, int); printf("\nEnter values of second set\


n");
struct node* Union( struct node*,
struct node*); choice=1;

void print(struct node* temp); while(choice){

struct node* head1 = NULL; struct printf("Enter value: ");

node* head2 = NULL; struct node* scanf("%d",&value);

head3 = NULL; struct node* if(!ispresent(head2,value))

newnode; int main() push(&head2, value);

printf("Want to enter more


values press(0,1)");
{
scanf("%d",&choice);
int choice=1,value;
}
printf("Enter values of first set\
n"); printf("Elements in second set are \n");

while(choice){
print(head2);
printf("Enter value: ");
head3 = Union(head1, head2);
scanf("%d",&value);

<Exp 1><Page 1><Roll No. 58>


printf("\n Elements of Union are \n"); {

while (temp != NULL) { printf("%d


print(head3);
", temp->data); temp = temp-
return 0;
>next;
}
}
struct node* Union( struct node* head1,
struct node* head2) }

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

struct node* result = NULL;


{
struct node *temp1 = head1, *temp2 =
head2; struct node* temp = head;

while (temp1 != NULL) while (temp != NULL) {

{ push(&result, temp1->data); if (temp->data == data)

temp1 = temp1->next; return 1;

} temp = temp->next;

while (temp2 != NULL) { }

if (!ispresent(result, temp2- return 0;


>data))
}
push(&result, temp2->data);

temp2 = temp2->next;

return result;

void push(struct node** head,int


new_data)

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

newnode->data = new_data;

newnode->next = *head;

*head = newnode;

void print(struct node* temp)

<Exp 1><Page 2><Roll No. 58>


OUTPUT Elements in second set are

683
Enter values of first set

Enter value: 2 Elements of Union are

984236
Want to enter more values press(0,1)1

Enter value: 4

Want to enter more values press(0,1)1

Enter value: 8

Want to enter more values press(0,1)1

Enter value: 9

Want to enter more values press(0,1)0

Elements in first set are 9 8 4 2

Enter values of second set

Enter value: 3

Want to enter more values press(0,1)1

Enter value: 8

Want to enter more values press(0,1)1

Enter value: 6

Want to enter more values press(0,1)1

Enter value: 3

Want to enter more values press(0,1)0

<Exp 1><Page 3><Roll No. 58>


Experiment 2
OBJECTIVE :-
Write a program in C to create two sets and perform the
Intersection operation on sets using Linked List.

Source Code

#include <stdio.h> if(!ispresent(head1,value))

#include <stdlib.h> push(&head1, value);

struct node printf("Want to enter more


values press(0,1)");
{
scanf("%d",&choice);
int data;
}
struct node* next;
printf("Elements in first set are \n");
};

void push(struct node**, int); int print(head1);

ispresent(struct node*, int); printf("\nEnter values of second set\


n");
struct node* Intersection( struct
node*, struct node*); choice=1;
void print(struct node* temp); while(choice){

struct node* head1 = NULL; struct printf("Enter value: ");

node* head2 = NULL; struct node* scanf("%d",&value);

head3 = NULL; struct node* if(!ispresent(head2,value))

newnode; int main() push(&head2, value);

printf("Want to enter more


values press(0,1)");
{
scanf("%d",&choice);
int choice=1,value;
}
printf("Enter values of first set\
n"); printf("Elements in second set are \n");
while(choice){
print(head2);
printf("Enter value: ");
head3 = Intersection(head1, head2);
scanf("%d",&value);

<Exp 2><Page 4><Roll No. 58>


printf("%d ", temp->data);

printf("\n Elements of Intersection are \ temp = temp->next;


n");
}
print(head3);
}

int ispresent(struct node* head, int data)


return 0;

} {

struct node* Intersection(struct node* struct node* temp = head;


head1,struct node* head2)
while (temp != NULL) {
{
if (temp->data == data)
struct node* result = NULL;
return 1;
struct node* temp1 = head1;
temp = temp->next;
while (temp1 != NULL) {
}
if (ispresent(head2, temp1-
>data)) return 0;

push(&result, temp1->data); }

temp1 = temp1->next;

return result;

void push(struct node** head,int


new_data)

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

newnode->data = new_data;

newnode->next = *head;

*head = newnode;

void print(struct node* temp)

while (temp != NULL) {

<Exp 2><Page 5><Roll No. 58>


OUTPUT Enter values of second set

Enter value: 3
Enter values of first set
Want to enter more values press(0,1)1
Enter value: 2
Enter value: 8
Want to enter more values press(0,1)1
Want to enter more values press(0,1)1
Enter value: 4
Enter value: 6
Want to enter more values press(0,1)1

Enter value: 8 Want to enter more values press(0,1)1

Enter value: 3
Want to enter more values press(0,1)1

Enter value: 9 Want to enter more values press(0,1)0

Want to enter more values press(0,1)0 Elements in second set are 3 6 8 3

Elements in first set are 9 8 4 2


Elements of Intersection are

<Exp 2><Page 6><Roll No. 58>


Experiment 3
OBJECTIVE :-
Write a program in C to create two sets and perform the difference
on sets using Linked List.

Source Code

#include <stdio.h> scanf("%d",&value);

#include <stdlib.h> if(!ispresent(head1,value))

struct node push(&head1, value);

{ printf("Want to enter more


values press(0,1)");
int data;
scanf("%d",&choice);
struct node* next;
}
};
printf("Elements in first set
void push(struct node**, int);
are \n"); print(head1);
int ispresent(struct node*, int);
printf("\nEnter values of second set\n");
struct node* diff( struct node*, struct
choice=1;
node*); void print(struct node* temp);
while(choice){
struct node* head1 = NULL;
printf("Enter value: ");
struct node* head2 = NULL;
scanf("%d",&value);
struct node* head3 = NULL;
if(!ispresent(head2,value))
struct node* newnode;
push(&head2, value);
int main()
printf("Want to enter more
{ values press(0,1)");
int choice=1,value; scanf("%d",&choice);
printf("Enter values of first }
set\n"); while(choice){ printf("Elements in second set
printf("Enter value: "); are \n"); print(head2);

<Exp 3><Page 7><Roll No. 58>


head3 = diff(head1, head2); void print(struct node* temp)

printf("\n Elements of diff while (temp != NULL) {

are \n"); print(head3); printf("%d ", temp->data);

temp = temp->next;

return 0; }

} }

struct node* diff(struct node* int ispresent(struct node* head, int data)
head1,struct node* head2)
{
{
struct node* temp = head;
struct node* result = NULL;
while (temp != NULL) {
struct node *temp1 = head1,
*temp2 = head2; if (temp->data == data)

while (temp1 != NULL) { return 1;

if (!ispresent(head2, temp1->data)) temp = temp->next;

push(&result, temp1->data); }

temp1 = temp1->next; return 0;

} }

return result;

void push(struct node** head,int new_data)

newnode = (struct node*)malloc(


sizeof(struct node));

newnode->data =

new_data; newnode->next

= *head; *head = newnode;

<Exp 3><Page 8><Roll No. 58>


OUTPUT
Enter values of first set

Enter values of first set

Enter value: 2

Want to enter more values press(0,1)1

Enter value: 2

Want to enter more values press(0,1)1

Enter value: 8

Want to enter more values press(0,1)1

Enter value: 9

Want to enter more values press(0,1)0

Elements in first set are

982

Enter values of second set

Enter value: 2

Want to enter more values press(0,1)1

Enter value: 8

Want to enter more values press(0,1)0

Elements in second set are

82

Elements of diff are

<Exp 3><Page 9><Roll No. 58>


Experiment 4
OBJECTIVE :-
Write a program in C to create two sets and perform the Symmetric
difference on sets using Linked List.

Source Code

#include <stdio.h> printf("Enter value: ");

#include <stdlib.h> scanf("%d",&value); if(!

struct node ispresent(head1,value))

{ push(&head1, value);

int data; printf("Want to enter more


values press(0,1)");
struct node* next;
scanf("%d",&choice);
};
}
void push(struct node**, int); int
printf("Elements in first set are \
ispresent(struct node*, int); n");
struct node* diff( struct node*, struct print(head1);
node*);
printf("\nEnter values of
void print(struct node* temp); second set\n");
struct node* head1 = NULL; struct choice=1;
node* head2 = NULL; struct node* while(choice){
head3 = NULL; struct node* printf("Enter value: ");

newnode; int main() scanf("%d",&value);

if(!ispresent(head2,value))
{ push(&head2, value);
int choice=1,value; printf("Want to enter more
values press(0,1)");
printf("Enter values of first set\
n"); scanf("%d",&choice);
while(choice){

<Exp 4><Page 10><Roll No. 58>


} return result;
printf("Elements in second set are }
\n");

print(head2);
void push(struct node** head,int
head3 = diff(head1, head2); new_data)

{
printf("\n Elements of diff are \n"); newnode = (struct
node*)malloc( sizeof(struct node));
print(head3); newnode->data = new_data;

newnode->next = *head;
return 0; *head = newnode;
} }
struct node* diff(struct node* void print(struct node* temp)
head1,struct node* head2)
{
{
while (temp != NULL) { printf("%d
struct node* result = NULL;
", temp->data); temp = temp-
struct node *temp1 = head1,
*temp2 = head2; >next;

while (temp1 != NULL) { }

if (!ispresent(head2, }
temp1->data))
int ispresent(struct node* head, int
push(&result, temp1- data)
>data);
{
temp1 = temp1->next;
struct node* temp = head;
}
while (temp != NULL) {
while (temp2 != NULL) {
if (temp->data == data)
if (!ispresent(head1,
temp2->data)) return 1;

push(&result, temp2- temp = temp->next;


>data);
}
temp2 = temp2->next;
return 0; }
}

<Exp 4><Page 11><Roll No. 58>


Want to enter more values press(0,1)1
OUTPUT
Enter values of first set Enter value: 4

Want to enter more values press(0,1)1


Enter value: 1

Want to enter more values Enter value: 5

Want to enter more values press(0,1)0


press(0,1)1 Enter value: 2

Want to enter more values Elements in second set are

press(0,1)1 Enter value: 3 543

Want to enter more values Elements of diff are

press(0,1)0 Elements in first set 4512

are 3 2 1

Enter values of second set

Enter value: 3

<Exp 4><Page 12><Roll No. 58>


Experiment 5
OBJECTIVE :-
Write a program in C to create two sets and perform the Powerset
operation on sets using Linked List.

Source Code

#include <stdio.h> if(!ispresent(head1,value))

#include <stdlib.h> push(&head1, value);

#include<math.h> printf("Want to enter more


values press(0,1)");
struct node
scanf("%d",&choice);
{
}
int data;
printf("Elements in first set are \
struct node* next; n");
}; print(head1);
void push(struct node**, int); int struct node *shiv = head1; int
ispresent(struct node*, int); listcount = 0; while (shiv !=
struct node *Power (struct node *, int); NULL)

{
void print(struct node* temp);
shiv = shiv->next;
struct node* head1 = NULL; struct
listcount++;
node* newnode; int main()
}

Power( head1, listcount);


{
return 0;
int choice=1,value;
}
printf("Enter values of first set\
n"); struct node *

while(choice){ Power (struct node *head, int n)

printf("Enter value: "); {

scanf("%d",&value); struct node *p = head;

<Exp 5><Page 13><Roll No. 58>


struct node *q = head; newnode->next = *head;
printf ("\n"); *head = newnode;
int set_lenth = pow (2, n); }
for (int i = 0; i < set_lenth; i++) void print(struct node* temp)

{
{
while (temp != NULL) { printf("%d
int m = 0;
", temp->data); temp = temp-
printf ("{");
>next;
for (int j = 0; j < n; j++)
}
{
}
p = head;
int ispresent(struct node* head, int
for (int k = 0; k < j; k++) data)
{ {
p = p->next; struct node* temp = head;
} while (temp != NULL) {
if (i & (1 << j)) if (temp->data == data)
{ return 1;
printf ("%d", p->data); if temp = temp->next;
(j != n - 1) }
printf (" "); return 0; }
}

printf ("}\n");

}void push(struct node** head,int


new_data)

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

newnode->data = new_data;

<Exp 5><Page 14><Roll No. 43>


OUTPUT {2 7 }

Enter values of first set {1 2 7 }

Enter value: 6 {9 7 }

Want to enter more values {1 9 7 }

press(0,1)1 Enter value: 7 {2 9 7 }

Want to enter more values {1 2 9 7 }

press(0,1)1 Enter value: 9 {6}

Want to enter more values {1 6}

press(0,1)1 Enter value: 2 {2 6}

Want to enter more values {1 2 6}

press(0,1)1 Enter value: 1 {9 6}

Want to enter more values {1 9 6}

press(0,1)0 Elements in first set {2 9 6}

are 1 2 9 7 6 {1 2 9 6}

{} {7 6}

{1 } {1 7 6}

{2 } {2 7 6}

{1 2 } {1 2 7 6}

{9 } {9 7 6}

{1 9 } {1 9 7 6}

{2 9 } {2 9 7 6}

{1 2 9 } {1 2 9 7 6}

{7 }

{1 7 }

<Exp 5><Page 15><Roll No. 58>


Experiment 6
OBJECTIVE :-
Write a program in C to Display the Boolean Truth Table for AND,
OR , NOT by using Linked List.

Source Code

#include <stdio.h> push(&head1, value);

#include <stdlib.h> printf("Want to enter more


values press(0,1)");
struct node
scanf("%d",&choice);
{
}
int data;
printf("Elements in first set are \n");
struct node* next;

}; print(head1);

void push(struct node**, int); int printf("\nEnter values of second set\


n");
ispresent(struct node*, int);
choice=1;
struct node* Union( struct node*,
struct node*); while(choice){
void print(struct node* temp); printf("Enter value: ");

struct node* head1 = NULL; struct scanf("%d",&value);

node* head2 = NULL; struct node push(&head2, value);

*temp1,*temp2; struct node* printf("Want to enter more


values press(0,1)");
newnode; int main()
scanf("%d",&choice);

}
{
printf("Elements in second set are \n");
int choice=1,value;

printf("Enter values of first set\ print(head2);


n");
printf("\n");
while(choice){
temp1=head1;
printf("Enter value: ");
temp2=head2;
scanf("%d",&value);

<Exp 6><Page 16><Roll No. 58>


while (temp1 != NULL && *head = newnode;
temp2!=NULL) {
}
printf("AND operation of %d and
%d is %d\n",temp1->data,temp2- void print(struct node* temp)
>data,temp1->data & temp2->data);
{
printf("OR operation of %d and
%d is %d\n",temp1->data,temp2- while (temp != NULL) { printf("%d
>data,temp1->data | temp2->data);
", temp->data); temp = temp-
printf("NOT operation of %d is %d\
n",temp1->data,(temp1->data==0)?1:0); >next;

}
printf("NOT operation of %d is %d\
}
n",temp2->data,(temp2->data==0)?1:0);
int ispresent(struct node* head, int data)
temp2 = temp2->next;
{
temp1 = temp1->next;
struct node* temp = head;
}
while (temp != NULL) {
}
if (temp->data == data)
void push(struct node** head,int
new_data) return 1;
{ temp = temp->next;
newnode = (struct }
node*)malloc( sizeof(struct node));
return 0;
newnode->data = new_data;
newnode->next = *head; }

<Exp 6><Page 17><Roll No. 58>


Output
Enter values of first set Want to enter more values press(0,1)0

Enter value: 1 Elements in second set are

Want to enter more values 0 1 0 AND operation of 1 and 0 is 0

press(0,1)1 Enter value: 0 OR operation of 1 and 0 is 1

Want to enter more values NOT operation of 1 is 0

press(0,1)1 Enter value: 1 NOT operation of 0 is 1

Want to enter more values AND operation of 0 and 1 is 0

press(0,1)0 Elements in first set OR operation of 0 and 1 is 1

are 1 0 1 NOT operation of 0 is 1

Enter values of second set NOT operation of 1 is 0

Enter value: 0 AND operation of 1 and 0 is 0

Want to enter more values OR operation of 1 and 0 is 1

press(0,1)1 Enter value: 1 NOT operation of 1 is 0

Want to enter more values NOT operation of 0 is 1

press(0,1)0 1 Enter value: 0

<Exp 6><Page 18><Roll No. 58>


Experiment 7
OBJECTIVE :-
Write a program in C to Display the Cartesian Product of 2 sets by
using Linked List.

Source Code

#include <stdbool.h> scanf("%d",&value);

#include <stdio.h> push(&head1, value);

#include <stdlib.h> printf("Want to enter more


values press(0,1)");
struct node
scanf("%d",&choice);
{
}
int data;
printf("Elements in first set
struct node* next; are \n");

}; print(head1);

void push(struct node**, int); printf("\nEnter values of


second set\n");
int ispresent(struct node*, int);
choice=1;
void Cartesian( struct node*,
struct node*); while(choice){

void print(struct node* temp); printf("Enter value: ");

struct node* head1 = NULL; scanf("%d",&value);

struct node* head2 = NULL; push(&head2, value);

struct node* newnode; printf("Want to enter more


values press(0,1)");
int main()
scanf("%d",&choice);
{
}
int choice=1,value;
printf("Elements in second set
printf("Enter values of first
are \n");
set\n");
print(head2);
while(choice){
printf("\n Elements of
printf("Enter value: ");
Cartesian are \n");

<Exp 7><Page 19><Roll No. 58>


Cartesian(head1, head2); void print(struct node* temp)
return 0; {
} while (temp != NULL) { printf("%d
void Cartesian( struct node* head1, ", temp->data); temp = temp-
struct node* head2)
>next;
{
}
struct node* result = NULL;
}
struct node *temp1 = head1,
*temp2 = head2; int ispresent(struct node* head, int
data)
while (temp1 != NULL) {
{
temp2=head2;
struct node* temp = head;
while(temp2!=NULL){
while (temp != NULL) {
printf("(");
if (temp->data == data)
printf("%d %d",temp1-
>data,temp2->data); return 1;

printf(")"); temp = temp->next;

temp2 = temp2->next; }

} return 0;

temp1 = temp1->next; }

void push(struct node** head,int


new_data)

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

newnode->data = new_data;

newnode->next = *head;

*head = newnode;

<Exp 7><Page 20><Roll No. 58>


Output
Enter values of first set

Enter value: 1

Want to enter more values press(0,1)1

Enter value: 2

Want to enter more values press(0,1)0

Elements in first set are

21

Enter values of second set

Enter value: 1 5

Want to enter more values press(0,1)1

Enter value: 6

Want to enter more values press(0,1)0

Elements in second set are

65

Elements of Cartesian are

(2 6)(2 5)(1 6)(1 5)

<Exp 7><Page 21><Roll No. 58>

You might also like