-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked.c
More file actions
76 lines (64 loc) · 1.42 KB
/
linked.c
File metadata and controls
76 lines (64 loc) · 1.42 KB
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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node* insert(struct node *head, int val){
if(head == NULL){
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = val;
newnode->next = NULL;
return newnode;
}
struct node *ptr = head;
while(ptr->next!=NULL)
ptr = ptr->next;
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = val;
newnode->next = NULL;
ptr->next = newnode;
return head;
}
void swap(int *a, int *b){
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
struct node* InterchangeNode(struct node *head){
struct node *ptr=head, *fastptr = head;
while(ptr!=NULL && ptr->next!=NULL)
{
int a = ptr->data;
int b = ptr->next->data;
swap(&a,&b);
ptr->data = a;
ptr->next->data = b;
fastptr = ptr->next->next;
ptr = fastptr;
}
return head;
}
int main(){
struct node *head=NULL;
int i;
srand(time(0));
int range = rand()%100 + 1;
for(i = 1;i<=range;i++)
head = insert(head, rand()%51);
struct node *ptr = head;
printf("Your Linked List is: \n");
while(ptr->next!=NULL){
printf("%d->",ptr->data);
ptr = ptr->next;
}
printf("%d\n",ptr->data);
head=InterchangeNode(head);
ptr = head;
printf("Output Linked List is: \n");
while(ptr->next!=NULL){
printf("%d->",ptr->data);
ptr = ptr->next;
}
printf("%d\n",ptr->data);
}