Skip to content

Commit d1ddc1b

Browse files
committed
Solution code for baekjoon/1181
1 parent aeda0e8 commit d1ddc1b

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

sorting/baekjoon/1181/solution.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
typedef struct node {
6+
char string[64];
7+
struct node* next;
8+
} node;
9+
10+
int my_strcmp(char* str1, char* str2) {
11+
const int length = (int)strlen(str1);
12+
for(int index = 0 ; index < length ; index++) {
13+
if(str1[index] < str2[index]) {
14+
return 1;
15+
} else if (str2[index] < str1[index]) {
16+
return -1;
17+
} else {
18+
19+
}
20+
}
21+
22+
return 0;
23+
}
24+
25+
void print_list(node* head) {
26+
node* current = head->next;
27+
while (1) {
28+
printf("%s\n", current->string);
29+
30+
if (current->next == NULL) break;
31+
else current = current->next;
32+
}
33+
}
34+
35+
void insert_node(node* head) {
36+
node* current = head;
37+
38+
node* temp = (node*)malloc((sizeof(node)));
39+
scanf("%s", temp->string);
40+
temp->next = NULL;
41+
42+
while(1) {
43+
if (current->next == NULL) {
44+
current->next = temp;
45+
46+
break;
47+
} else if (strlen(temp->string) == strlen(current->next->string)) {
48+
if (my_strcmp(temp->string, current->next->string) == 1) {
49+
temp->next = current->next;
50+
current->next = temp;
51+
52+
break;
53+
} else if (my_strcmp(temp->string, current->next->string) == -1) {
54+
// Take one more step
55+
current = current->next;
56+
} else {
57+
// Identical string
58+
break;
59+
}
60+
} else if (strlen(temp->string) < strlen(current->next->string)) {
61+
temp->next = current->next;
62+
current->next = temp;
63+
64+
break;
65+
} else {
66+
current = current->next;
67+
}
68+
}
69+
}
70+
71+
int main(void) {
72+
int n = 0;
73+
scanf("%d", &n);
74+
75+
node* head = (node*)calloc(1, sizeof(node));
76+
head->next = NULL;
77+
78+
while(n != 0) {
79+
insert_node(head);
80+
n = n - 1;
81+
}
82+
83+
print_list(head);
84+
85+
return 0;
86+
}

0 commit comments

Comments
 (0)