-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab5_3.c
69 lines (68 loc) · 1.42 KB
/
lab5_3.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
//Write a C code that exchanges data of every node with its next node, starting from the first node in a single linked list.
//Example: Input: 1->2->3->4->5->6->7, Output: 2->1->4->3->6->5->7
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} * first;
void create_linkedlist(int a)
{
struct node *temp, *ptr;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = a;
temp->next = NULL;
if (first == NULL)
{
first = temp;
}
else
{
ptr = first;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}
void swap()
{
int a, b;
struct node *ptr;
ptr = first;
while (ptr != NULL && ptr->next != NULL)
{
a = ptr->data;
b = ptr->next->data;
ptr->next->data = a;
ptr->data = b;
ptr = ptr->next->next;
}
}
void display()
{
printf("After exchanging data: \n");
struct node *ptr;
ptr = first;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
}
int main()
{
int b, n;
printf("Enter the number of nodes needed: \n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter data%d \n", i + 1);
scanf("%d", &b);
create_linkedlist(b);
}
swap();
display();
}