forked from ianshulx/DSA-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairwise_swap_nodes.cpp
84 lines (69 loc) · 1.47 KB
/
pairwise_swap_nodes.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
// C++ program to pairwise swap elements
// in a given linked list
#include <bits/stdc++.h>
using namespace std;
/* A linked list node */
class Node {
public:
int data;
Node* next;
};
/* Function to pairwise swap elements
of a linked list */
void pairWiseSwap(Node* head)
{
Node* temp = head;
/* Traverse further only if
there are at-least two nodes left */
while (temp != NULL && temp->next != NULL) {
/* Swap data of node with
its next node's data */
swap(temp->data,
temp->next->data);
/* Move temp by 2 for the next pair */
temp = temp->next->next;
}
}
/* Function to add a node at the
beginning of Linked List */
void push(Node** head_ref, int new_data)
{
/* allocate node */
Node* new_node = new Node();
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point
to the new node */
(*head_ref) = new_node;
}
/* Function to print nodes
in a given linked list */
void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}
// Driver Code
int main()
{
Node* start = NULL;
/* The constructed linked list is:
1->2->3->4->5 */
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);
cout << "Linked list "
<< "before calling pairWiseSwap()\n";
printList(start);
pairWiseSwap(start);
cout << "\nLinked list "
<< "after calling pairWiseSwap()\n";
printList(start);
return 0;
}