-
Notifications
You must be signed in to change notification settings - Fork 36
/
cyberRECKON_SwapConsecutiveNodes.c
48 lines (48 loc) · 951 Bytes
/
cyberRECKON_SwapConsecutiveNodes.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
//Swapping two consecutive nodes in linked list
//Language : C
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{ int data;
struct node * next;
}*head=NULL,*temp;
void swap()
{ temp=head;
while(temp!=NULL&&temp->next!=NULL)
{temp->data+=temp->next->data;
temp->next->data=temp->data-temp->next->data;
temp->data-=temp->next->data;
temp=temp->next->next;
}
}
void display()
{ temp=head;
while(temp->next!=NULL)
{printf("%d->",temp->data);
temp=temp->next;
}
printf("%d\n",temp->data);
}
void main()
{ int ch;
clrscr();
do
{ struct node *ptr=(struct node *)malloc(sizeof(struct node*));
ptr->next=NULL;
printf("Enter data : ");
scanf("%d",&ptr->data);
if(head==NULL)
head=temp=ptr;
else
{temp->next=ptr;
temp=ptr;}
printf("Want to enter(1 yes,0 no)?");
scanf("%d",&ch);
}
while(ch==1);
display();
swap();
display();
getch();
}