-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem27.c
More file actions
118 lines (100 loc) Β· 2.69 KB
/
problem27.c
File metadata and controls
118 lines (100 loc) Β· 2.69 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
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
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Structure to represent each child in the parade
typedef struct ListNode {
int badgeNumber;
struct ListNode *next;
} ListNode;
// Create a new node for a child with the given badge number
ListNode* createChild(int badgeNumber) {
ListNode* newChild = (ListNode*)malloc(sizeof(ListNode));
newChild->badgeNumber = badgeNumber;
newChild->next = NULL;
return newChild;
}
// Add a child to the end of the linked list
void addChild(ListNode** head, ListNode** tail, int badgeNumber) {
ListNode* newChild = createChild(badgeNumber);
if (*head == NULL) {
*head = *tail = newChild;
} else {
(*tail)->next = newChild;
*tail = newChild;
}
}
// Find the leader (middle child or second middle if even count)
ListNode* findParadeLeader(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
// Print the parade lineup from the leader onward
void printLineFromLeader(ListNode* leader) {
printf("Required children: ");
while (leader) {
printf("%d", leader->badgeNumber);
if (leader->next) {
printf(" -> ");
}
leader = leader->next;
}
printf("\n");
}
// Free memory
void cleanUp(ListNode* head) {
while (head) {
ListNode* temp = head;
head = head->next;
free(temp);
}
}
// Main driver function
int main() {
char inputLine[1024];
printf("Enter the students badge no: ");
if (!fgets(inputLine, sizeof(inputLine), stdin)) {
printf("Failed to read input.\n");
return 1;
}
ListNode *head = NULL, *tail = NULL;
char* token = strtok(inputLine, " \n");
int badgeCount = 0;
while (token != NULL) {
// Check if token is a valid number
int valid = 1;
for (int i = 0; token[i]; i++) {
if (!isdigit(token[i])) {
valid = 0;
break;
}
}
if (!valid) {
printf("!! Invalid Input !!\n");
cleanUp(head);
return 1;
}
int badgeNumber = atoi(token);
if (badgeNumber <= 0) {
printf("!! Invalid Input !!\n");
cleanUp(head);
return 1;
}
addChild(&head, &tail, badgeNumber);
badgeCount++;
token = strtok(NULL, " \n");
}
if (badgeCount == 0) {
printf("!! Invalid Input !!\n");
return 1;
}
ListNode* leader = findParadeLeader(head);
printLineFromLeader(leader);
cleanUp(head);
return 0;
}