-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswap_adjacent_element.c
54 lines (54 loc) · 1.19 KB
/
swap_adjacent_element.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void add(struct node **s,int d)
{
struct node *new=(struct node*)malloc(sizeof(struct node));
new->data=d;
new->next=*s;
*s=new;
}
void swap(struct node **s)
{
if((*s)==NULL||(*s)->next==NULL)
{}
else{
struct node *next1=(struct node*)malloc(sizeof(struct node));
struct node *curr=(struct node*)malloc(sizeof(struct node));
struct node *previous=(struct node*)malloc(sizeof(struct node));
curr=(*s)->next->next;
previous=*s;
*s=(*s)->next;
(*s)->next=previous;
int i=0;
while(curr!=NULL&&curr->next!=NULL)
{
previous->next=curr->next; //200 300 400
previous=curr; //300 100 200
next1=curr->next->next;
curr->next->next=curr; //100 200
curr=next1;
curr=next1; //200 300
}
previous->next=curr;}
}
int main()
{
struct node *s=(struct node*)malloc(sizeof(struct node));
/*adding element at starting*/
add(&s,3);
add(&s,2);
add(&s,1);
add(&s,4);
add(&s,5);
//swap(&s);
while(s!=NULL)
{
printf("%d",s->data);
s=s->next;
}
}