-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathdifference.cpp
61 lines (50 loc) · 1.51 KB
/
difference.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include "basicActions.h"
using namespace std;
// Driver Function.
Node *findDifference(Node *head1, Node *head2)
{
// Setting two Temporary "Node" Pointers to traverse the lists..
Node *p = head1, *q = head2, *head;
// Allocation of Memory for new Node.
Node *temp = new Node();
// Setting Node's Data .
temp->data = (p->data) - (q->data);
temp->next = nullptr;
// Pushing the pointers 'p' and 'q' forward traversing the lists.
p = p->next;
q = q->next;
// Setting the head Pointer.
head = temp;
// Traversing Both the lists at the same time.
while (p != nullptr && q != NULL)
{
// Insert the difference of the data elements of two lists at the end.
insertAtEnd(head, (p->data - q->data));
// Pushing the pointers forward.
p = p->next;
q = q->next;
}
// returning the head of the Newly made List.
return head;
}
int main()
{
// Three Heads for three Linked Lists.
Node *head1 = nullptr;
Node *head2 = nullptr;
Node *head = nullptr;
// Creation and Insertion of First List Data Elements.
insertAtEnd(head1, 6);
insertAtEnd(head1, 5);
insertAtEnd(head1, 3);
insertAtEnd(head1, 0);
// Creating and Insertion of Second List Data Elements.
insertAtEnd(head2, 4);
insertAtEnd(head2, 5);
insertAtEnd(head2, 2);
insertAtEnd(head2, 0);
// Calling the Driver Function.
head = findDifference(head1, head2);
displayNodes(head);
}