int delete_from_list(int val) { struct test_struct *prev = NULL; struct test_struct *del = NULL; printf("\n Deleting value [%d] from list\n",val); del = search_in_list(val,&prev); if(del == NULL) { return -1; } else { if(prev != NULL) prev->next = del->next; if(del == curr) { curr = prev; } else if(del == head) { head = del->next; } } free(del); del = NULL; return 0; }
int main(void) { int i = 0, ret = 0; struct node *ptr = NULL; print_list(); for(i = 0; i<100000000; i++) add_to_list(i); //print_list(); ptr = search_in_list(99999999, NULL); if(NULL == ptr) { printf("Search [val = %d] failed, no such element found\n",i); } else { printf("Search passed [val = %d]\n",ptr->val); } printf("Lets reverse the linked list\n"); reverseList(); return 0; }
// Desc : 연결 리스트에 새로운 노드를 현재 연결 리스트의 가장 뒤에 추가 // Param : head - 연결 리스트의 헤드 포인터 // new_item - 추가될 노드의 데이터 // Return : 리스트의 헤드 포인터 ListNode *add_to_list(ListNode *head, ListNode *new_item) { ListNode *iter; new_item->next = NULL; new_item->prev = NULL; // TODO : 여기에 코드를 작성하시오 if(head == NULL) { head = new_item; } else { iter = head; if(new_item->bank->name != NULL && search_in_list(head, new_item->bank->name) != NULL) { printf(":: Already Exists!. NOT Updated!\n"); } else { while(iter->next != NULL) { iter = iter->next; } iter->next = new_item; new_item->prev = iter; } } return head; }
int delete_from_list(int idx, struct node *head, struct node *curr) { struct node *prev = NULL; struct node *del = NULL; del = search_in_list(idx,&prev, head, curr); if(del == NULL) { return -1; } else { if(prev != NULL) prev->next = del->next; if(del == curr) { curr = prev; } else if(del == head) { head = del->next; } } free(del); del = NULL; return 0; }
int delete_from_list(char *bssid) { struct ap_struct *prev = NULL; struct ap_struct *del = NULL; #ifdef _DEBUG_APLIST printf("\n Deleting value [%s] from list\n", bssid); #endif del = search_in_list(bssid, &prev); if (del == NULL) { return -1; } else { if (prev != NULL) prev->next = del->next; if (del == curr) { curr = prev; } else if (del == head) { head = del->next; } } free(del); del = NULL; return 0; }
static GList *fields_compare_by_status(GList *l1, GList *l2) { GList *diff=NULL, *l; struct db_field *f; for (l = g_list_first(l1); l; l = l->next) { f = (struct db_field *)l->data; if (search_in_list(l2, f, SEARCH_STATUS_DIFF)) diff = g_list_append(diff, f); } return diff; }
static GList *fields_compare(GList *l1, GList *l2) { GList *diff=NULL, *l; struct db_field *f; for (l = g_list_first(l1); l; l = l->next) { f = (struct db_field *)l->data; if (!search_in_list(l2, f, SEARCH_ONLY_NAME)) diff = g_list_append(diff, f); } return diff; }
// Desc : 키보드로부터 새로운 메뉴에 대한 정보를 입력 받아 연결 리스트에 추가 // Param : head - 리스트의 헤드 포인터 // Return : 리스트의 헤드 포인터 ListNode *add_item(ListNode *head) { char name[MAX_NAME_SIZE]; int amount; ListNode *new_item, *item_in_list; // TODO : 여기에 코드를 작성하시오 // 필요시, 다음 코드 조각을 사용하시오. memset(name, 0x00, MAX_NAME_SIZE); printf("> Name : "); scanf("%s", name); fflush(stdin); printf("> Amount : "); scanf("%d", &amount); fflush(stdin); new_item = (ListNode*) malloc(sizeof(ListNode)); new_item->bank->amount = amount; new_item->bank->name = (char*) malloc(strlen(name)+1); strcpy(new_item->bank->name, name); if(head == NULL) { head = new_item; } else { item_in_list = head; if(search_in_list(head, name) != NULL) { printf(":: Already Exists!. NOT Updated!\n"); } else { while(item_in_list->next != NULL) { item_in_list = item_in_list->next; } item_in_list->next = new_item; new_item->prev = item_in_list; } } printf(":: Updated!\n"); // printf(":: Added!\n"); return head; }
int main(void) { int i = 0, ret = 0; struct test_struct *ptr = NULL; print_list(); for(i = 5; i<10; i++) add_to_list(i,true); print_list(); for(i = 4; i>0; i--) add_to_list(i,false); print_list(); for(i = 1; i<10; i += 4) { ptr = search_in_list(i, NULL); if(NULL == ptr) { printf("\n Search [val = %d] failed, no such element found\n",i); } else { printf("\n Search passed [val = %d]\n",ptr->val); } print_list(); ret = delete_from_list(i); if(ret != 0) { printf("\n delete [val = %d] failed, no such element found\n",i); } else { printf("\n delete [val = %d] passed \n",i); } print_list(); } return 0; }
static void random_test (void) { /* * outline: * 1. create new tree and empty list of intervals * 2. insert some intervals into tree and list * 3. do various searches, compare results of both structures * 4. delete some intervals * 5. do various searches, compare results of both structures * 6. free memory */ gint i, start, end; EInterval *interval = NULL; EIntervalTree *tree; GList *l1, *l2, *next; gint num_deleted = 0; tree = e_intervaltree_new (); for (i = 0; i < NUM_INTERVALS_CLOSED; i++) { ECalComponent *comp; start = g_rand_int_range (myrand, 0, 1000); end = g_rand_int_range (myrand, start, 2000); comp = create_test_component (start, end); if (!comp) { g_message (G_STRLOC ": error"); exit (-1); } interval = g_new (EInterval, 1); interval->start = start; interval->end = end; interval->comp = comp; list = g_list_insert (list, interval, -1); e_intervaltree_insert (tree, start, end, comp); } /* insert open ended intervals */ for (i = 0; i < NUM_INTERVALS_OPEN; i++) { ECalComponent *comp; start = g_rand_int_range (myrand, 0, 1000); comp = create_test_component (start, end); if (!comp) { g_message (G_STRLOC ": error"); exit (-1); } interval = g_new (EInterval, 1); interval->start = start; interval->end = _TIME_MAX; interval->comp = comp; list = g_list_insert (list, interval, -1); e_intervaltree_insert (tree, start, interval->end, comp); /* g_print ("%d - %d\n", start, interval->end); */ } g_print ("Number of intervals inserted: %d\n", NUM_INTERVALS_CLOSED + NUM_INTERVALS_OPEN); for (i = 0; i < NUM_SEARCHES; i++) { start = g_rand_int_range (myrand, 0, 1000); end = g_rand_int_range (myrand, 2000, _TIME_MAX); /* g_print ("Search for : %d - %d\n", start, end); */ l1 = e_intervaltree_search (tree, start, end); l2 = search_in_list (list, start, end); if (!compare_interval_lists (l2, l1)) { e_intervaltree_dump (tree); g_message (G_STRLOC "Error"); exit (-1); } /* g_print ("OK\n"); */ g_list_foreach (l1, (GFunc)g_object_unref, NULL); g_list_foreach (l2, (GFunc)unref_comp, NULL); g_list_free (l1); g_list_free (l2); } /* open-ended intervals */ for (i = 0; i < 20; i++) { start = g_rand_int_range (myrand, 0, 1000); end = _TIME_MAX; /* g_print ("Search for : %d - %d\n", start, end); */ l1 = e_intervaltree_search (tree, start, end); l2 = search_in_list (list, start, end); if (!compare_interval_lists (l2, l1)) { e_intervaltree_dump (tree); g_message (G_STRLOC "Error"); exit (-1); } /* g_print ("OK\n"); */ g_list_foreach (l1, (GFunc)g_object_unref, NULL); g_list_foreach (l2, (GFunc)unref_comp, NULL); g_list_free (l1); g_list_free (l2); } l1 = list; while (l1) { /* perhaps we will delete l1 */ next = l1->next; if (g_rand_double (myrand) < pbality_delete) { ECalComponent *comp; const gchar *uid = NULL; gchar *rid; interval = (EInterval*) l1->data; comp = interval->comp; /* delete l1 */ /* g_print ("Deleting interval %d - %d\n", interval->start, interval->end); */ rid = e_cal_component_get_recurid_as_string (comp); e_cal_component_get_uid (comp, &uid); if (!e_intervaltree_remove (tree, uid, rid)) { g_free (rid); e_intervaltree_dump (tree); g_print ("Deleting interval %d - %d ERROR\n", interval->start, interval->end); exit (-1); } g_free (rid); g_object_unref (interval->comp); g_free (l1->data); list = g_list_delete_link (list, l1); num_deleted++; } l1 = next; } g_print ("Number of intervals deleted: %d\n", num_deleted); for (i = 0; i < NUM_SEARCHES; i++) { start = g_rand_int_range (myrand, 0, 1000); end = g_rand_int_range (myrand, start, 2000); /* g_print ("Search for : %d - %d\n", start, end); */ l1 = e_intervaltree_search (tree, start, end); /* g_print ("Results from tree:\n"); print_nodes_list (l1); */ l2 = search_in_list (list, start, end); if (!compare_interval_lists (l2, l1)) { g_print ("ERROR!\n\n"); return; } g_list_foreach (l1, (GFunc)g_object_unref, NULL); g_list_foreach (l2, (GFunc)unref_comp, NULL); g_list_free (l1); g_list_free (l2); /* g_print ("OK\n"); */ } e_intervaltree_destroy (tree); g_list_foreach (list, (GFunc)unref_comp, NULL); g_list_free (list); }