typedef void CircleList;
struct CircleListNode {
CircleListNode* next;
};
struct CircleListT {
CircleListNode header;
CircleListNode* Slider;
int length;
};
CircleList* createCircleList() {
CircleListT* list = new CircleListT;
if (list == NULL) {
return NULL;
}
list->length = 0;
list->header.next = NULL;
list->Slider = NULL;
return list;
}
void destoryCircleList(CircleList* list) {
CircleListT* listt = (CircleListT*)list;
if (listt == NULL) {
return;
}
delete listt;
listt = NULL;
}
void clearCircleList(CircleList* list) {
CircleListT* listt = (CircleListT*)list;
if (listt == NULL) {
return;
}
listt->length = 0;
listt->header.next = NULL;
listt->Slider = NULL;
}
int getCircleListLength(CircleList* list) {
CircleListT* listt = (CircleListT*)list;
int ret = -1;
if (listt == NULL) {
return ret;
}
ret = listt->length;
return ret;
}
CircleListNode* getNode(CircleList* list, int pos) {
CircleListT* listt = (CircleListT*)list;
if (listt == NULL || pos < 0) {
return NULL;
}
CircleListNode* current = (CircleListNode*)listt;
//cout << "getNode current : " << current << endl;
for (int i = 0; i < pos && (current->next != NULL); i++) {
current = current->next;
//cout << "current: " << current << endl;
}
CircleListNode* node = current->next;
//cout << "getNode node: " << node << " pos: " << pos << endl;
return node;
}
int insertNodePre(CircleList* list, CircleListNode* node) {
CircleListT* listt = (CircleListT*)list;
if (listt == NULL || node == NULL ) {
return -1;
}
CircleListNode* current = (CircleListNode*)listt;
node->next = current->next;
current->next = node;
listt->length++;
if (listt->length == 0) {
//node->next = node;
}
else {
CircleListNode* last = getNode(listt, listt->length - 1);
listt->Slider = node;
last->next = current->next;
}
return 0;
}
int insertNode(CircleList* list, CircleListNode* node, int pos) {
CircleListT* listt = (CircleListT*)list;
if (listt == NULL || node == NULL || pos < 0) {
return -1;
}
CircleListNode* current = (CircleListNode*)listt;
for (int i = 0; i < pos && (current->next != NULL); i++) {
current = current->next;
}
node->next = current->next;
current->next = node;
//如果是第一次插入元素
if (listt->length == 0) {
listt->Slider = node;
}
listt->length++;
//如果是头插法
if (current == (CircleListNode*)listt) {
CircleListNode* last = getNode(listt, listt->length - 1);
last->next = current->next;
}
return 0;
}
CircleListNode* deleteNode(CircleList* list, int pos) {
CircleListT* listt = (CircleListT*)list;
if (listt == NULL || pos < 0||pos>=listt->length) {
return NULL;
}
CircleListNode* current = (CircleListNode*)listt;
for (int i = 0; i < pos; i++) {
current = current->next;
}
CircleListNode* last = NULL;
//如果删除第一个元素
if (current == (CircleListNode*)listt) {
last= getNode(listt, listt->length - 1);
}
//删除元素
CircleListNode* node = current->next;
current->next = node->next;
listt->length--;
if (last != NULL) {
listt->header.next = node->next;
last->next = node->next;
}
//删除元素为游标所指
if (listt->Slider == node) {
listt->Slider = node->next;
}
//删除元素后长度为零
if (listt->length == 0) {
listt->header.next = NULL;
listt->Slider = NULL;
}
return node;
}
CircleListNode* deleteNode(CircleList* list, CircleListNode* node) {
CircleListT* listt = (CircleListT*)list;
if (listt == NULL || node==NULL) {
return NULL;
}
CircleListNode* current = (CircleListNode*)listt;
CircleListNode* deletenode = NULL;
int index = -1;
for (int i = 0; i < listt->length; i++) {
if (current->next == node) {
index = i;
deletenode = current->next;
break;
}
current = current->next;
}
if (index != -1) {
deleteNode(list, index);
}
return deletenode;
}
CircleListNode* resetList(CircleList* list) {
CircleListT* listt = (CircleListT*)list;
CircleListNode* node = NULL;
if (listt == NULL) {
return NULL;
}
listt->Slider = listt->header.next;
node = listt->Slider;
return node;
}
CircleListNode* currentNode(CircleList* list) {
CircleListT* listt = (CircleListT*)list;
CircleListNode* node = NULL;
if (listt == NULL) {
return NULL;
}
node = listt->Slider;
return node;
}
//返回当前位置 游标下移
CircleListNode* nextNode(CircleList* list) {
CircleListT* listt = (CircleListT*)list;
CircleListNode* node = NULL;
if (listt == NULL) {
return NULL;
}
node = listt->Slider;
listt->Slider = node->next;
return node;
}
struct NodeValue {
CircleListNode node;
int value;
};
int main() {
CircleList* list = createCircleList();
NodeValue v1, v2, v3, v4, v5, v6,v7,v8;
v1.value = 1;
v2.value = 2;
v3.value = 3;
v4.value = 4;
v5.value = 5;
v6.value = 6;
v7.value = 7;
v8.value = 8;
//尾插法
/*insertNode(list, (CircleListNode*)&v1, getCircleListLength(list));
insertNode(list, (CircleListNode*)&v2, getCircleListLength(list));
insertNode(list, (CircleListNode*)&v3, getCircleListLength(list));
insertNode(list, (CircleListNode*)&v4, getCircleListLength(list));
insertNode(list, (CircleListNode*)&v5, getCircleListLength(list));
insertNode(list, (CircleListNode*)&v6, getCircleListLength(list));
insertNode(list, (CircleListNode*)&v7, getCircleListLength(list));
insertNode(list, (CircleListNode*)&v8, getCircleListLength(list));*/
//头插法
insertNodePre(list, (CircleListNode*)&v1);
insertNodePre(list, (CircleListNode*)&v2);
insertNodePre(list, (CircleListNode*)&v3);
insertNodePre(list, (CircleListNode*)&v4);
insertNodePre(list, (CircleListNode*)&v5);
insertNodePre(list, (CircleListNode*)&v6);
for (int i = 0; i < getCircleListLength(list)*2; i++) {
NodeValue* v = (NodeValue*)nextNode(list);
cout << "value: " << v->value << endl;
}
//约瑟夫问题
/*while (getCircleListLength(list) > 0) {
NodeValue* v = NULL;
for (int i = 0; i < 3; i++) {
v = (NodeValue*)nextNode(list);
}
cout << "delete v: " << v->value << endl;
deleteNode(list, (CircleListNode*)v);
}*/
system("pause");
return 0;
}
- 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
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257