static ListNode * detachAtIndex(List * list, int index) { ListNode * nodeToRemove, *previousNode, *nextNode; if(list == NULL) return NULL; if(index < 0 || index >= List_getLength(list)) return NULL; if(index == 0) return detachFront(list); if(index == (List_getLength(list) - 1)) return detachBack(list); nodeToRemove = nodeAtIndex(list, index); previousNode = nodeToRemove->previousNode; nextNode = nodeToRemove->nextNode; previousNode->nextNode = nextNode; nextNode->previousNode = previousNode; nodeToRemove->nextNode = NULL; nodeToRemove->previousNode = NULL; list->length--; return nodeToRemove; }
List * List_insertList(List * list, int position, List * insertList) { ListIterator * newIterator; ListIterator * insertIterator; void * data; List * newList; if(list == NULL || insertList == NULL) return NULL; if(position < 0 || position > List_getLength(list)) return NULL; if(position == 0) return List_prependList(list, insertList); if(position == List_getLength(list)) return List_appendList(list, insertList); newList = List_clone(list); newIterator = ListIterator_create(newList); insertIterator = ListIterator_create(insertList); if(ListIterator_seekToPosition(newIterator, position) == NULL) { ListIterator_destroy(newIterator); ListIterator_destroy(insertIterator); List_destroy(newList); return NULL; } data = ListIterator_seekToFirst(insertIterator); while(data != NULL) { if(!ListIterator_insertValue(newIterator, data)) { ListIterator_destroy(newIterator); ListIterator_destroy(insertIterator); List_destroy(newList); return NULL; } ListIterator_nextItem(newIterator); data = ListIterator_nextItem(insertIterator); } ListIterator_destroy(newIterator); ListIterator_destroy(insertIterator); return newList; }
void test(){ /* 测试链表函数 测试结果:全部函数运行通过 */ List A; int a[]={1,2,3,4}; List B; int b[]={5,6,7,8,9}; List_Init(&A); List_Init(&B); List_creat(&A,a,4); List_creat(&B,b,5); printf("\nA:"); List_printf(&A); printf("\nB:"); List_printf(&B); List_Insert(&A,1,2); printf("\nA:"); List_printf(&A); printf("\nA length:%d",List_getLength(&A)); List_clear(&B); printf("\nB:"); List_printf(&B); List_destroy(&B); }
List * List_removeRange(List * list, int start, int end) { List * newList; int length, i; void * data; ListIterator * iterator; if(list == NULL) return NULL; if(start < 0 || start >= List_getLength(list)) return NULL; if(end < 0 || end >= List_getLength(list)) return NULL; if(end < start) return NULL; length = end - start; newList = List_create(); iterator = ListIterator_create(list); if(ListIterator_seekToPosition(iterator, start) == NULL) { ListIterator_destroy(iterator); List_destroy(newList); return NULL; } for(i = 0; i <= length; i++) { data = ListIterator_removeCurrent(iterator); if(data == NULL) { ListIterator_destroy(iterator); List_destroy(newList); } List_addBack(newList, data); } ListIterator_destroy(iterator); return newList; }
int List_positionOfSubListOffset(List * list, List * sublist, int (*isEqual)(void *, void*), int offset) { int i; if(list == NULL || sublist == NULL || isEqual == NULL) return false; if(List_isEmpty(sublist)) return -1; if(List_getLength(sublist) > List_getLength(list)) return -1; if(offset < 0 || offset >= List_getLength(sublist)) return -1; if(List_getLength(list) == List_getLength(sublist) && offset == 0) { if(List_isEqual(list, sublist, isEqual)) return 0; else return -1; } for(i = offset; i < List_getLength(list) - List_getLength(sublist) + 1; i++) { if(locationContainsSubList(list, sublist, isEqual, i)) return i; } return -1; }
Boolean List_containsSubList(List * list, List * sublist, int (*isEqual)(void *, void*)) { int i; if(list == NULL || sublist == NULL || isEqual == NULL) return false; if(List_isEmpty(sublist)) return true; if(List_getLength(sublist) > List_getLength(list)) return false; if(List_getLength(list) == List_getLength(sublist)) return List_isEqual(list, sublist, isEqual); for(i = 0; i < List_getLength(list) - List_getLength(sublist) + 1; i++) { if(locationContainsSubList(list, sublist, isEqual, i)) return true; } return false; }
Boolean List_insertAtPosition(List * list, void * data, int index) { ListNode * nextNode, *previousNode, *newNode; if(list == NULL || data == NULL) return false; if(index < 0 || index > List_getLength(list)) return false; newNode = createNode(data); if(newNode == NULL) return false; if(index == 0) { return List_addFront(list, newNode); } if(index == List_getLength(list)) { return List_addBack(list, newNode); } nextNode = nodeAtIndex(list, index); previousNode = nextNode->previousNode; previousNode->nextNode = newNode; newNode->nextNode = nextNode; nextNode->previousNode = newNode; newNode->previousNode = previousNode; list->length++; return true; }
Boolean List_isEqual(List * list, List * secondList, int (*isEqual)(void *, void *)) { ListIterator * first; ListIterator * second; void * firstData; void * secondData; if(list == NULL || secondList == NULL || isEqual == NULL) return false; if(List_getLength(list) != List_getLength(secondList)) return false; first = ListIterator_create(list); second = ListIterator_create(secondList); firstData = ListIterator_seekToFirst(first); secondData = ListIterator_seekToFirst(second); while(firstData != NULL && secondData != NULL) { if(!isEqual(firstData, secondData)) { ListIterator_destroy(first); ListIterator_destroy(second); return false; } firstData = ListIterator_nextItem(first); secondData = ListIterator_nextItem(second); } ListIterator_destroy(first); ListIterator_destroy(second); return firstData == secondData; }
void * List_removePosition(List * list, int index) { ListNode * nodeToRemove; void * data; if(list == NULL) return NULL; if(index < 0 || index >= List_getLength(list)) return NULL; nodeToRemove = detachAtIndex(list, index); if(nodeToRemove == NULL) return NULL; data = nodeToRemove->data; destroyNode(nodeToRemove); return data; }
Boolean List_isEmpty(List * list) { return List_getLength(list) == 0; }
bool Stack_getLength(Stack s, size_t *lgt){ return List_getLength((List)s, lgt); }