Shivam Sharma Discrete Lab File 1
Shivam Sharma Discrete Lab File 1
KANNAUJ
Topic:
KCS-353 Lab Experiment Part-1
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
while(choice){
print(head2);
printf("Enter value: ");
head3 = Union(head1, head2);
scanf("%d",&value);
} temp = temp->next;
temp2 = temp2->next;
return result;
newnode = (struct
node*)malloc( sizeof(struct node));
newnode->data = new_data;
newnode->next = *head;
*head = newnode;
683
Enter values of first set
984236
Want to enter more values press(0,1)1
Enter value: 4
Enter value: 8
Enter value: 9
Enter value: 3
Enter value: 8
Enter value: 6
Enter value: 3
Source Code
} {
push(&result, temp1->data); }
temp1 = temp1->next;
return result;
newnode = (struct
node*)malloc( sizeof(struct node));
newnode->data = new_data;
newnode->next = *head;
*head = newnode;
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: 3
Want to enter more values press(0,1)1
Source Code
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)
push(&result, temp1->data); }
} }
return result;
newnode->data =
new_data; newnode->next
Enter value: 2
Enter value: 2
Enter value: 8
Enter value: 9
982
Enter value: 2
Enter value: 8
82
Source Code
{ push(&head1, 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){
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;
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;
are 3 2 1
Enter value: 3
Source Code
{
void print(struct node* temp);
shiv = shiv->next;
struct node* head1 = NULL; struct
listcount++;
node* newnode; int main()
}
{
{
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");
newnode = (struct
node*)malloc( sizeof(struct node));
newnode->data = new_data;
Enter value: 6 {9 7 }
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 }
Source Code
}; print(head1);
}
{
printf("Elements in second set are \n");
int choice=1,value;
}
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; }
Source Code
}; print(head1);
temp2 = temp2->next; }
} return 0;
temp1 = temp1->next; }
newnode = (struct
node*)malloc( sizeof(struct node));
newnode->data = new_data;
newnode->next = *head;
*head = newnode;
Enter value: 1
Enter value: 2
21
Enter value: 1 5
Enter value: 6
65