-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLinked_List_SwapAdjacentNodes.c
69 lines (61 loc) · 1.03 KB
/
Linked_List_SwapAdjacentNodes.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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
void swap(int *, int *);
void PairWiseSwap(struct node *head)
{
struct node *temp = head;
while(temp != NULL && temp->next != NULL)
{
swap(&temp->data,&temp->next->data);
temp = temp->next->next;
}
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
struct node* push(struct node *head,int data)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
head = temp;
else
{
struct node *temp1 = head;
while(temp1->next != NULL)
temp1 = temp1->next;
temp1->next = temp;
}
return head;
}
void Print(struct node *head)
{
while(head != NULL)
{
printf("%d ",head->data);
head = head->next;
}
}
int main()
{
struct node *head =NULL;
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
//head = push(head, 1);
Print(head);
printf("\n");
PairWiseSwap(head);
Print(head);
printf("\n");
}