-
Notifications
You must be signed in to change notification settings - Fork 2
/
input.c
92 lines (73 loc) · 2.02 KB
/
input.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
#include "input.h"
uint32_t getLineNum(char* line) {
uint32_t i;
uint32_t num = 0;
for(i = 0; line[i] >= '0' && line[i] <= '9' && i <= MAX_NUM_DIGITS; ++i)
num = num * 10 + (line[i] - '0');
return num;
}
strLine_t* mkStrLine(char* line) {
strLine_t* strLine = Malloc(sizeof(strLine_t));
strLine->prev = NULL;
strLine->next = NULL;
strLine->line = line;
strLine->num = getLineNum(line);
return strLine;
}
void addStrLine(strLines_t* lines, strLine_t* new) {
if(new == NULL)
return;
if(lines->head == NULL) {
lines->head = new;
lines->tail = new;
return;
}
if(lines->head == lines->tail) {
if(lines->head->num < new->num) {
lines->head->next = new;
new->prev = lines->head;
lines->tail = new;
}else if(lines->head->num > new->num) {
lines->head->prev = new;
new->next = lines->head;
lines->head = new;
}
return;
}
if(lines->head->num == new->num) {
lines->head->line = new->line;
return;
}else if(lines->tail->num == new->num) {
lines->tail->line = new->line;
return;
}
if(lines->tail->num < new->num) {
lines->tail->next = new;
new->prev = lines->tail;
lines->tail = new;
return;
}
// curLine will be bigger ->prev will be <
strLine_t* curLine = lines->head;
strLine_t* prev;
strLine_t* next;
while(curLine != NULL && curLine->num <= new->num) {
if(curLine->num == new->num) {
prev = curLine->prev;
next = curLine->next;
prev->next = new;
new->prev = prev;
next->prev = new;
new->next = next;
return;
}
if(curLine->next == NULL)
break;
curLine = curLine->next;
}
prev = curLine->prev;
prev->next = new;
new->prev = prev;
curLine->prev = new;
new->next = curLine;
}