void List_move(void *fromList, void *toList, void *fromListFromNode, void *fromListToNode, void *toListNextNode ) { Node *node; Node *nextNode; assert(fromList != NULL); assert(toList != NULL); if (fromListFromNode == LIST_START) fromListFromNode = ((List*)fromList)->head; node = (Node*)fromListFromNode; while (node != fromListToNode) { nextNode = node->next; List_remove(fromList,node); List_insert(toList,node,toListNextNode); node = nextNode; } if (node != NULL) { List_remove(fromList,node); List_insert(toList,node,toListNextNode); } }
void List_insertSorted(List list, void* ptr, List_compare ptrList_compare){ Node node, current; int beforeTail = 1; int isHead = 1; assert(list); node = Node_new(); if(node == NULL) return; node->ptr = ptr; if(list->current == NULL) { list->current = node; list->head = node; list->tail = node; return; } List_head(list); if(list->current->ptr == ptr) List_remove(list); while ((*ptrList_compare)(list->current->ptr, ptr) && beforeTail) { isHead = 0; beforeTail = List_next(list); if(list->current->ptr == ptr) List_remove(list); } current = list->current; list->size++; if(beforeTail){ node->next = current; if(isHead) list->head = node; else{ node->prev = current->prev; current->prev->next = node; } current->prev = node; } else{ current->next = node; node->prev = current; list->tail = node; } }
int main() { List *list = NULL; char c; int i; while ((c = getchar()) != 'q') switch (c) { case 'a': { scanf("%i", &i); if (List_add(&list, i) == 1) printf("Error\n"); break; } case 'r': { scanf("%i", &i); if (List_remove(&list, i) == 1) printf("Not found =(\n"); break; } case 'p': { List_print(&list); break; } } List_delList(&list); return 0; }
/* Remove env variable and value identified by name */ static inline void removeEnv(T C, const char *name) { char *e = findEnv(C, name); if (e) { List_remove(C->env, e); FREE(e); } }
/*! * @brief Remove and delete entry from the Translation Table * * @param da Device address * * @sa _SysLinkMemUtils_insertMapElement */ static Ptr _SysLinkMemUtils_removeMapElement (Ptr da) { AddrNode * node; Ptr addr = NULL; GT_1trace (curTrace, GT_ENTER, "_SysLinkMemUtils_removeMapElement", da); OsalSemaphore_pend (SysLinkMemUtils_module->semList, OSALSEMAPHORE_WAIT_FOREVER); node = _SysLinkMemUtils_findNode (da); if (!node || node->da != da) { #if !defined(SYSLINK_BUILD_OPTIMIZE) GT_setFailureReason (curTrace, GT_4CLASS, (Char *)__func__, PROCMGR_E_FAIL, "Entry not found!"); #endif /* if !defined(SYSLINK_BUILD_OPTIMIZE) */ } else { addr = node->ua; List_remove (SysLinkMemUtils_module->addrTable, &node->elem); Memory_free (NULL, node, sizeof (AddrNode)); } OsalSemaphore_post (SysLinkMemUtils_module->semList); GT_1trace (curTrace, GT_LEAVE, "_SysLinkMemUtils_removeMapElement", addr); return addr; }
void addBookReader(library_t * self,const char * name,const char * readerName){ for(int i = 0;i<List_getSize(self->book);i++){ book_t * tmpBook = List_get(self->book,i,NULL); if(strcmp(tmpBook->name,name)==0 && tmpBook->status == 0){ user_t * tmpUser = newUser(readerName); //List_add(tmpBook->users,List_getSize(tmpBook->users)-1,tmpUser); List_t tmpList = tmpBook->users; List_add(tmpList,List_getSize(tmpList),tmpUser); //user_t * tmpUser1 = List_get(tmpList,List_getSize(tmpList)-1,NULL); //puts(tmpUser1->name); book_t * nB = newBookchangeParam(name,1,tmpBook->curDaysUsed,"day",tmpList); List_remove(self->book,i,NULL); List_add(self->book,i,nB); return; } } }
/** Removes "." and ".." from a path */ char *getCanonicalPath(const char *path){ char *prefix = NULL; char *rest = NULL; char *tmp = NULL; size_t offset = 0; char **src = NULL; List *dst = NULL; size_t ii = 0; Buffer *buf = NULL; char *result = NULL; if (path != NULL && strlen(path) > 0) { tmp = strxreplace(astrcpy(path), '\\', '/'); if (isDosPath(tmp) || isUncPath(tmp)) { if (isDosPath(tmp)) { prefix = getPathPart(tmp, PATH_DRIVE); offset = 0; } else if (isUncPath(tmp)) { prefix = getPathPart(tmp, PATH_HOST); offset = 2; } rest = astrcpy(strchr(&tmp[offset], '/')); } else { rest = astrcpy(tmp); } src = astrtok(rest, "/"); dst = new_List(); while (src[ii] != NULL) { if (strxequals(src[ii], "..")) { List_remove(dst, -1, false); } else if (!strxequals(src[ii], ".")) { List_append(dst, src[ii]); } ii++; } buf = new_Buffer(0); if (prefix != NULL) { Buffer_appendString(buf, prefix); } for (ii = 0; ii < List_length(dst); ++ii) { Buffer_appendString(buf, List_get(dst, ii)); if (ii != (List_length(dst) - 1)) { Buffer_appendChar(buf, '/'); } } result = astrcpy(buf->data); delete_Buffer(buf); delete_List(dst, false); astrtokfree(src); mu_free(prefix); mu_free(rest); mu_free(tmp); } return result; }
void remove_mid(List *list) { List_push(list, 12); List_push(list, 7); List_push(list, 8); List_remove(list, List_index(list, 1)); assert(List_size(list) == 2); assert(List_first(list)->data == 12); assert(List_last(list)->data == 8); }
void FileFragmentList_removeFile(FileFragmentList *fileFragmentList, FileFragmentNode *fileFragmentNode) { assert(fileFragmentList != NULL); assert(fileFragmentNode != NULL); List_remove(fileFragmentList,fileFragmentNode); freeFileFragmentNode(fileFragmentNode,NULL); LIST_DELETE_NODE(fileFragmentNode); }
void FragmentList_discard(FragmentList *fragmentList, FragmentNode *fragmentNode) { assert(fragmentList != NULL); assert(fragmentNode != NULL); List_remove(fragmentList,fragmentNode); freeFragmentNode(fragmentNode,NULL); LIST_DELETE_NODE(fragmentNode); }
void List_empty(List list) { assert(list); List_head(list); while(!List_isEmpty(list)) List_remove(list); }
void Post_unsubscribeWantedCarTransist(Post_t self, void * receiver, PostEvent_CB cb){ int lSize = List_getSize(self->wantedCarEvents); for(int i = 0; i < lSize; i++){ Event_t ev = List_get(self->wantedCarEvents, i, NULL); if(ev->receiver == receiver && ev->cb == cb){ List_remove(self->wantedCarEvents, i, NULL); } } }
void deleteBook(library_t * self,const char * name){ for(int i = 0;i<List_getSize(self->book);i++){ book_t * tmpBook = List_get(self->book,i,NULL); if(strcmp(tmpBook->name,name)==0){ List_remove(self->book,i,NULL); return; } } }
void *List_shift(List *list) { List_check(list); check(list, "Can't shift from a NULL list"); ListNode *node = list->first; return node != NULL ? List_remove(list, node) : NULL; error: return NULL; }
char *test_remove() { char *val = List_remove(list, list->first->next); mu_assert(val == test2, "Wrong removed element."); mu_assert(List_count(list) == 2, "Wrong count after remove."); mu_assert(List_first(list) == test3, "Wrong first after remove."); mu_assert(List_last(list) == test1, "Wrong last after remove."); return NULL; }
char *test_remove() { //only test middle case since push shift test other cases char *val = List_remove(list, list->first->next); mu_assert(val == test2, "Wrong removed element."); mu_assert(List_count(list) == 2, "Wrong count after remove."); mu_assert(List_first(list) == test3, "Wrong first after remove."); mu_assert(List_last(list) == test1, "Wrong last after remove."); return NULL; }
static ListNode* getNode(void* data) { ListNode* node = nodeCache.firstNode; if(node != NULL) { List_remove(node); node->data = data; } else { node = List_newNode(data); } return node; }
void *List_pop(List *list) { List_check(list); check(list, "Can't pop from a NULL list"); ListNode *node = list->last; return node != NULL ? List_remove(list, node) : NULL; error: return NULL; }
/** *** \brief Destroys the specified Dict, freeing the memory associated with it *** \param d The Dict to destroy *** \param freeData True if the storage for the items in the Dict is to be freed *** also **/ void delete_Dict(Dict *d, bool freeData) { List *l = (List *)d; Pair *p = NULL; while (List_length(l) > 0) { p = (Pair *)List_get(l, 0); mu_free(p->key); if (freeData) mu_free(p->value); List_remove(l, 0, true); } }
Node *List_getLast(void *list) { Node *node; assert(list != NULL); node = ((List*)list)->tail; if (node != NULL) List_remove(list,node); return node; }
char *test_remove() { /*only need to test middle remove case since push/shift already tests the other cases*/ char *val = List_remove(list, list->first->next); mu_assert(val == test2, "Wrong removed element."); mu_assert(List_count(list) == 2, "Wrong count after remove."); mu_assert(List_first(list) == test3, "Wrong first after remove."); mu_assert(List_last(list) == test1, "Wrong last after remove."); return NULL; }
void freePath(List path) { List_head(path); while(!List_isEmpty(path)) { Position_delete(List_getCurrent(path)); List_remove(path); } List_delete(path); }
char *test_remove() { List_push(list, tv1); List_push(list, tv2); List_push(list, tv3); char *out = List_remove(list, list->first->next); mu_assert(out == tv2, "Removed wrong thing."); mu_assert(List_first(list) == tv1, "Wrong first ref."); mu_assert(List_last(list) == tv3, "Wrong last ref."); return NULL; }
void* Queue_dequeue(Queue queue) { void* ptr; assert(queue); List_head(queue); ptr = List_getCurrent(queue); List_remove(queue); return ptr; }
void List_insertLast(List* list, ListNode* newNode) { ErrorIf(list == NULL, VEXOS_ARGNULL); ErrorIf(newNode == NULL, VEXOS_ARGNULL); if(newNode->list != NULL) { List_remove(newNode); } if(list->lastNode == NULL) { List_insertFirst(list, newNode); } else { List_insertAfter(list->lastNode, newNode); } }
bool Dict_remove(Dict *d, const char *key, bool freeData) { bool retVal = false; size_t idx = 0; Pair *p = NULL; if (keyToIndex(d, key, &idx)) { p = (Pair *)List_get((List *)d, idx); if(freeData) mu_free(p->value); mu_free(p->key); retVal = List_remove((List *)d, idx, true); } return retVal; }
void Scheduler_removeButtonSchedulers(Button* button) { if(button == NULL) return; ListNode* node = buttonList.firstNode; while(node != NULL) { ButtonScheduler* sched = node->data; if(Button_getSchedulerButton(sched) == button) { node = List_remove(node); free(sched); continue; } node = node->next; } }
void List_delete(List list) { assert(list); List_head(list); if(!List_isEmpty(list)) LOGWARNING("Deleting a non-empty list"); while(!List_isEmpty(list)) List_remove(list); free(list); }
END_TEST START_TEST (test_List_remove_1) { List_add(L, "foo"); fail_unless( !strcmp(List_remove(L, 0), "foo") ); fail_unless(List_size(L) == 0); /* fail_unless(L->head == NULL); fail_unless(L->tail == NULL); */ }
void List_insertFirst(List* list, ListNode* newNode) { ErrorIf(list == NULL, VEXOS_ARGNULL); ErrorIf(newNode == NULL, VEXOS_ARGNULL); if(newNode->list != NULL) { List_remove(newNode); } if(list->firstNode == NULL) { list->firstNode = newNode; list->lastNode = newNode; newNode->prev = NULL; newNode->next = NULL; newNode->list = list; list->nodeCount++; } else { List_insertBefore(list->firstNode, newNode); } }