# Difference of two singly linked lists and display the resultant list

#### Problem Description

A ‘C’ program to create two singly linked lists and calculate the difference of two lists and display the resultant list.

#### Source Code

```#include
#include

/* Link list node */
struct node
{
int data;
struct node* next;
};

/* A utility function to insert a node at the beginning of a linked list*/
void push (struct node** head_ref, int new_data);

/* A utilty function to chec if given data is present in a list */
int isPresent (struct node *head, int data);

/* Function to get the union of two linked lists head1 and head2 */

/* Function to get intersection of two linked lists head1 and head2 */
struct node *getDiff (struct node *head1, struct node *head2)
{
struct node *result = NULL;
struct node *t1 = head1;

// Traverse list1 and search each element of it in list2. If the element
// is present in list 2, then insert the element to result
while (t1 != NULL)
{
push (&result, t1->data);
t1 = t1->next;
}

return result;
}

/* A utility function to insert a node at the begining of a linked list*/
void push (struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));

/* put in the data */
new_node->data = new_data;

/* link the old list off the new node */

/* move the head to point to the new node */
}

/* A utility function to print a linked list*/
void printList (struct node *node)
{
while (node != NULL)
{
printf ("%d ", node->data);
node = node->next;
}
}

/* A utility function that returns true if data is present in a linked list
else return false */
int is present (struct node *head, int data)
{
struct node *t = head;
while (t != NULL)
{
if (t->data == data)
return 1;
t = t->next;
}
return 0;
}

void main()
{
/* Start with the empty list */
struct node* head1 = NULL;
struct node* head2 = NULL;
struct node* difference = NULL;
int n,num,i;
clrscr();
/*create a linked lits 10->15->5->20 */
printf("\nEnter total number elements of first list : \n");
scanf("%d",&n);
for(i=0;i
```

### Output

Enter total number elements of first list :
4
enter 1 element : 5
enter 2 element : 8
enter 3 element : 3
enter 4 element : 9
Enter total number elements of second list :
3
enter 1 element : 2
enter 2 element : 8
enter 3 element : 9
First list is
9 3 8 5
Second list is
9 8 2
Difference (elements in first list but not in second list are 🙂
5 3