struct Lines; /* typedefのための前方宣言 */
typedef struct Lines Lines;
typedef Lines * LinesPtr;
struct Lines { char *word; LinesPtr next, prev; };
LinePtr linesGetLast(LinePtr p) { if(p){ while(p && p->next) p = p->next; } return p; }
LinePtr linesGetFirst(LinePtr p) { if(p){ while(p && p->prev) p = p->prev; } return p; }
LinesPtr add_line(int num, LinesPtr current)
{
char input_line[512];
LinesPtr temp_lines = NULL;
while(fgets(input_line, sizeof(input_line), stdin) != NULL){
if(input_line[0] == '.' || input_line[0] == '\0') break;
temp_lines = malloc(sizeof(Lines)); /* エラーチェック省略 */
temp_lines->prev = NULL; temp_lines->next = NULL;
temp_lines->word = malloc(sizeof(char) * strlen(input_line) + 1);
strcpy(temp_lines->word, input_line);
if(current != NULL){
temp_lines->next = current->next; current->next = temp_lines;
temp_lines->prev = current;
if(temp_lines->next != NULL) temp_lines->next->prev = temp_lines;
}
current = temp_lines;
}
return temp_lines; /* 割り込み終了位置を返す */
}