回答編集履歴
2
完全なソースに差し替え
test
CHANGED
@@ -35,8 +35,36 @@
|
|
35
35
|
}
|
36
36
|
```
|
37
37
|
|
38
|
-
さらに、標準の[std::sort](https://cpprefjp.github.io/reference/algorithm/sort.html)の真似をして比較演算子を外から渡すようにすれば、別のソート順にも対応できます。(
|
38
|
+
さらに、標準の[std::sort](https://cpprefjp.github.io/reference/algorithm/sort.html)の真似をして比較演算子を外から渡すようにすれば、別のソート順にも対応できます。(動作検証できるように、完全なソースを載せておきます。メモリ解放は省略しています)
|
39
39
|
```c++
|
40
|
+
#include <string>
|
41
|
+
#include <cstring>
|
42
|
+
#include <iostream>
|
43
|
+
using namespace std;
|
44
|
+
|
45
|
+
typedef struct CharacterStatus {
|
46
|
+
string strName;
|
47
|
+
int nCharaNum; // 登録順ソートのため
|
48
|
+
char chFurigana[256]; // 名前順ソートで利用する---
|
49
|
+
CharacterStatus* pPrev;
|
50
|
+
CharacterStatus* pNext;
|
51
|
+
} CharaNode;
|
52
|
+
|
53
|
+
typedef struct CharacterNodeList {
|
54
|
+
CharaNode* pHead; // 先頭のノードを指すポインタ
|
55
|
+
CharaNode* pTail; // 末尾のノードを指すポインタ
|
56
|
+
} CharaList;
|
57
|
+
|
58
|
+
// pPosの後ろにpNodeを追加(pPosがnullptrなら先頭に追加)
|
59
|
+
void add_after(CharaList* pList, CharaNode* pPos, CharaNode* pNode) {
|
60
|
+
CharaNode*& pNext = (pPos) ? pPos->pNext : pList->pHead;
|
61
|
+
CharaNode*& pPrev = (pNext) ? pNext->pPrev : pList->pTail;
|
62
|
+
pNode->pNext = pNext;
|
63
|
+
pNode->pPrev = pPrev;
|
64
|
+
pNext = pNode;
|
65
|
+
pPrev = pNode;
|
66
|
+
}
|
67
|
+
|
40
68
|
// 昇順になる位置にpNodeを挿入
|
41
69
|
template<class Compare>
|
42
70
|
void insert(CharaList* pList, CharaNode* pNode, Compare comp) {
|
@@ -76,4 +104,25 @@
|
|
76
104
|
void sortListOrderFurigana(CharaList* pList) {
|
77
105
|
sort(pList, [](CharaNode* a, CharaNode* b) { return strcmp(a->chFurigana, b->chFurigana) < 0; });
|
78
106
|
}
|
107
|
+
|
108
|
+
void print(CharaList* pList) {
|
109
|
+
for (CharaNode* pCur = pList->pHead; pCur; pCur = pCur->pNext)
|
110
|
+
cout << pCur->strName << " " << pCur->nCharaNum << " " << pCur->chFurigana << ", ";
|
111
|
+
cout << endl;
|
112
|
+
}
|
113
|
+
|
114
|
+
int main() {
|
115
|
+
CharaList* pList = new CharaList{};
|
116
|
+
add_after(pList, nullptr, new CharaNode{ "a", 2, "え" });
|
117
|
+
add_after(pList, nullptr, new CharaNode{ "g", 1, "あ" });
|
118
|
+
add_after(pList, nullptr, new CharaNode{ "e", 4, "い" });
|
119
|
+
add_after(pList, nullptr, new CharaNode{ "c", 3, "う" });
|
120
|
+
print(pList);
|
121
|
+
sortListOrderName(pList);
|
122
|
+
print(pList);
|
123
|
+
sortListOrderRegist(pList);
|
124
|
+
print(pList);
|
125
|
+
sortListOrderFurigana(pList);
|
126
|
+
print(pList);
|
127
|
+
}
|
79
128
|
```
|
1
ソートの共通化を望んでいるような気がしたので
test
CHANGED
@@ -34,3 +34,46 @@
|
|
34
34
|
}
|
35
35
|
}
|
36
36
|
```
|
37
|
+
|
38
|
+
さらに、標準の[std::sort](https://cpprefjp.github.io/reference/algorithm/sort.html)の真似をして比較演算子を外から渡すようにすれば、別のソート順にも対応できます。(`add_after`は上と同じ)
|
39
|
+
```c++
|
40
|
+
// 昇順になる位置にpNodeを挿入
|
41
|
+
template<class Compare>
|
42
|
+
void insert(CharaList* pList, CharaNode* pNode, Compare comp) {
|
43
|
+
// 昇順になる位置を後ろから探す
|
44
|
+
CharaNode* pPos = pList->pTail;
|
45
|
+
while (pPos && comp(pNode, pPos))
|
46
|
+
pPos = pPos->pPrev;
|
47
|
+
// 見つけた位置にpNodeを追加
|
48
|
+
add_after(pList, pPos, pNode);
|
49
|
+
}
|
50
|
+
|
51
|
+
// 昇順にソート
|
52
|
+
template<class Compare>
|
53
|
+
void sort(CharaList* pList, Compare comp) {
|
54
|
+
// 中身をpCurに退避してpListを空にする
|
55
|
+
CharaNode* pCur = pList->pHead;
|
56
|
+
pList->pHead = pList->pTail = nullptr;
|
57
|
+
// pCurを先頭から一つずつ挿入していく
|
58
|
+
while (pCur) {
|
59
|
+
CharaNode* pNext = pCur->pNext;
|
60
|
+
insert(pList, pCur, comp);
|
61
|
+
pCur = pNext;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
// 登録順に並び替え
|
66
|
+
void sortListOrderRegist(CharaList* pList) {
|
67
|
+
sort(pList, [](CharaNode* a, CharaNode* b) { return a->nCharaNum < b->nCharaNum; });
|
68
|
+
}
|
69
|
+
|
70
|
+
// 名前順に並び替え
|
71
|
+
void sortListOrderName(CharaList* pList) {
|
72
|
+
sort(pList, [](CharaNode* a, CharaNode* b) { return a->strName < b->strName; });
|
73
|
+
}
|
74
|
+
|
75
|
+
// ふりがな順に並び替え
|
76
|
+
void sortListOrderFurigana(CharaList* pList) {
|
77
|
+
sort(pList, [](CharaNode* a, CharaNode* b) { return strcmp(a->chFurigana, b->chFurigana) < 0; });
|
78
|
+
}
|
79
|
+
```
|