-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathswap_nodes_in_linkedlist.c
71 lines (65 loc) · 1.51 KB
/
swap_nodes_in_linkedlist.c
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int data;
struct node *next;
} node;
node *insert(node *);
node *swap(node *);
node *output(node *);
int main() {
node *head;
int n, i;
printf("\nEnter the number of elements of the node: ");
scanf("%d", &n);
head = (node*)malloc(sizeof(node));
head->next = NULL;
for (i = 0; i < n; i++) {
head = insert(head);
}
head = swap(head);
head = output(head);
return 0;
}
node* insert(node* head) {
node *temp, *root = head;
while(head->next) {
head = head->next;
}
printf("\nEnter the element of the node: ");
temp = (node *)malloc(sizeof(node));
scanf("%d", &temp->data);
temp->next = NULL;
head->next = temp;
return root;
}
node* output(node* head) {
node *root = head;
while(head) {
printf("%d ", head->data);
head = head->next;
}
return root;
}
node* swap(node* head) {
head = head->next;
node *temp1, *temp2, *root = head->next;
while(head && head->next) {
temp1 = head->next;
temp2 = temp1->next;
if(temp2==NULL) {
temp1->next = head;
head->next = NULL;
break;
}
if(temp2->next==NULL) {
temp1->next = head;
head->next = temp2;
break;
}
temp1->next = head;
head->next = temp2->next;
head = temp2;
}
return root;
}