-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpair-swap-by-value.cpp
85 lines (74 loc) · 1.81 KB
/
pair-swap-by-value.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// http://www.geeksforgeeks.org/pairwise-swap-elements-of-a-given-linked-list/
// this program swaps the data of two nodes
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
void pairSwapRecursion(Node *head);
void pairSwapIteration(Node *head);
void swap(Node *a, Node *b);
Node* addNode(Node *head, int data);
void printList(Node *head);
void pairSwapRecursion(Node *head) {
// there must be atleast two nodes in the list
if (head == NULL || head->next == NULL)
return;
swap(head, head->next);
pairSwapRecursion(head->next->next);
}
void pairSwapIteration(Node *head) {
// swap data of nodes till there are two nodes in the list
while (head != NULL && head->next != NULL) {
swap(head, head->next);
head = head->next->next;
}
}
void swap(Node *a, Node *b) {
int temp = a->data;
a->data = b->data;
b->data = temp;
}
Node* addNode(Node *head, int data) {
if (head == NULL)
return new Node(data);
Node *ptr = head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new Node(data);
return head;
}
void printList(Node *head) {
if (head == NULL)
return;
while (head != NULL) {
cout<<head->data<<" ";
head = head->next;
}
}
int main() {
Node *head = NULL;
head = addNode(head, 1);
head = addNode(head, 2);
head = addNode(head, 3);
head = addNode(head, 4);
head = addNode(head, 5);
head = addNode(head, 6);
head = addNode(head, 7);
cout<<"original: ";
printList(head);
cout<<endl<<"1st swap (iterative): ";
pairSwapIteration(head);
printList(head);
cout<<endl<<"2nd swap (recursive): ";
pairSwapRecursion(head);
printList(head);
cout<<endl;
return 0;
}