回答編集履歴
1
コード追加
answer
CHANGED
@@ -1,3 +1,125 @@
|
|
1
1
|
可能であれば、二次元配列、単方向リスト、ついでに双方向リストでも作ってみて、各々どういった利点・欠点があるのかを実感として比較することをお勧めします。
|
2
2
|
特別厳しい条件があるわけでも無いでしょうし、きちんと動かすことさえ出来ればどれでも構わないレベルだと思います。
|
3
|
-
むしろ、機能の追加によってもコード全体の見通しを良く(読み易く分かり易く)出来るかの方が難しいと思います。
|
3
|
+
むしろ、機能の追加によってもコード全体の見通しを良く(読み易く分かり易く)出来るかの方が難しいと思います。
|
4
|
+
|
5
|
+
---
|
6
|
+
episteme さんの回答のコードからファイル分割・単方向リストにしてみました。
|
7
|
+
commandline は簡単にする為固定サイズです。
|
8
|
+
```c
|
9
|
+
#include <stdio.h>
|
10
|
+
#include "history.h"
|
11
|
+
|
12
|
+
int main() {
|
13
|
+
HISTORY* hist = history_initialize(4); // 最新4つ分を保持する
|
14
|
+
|
15
|
+
const char* input[6] = { "alpha", "brabo", "charlie", "delta", "echo", "fox" };
|
16
|
+
for (int i = 0; i < 6; ++i) {
|
17
|
+
history_push(hist, input[i]);
|
18
|
+
for (int h = history_size(hist); h > 0; --h) {
|
19
|
+
printf("[%s] ", history_at(hist, h-1));
|
20
|
+
}
|
21
|
+
printf("\n");
|
22
|
+
}
|
23
|
+
history_terminate(hist);
|
24
|
+
return 0;
|
25
|
+
}
|
26
|
+
```
|
27
|
+
history.h
|
28
|
+
```c
|
29
|
+
#ifndef HISTORY_H_
|
30
|
+
#define HISTORY_H_
|
31
|
+
|
32
|
+
typedef struct link {
|
33
|
+
struct link *next;
|
34
|
+
char commandline[100];
|
35
|
+
} HIST_LINK;
|
36
|
+
|
37
|
+
typedef struct history {
|
38
|
+
HIST_LINK *list;
|
39
|
+
int capacity;
|
40
|
+
int size;
|
41
|
+
} HISTORY;
|
42
|
+
|
43
|
+
HISTORY* history_initialize(int cap);
|
44
|
+
void history_terminate(HISTORY* hist);
|
45
|
+
|
46
|
+
int history_size(const HISTORY* hist);
|
47
|
+
int history_capacity(const HISTORY* hist);
|
48
|
+
const char* history_at(const HISTORY* hist, int n);
|
49
|
+
void history_pop_back(HISTORY* hist);
|
50
|
+
void history_push(HISTORY* hist, const char* str);
|
51
|
+
|
52
|
+
#endif /* HISTORY_H_ */
|
53
|
+
```
|
54
|
+
history.c
|
55
|
+
```c
|
56
|
+
#include <stdio.h>
|
57
|
+
#include <stdlib.h>
|
58
|
+
#include <string.h>
|
59
|
+
|
60
|
+
#include "history.h"
|
61
|
+
|
62
|
+
/* history 実装 */
|
63
|
+
|
64
|
+
HISTORY* history_initialize(int cap) {
|
65
|
+
HISTORY* hist = (HISTORY*)malloc(sizeof(HISTORY));
|
66
|
+
if(hist != NULL) {
|
67
|
+
hist->list = NULL;
|
68
|
+
hist->capacity = cap;
|
69
|
+
}
|
70
|
+
return hist;
|
71
|
+
}
|
72
|
+
|
73
|
+
int history_size(const HISTORY* hist) {
|
74
|
+
int size = 0;
|
75
|
+
for(HIST_LINK *link=hist->list; link!=NULL; link=link->next, size++);
|
76
|
+
return size;
|
77
|
+
}
|
78
|
+
|
79
|
+
int history_capacity(const HISTORY* hist) {
|
80
|
+
return hist->capacity;
|
81
|
+
}
|
82
|
+
|
83
|
+
const char* history_at(const HISTORY* hist, int n) {
|
84
|
+
if(n < 0) return NULL;
|
85
|
+
HIST_LINK *link = hist->list;
|
86
|
+
for(int i=0; i<n && link!=NULL; i++, link=link->next);
|
87
|
+
return link!=NULL ? link->commandline : NULL;
|
88
|
+
}
|
89
|
+
|
90
|
+
void history_pop_back(HISTORY* hist) {
|
91
|
+
if(hist->list == NULL) return;
|
92
|
+
HIST_LINK **p = &hist->list;
|
93
|
+
for(; (*p)->next!=NULL; p=&(*p)->next);
|
94
|
+
free(*p);
|
95
|
+
*p = NULL;
|
96
|
+
}
|
97
|
+
|
98
|
+
void history_push(HISTORY* hist, const char* str) {
|
99
|
+
HIST_LINK *newelement = (HIST_LINK *)malloc(sizeof(HIST_LINK));
|
100
|
+
newelement->next = NULL;
|
101
|
+
strcpy(newelement->commandline, str);
|
102
|
+
|
103
|
+
while(history_size(hist) >= hist->capacity) history_pop_back(hist);
|
104
|
+
|
105
|
+
newelement->next = hist->list;
|
106
|
+
hist->list = newelement;
|
107
|
+
}
|
108
|
+
|
109
|
+
void history_terminate(HISTORY* hist) {
|
110
|
+
for(HIST_LINK *link=hist->list, *p; (p=link)!=NULL; ) {
|
111
|
+
link = link->next;
|
112
|
+
free(p);
|
113
|
+
}
|
114
|
+
free(hist);
|
115
|
+
}
|
116
|
+
```
|
117
|
+
実行結果:
|
118
|
+
```plain text
|
119
|
+
[alpha]
|
120
|
+
[alpha] [brabo]
|
121
|
+
[alpha] [brabo] [charlie]
|
122
|
+
[alpha] [brabo] [charlie] [delta]
|
123
|
+
[brabo] [charlie] [delta] [echo]
|
124
|
+
[charlie] [delta] [echo] [fox]
|
125
|
+
```
|