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;
}
循环单链表的头插及尾插实现
最新推荐文章于 2023-10-20 18:30:53 发布