insert a node at the tail of linked list in Appium


1
#include ↔​
10

11
char* readline();
12

13
typedef struct SinglyLinkedListNode SinglyLinkedListNode;
14
typedef struct SinglyLinkedList SinglyLinkedList;
15

16
struct SinglyLinkedListNode {
17
int data;
18
SinglyLinkedListNode* next;
19
};
20

21
struct SinglyLinkedList {
22
SinglyLinkedListNode* head;
23
};
24

25
SinglyLinkedListNode* create_singly_linked_list_node(int node_data) {
26
SinglyLinkedListNode* node = malloc(sizeof(SinglyLinkedListNode));
27

28
node->data = node_data;
29
node->next = NULL;
30

31
return node;
32
}
33

34
void print_singly_linked_list(SinglyLinkedListNode* node, char* sep, FILE* fptr) {
35
while (node) {
36
fprintf(fptr, "%d", node->data);
37

38
node = node->next;
39

40
if (node) {
41
fprintf(fptr, "%s", sep);
42
}
43
}
44
}
45

46
void free_singly_linked_list(SinglyLinkedListNode* node) {
47
while (node) {
48
SinglyLinkedListNode* temp = node;
49
node = node->next;
50

51
free(temp);
52
}
53
}
54
// Complete the insertNodeAtTail function below.
55

56
/*
57
* For your reference:
58
*
59
* SinglyLinkedListNode {
60
* int data;
61
* SinglyLinkedListNode* next;
62
* };
63
*
64
*/
65
SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {
66

67

68
}
69
int main()
70
{
71
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
72

73
SinglyLinkedList* llist = malloc(sizeof(SinglyLinkedList));
74
llist->head = NULL;
75

76
char* llist_count_endptr;
77
char* llist_count_str = readline();
78
int llist_count = strtol(llist_count_str, &llist_count_endptr, 10);
79

80
if (llist_count_endptr == llist_count_str || *llist_count_endptr != '\0') { exit(EXIT_FAILURE); }
81

82
for (int i = 0; i < llist_count; i++) {
83
char* llist_item_endptr;
84
char* llist_item_str = readline();
85
int llist_item = strtol(llist_item_str, &llist_item_endptr, 10);
86

87
if (llist_item_endptr == llist_item_str || *llist_item_endptr != '\0') { exit(EXIT_FAILURE); }
88

89
SinglyLinkedListNode* llist_head = insertNodeAtTail(llist->head, llist_item);
90
llist->head = llist_head;
91
}
92

93

94

95
char *sep = "\n";
96

97
print_singly_linked_list(llist->head, sep, fptr);
98
fprintf(fptr, "\n");
99

100
free_singly_linked_list(llist->head);
101

102
fclose(fptr);
103

104
return 0;
105
}
106

107
char* readline() {
108
size_t alloc_length = 1024;
109
size_t data_length = 0;
110
char* data = malloc(alloc_length);
111

112
while (true) {
113
char* cursor = data + data_length;
114
char* line = fgets(cursor, alloc_length - data_length, stdin);
115

116
if (!line) {
117
break;
118
}
119

120
data_length += strlen(cursor);
121

122
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
123
break;
124
}
125

126
alloc_length <<= 1;
127

128
data = realloc(data, alloc_length);
129

130
if (!line) {
131
break;
132
}
133
}
134

135
if (data[data_length - 1] == '\n') {
136
data[data_length - 1] = '\0';
137

138
data = realloc(data, data_length);
139
} else {
140
data = realloc(data, data_length + 1);
141

142
data[data_length] = '\0';
143
}
144

145
return data;
146
}
147