-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab5_5.c
110 lines (110 loc) · 2.68 KB
/
lab5_5.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//Write a C code to split the original single linked list into two sub-lists, where the first and second sub-list contains the even position nodes and odd position nodes of the original list respectively, and then join the second sub-list at the end of the first sub-list. Example: If the list contains 3->4->2->1->7->9->8, then the function needs to produce 3->2->7->8->4->1->9.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} * first, *head, *start;
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 sub()
{
struct node *ptr;
ptr = first;
int a = 1;
while (ptr != NULL)
{
if (a % 2 == 0)
{
struct node *temp, *ptr1;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = ptr->data;
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
ptr1 = head;
while (ptr1->next != NULL)
{
ptr1 = ptr1->next;
}
ptr1->next = temp;
}
}
else
{
struct node *temp1, *ptr2;
temp1 = (struct node *)malloc(sizeof(struct node));
temp1->data = ptr->data;
temp1->next = NULL;
if (start == NULL)
{
start = temp1;
}
else
{
ptr2 = start;
while (ptr2->next != NULL)
{
ptr2 = ptr2->next;
}
ptr2->next = temp1;
}
}
a++;
ptr = ptr->next;
}
ptr = start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = head;
}
void display()
{
printf("After joining the two sublists: \n");
struct node *ptr;
ptr = start;
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);
}
sub();
display();
}